View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.resources;
20  
21  import java.nio.file.Path;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  
25  import org.apache.maven.api.ProjectScope;
26  import org.apache.maven.api.plugin.MojoException;
27  import org.apache.maven.api.plugin.annotations.Mojo;
28  import org.apache.maven.api.plugin.annotations.Parameter;
29  import org.apache.maven.api.services.ProjectManager;
30  import org.apache.maven.shared.filtering.Resource;
31  
32  /**
33   * Copy resources for the test source code to the test output directory.
34   * Always uses the project.build.testResources element to specify the resources to copy.
35   *
36   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
37   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
38   */
39  @Mojo(name = "testResources", defaultPhase = "process-test-resources", projectRequired = true)
40  public class TestResourcesMojo extends ResourcesMojo {
41      /**
42       * The output directory into which to copy the resources.
43       */
44      @Parameter(defaultValue = "${project.build.testOutputDirectory}", required = true)
45      private Path outputDirectory;
46  
47      /**
48       * The list of resources we want to transfer.
49       */
50      @Parameter
51      private List<Resource> resources;
52  
53      /**
54       * Set this to 'true' to bypass copying of test resources.
55       * Its use is NOT RECOMMENDED, but quite convenient on occasion.
56       * @since 2.6
57       */
58      @Parameter(property = "maven.test.skip", defaultValue = "false")
59      private boolean skip;
60  
61      /**
62       * {@inheritDoc}
63       */
64      public void execute() throws MojoException {
65          if (skip) {
66              getLog().info("Not copying test resources");
67              return;
68          }
69  
70          if (resources == null) {
71              resources = session.getService(ProjectManager.class).getResources(project, ProjectScope.TEST).stream()
72                      .map(ResourceUtils::newResource)
73                      .collect(Collectors.toList());
74          }
75  
76          super.doExecute();
77      }
78  
79      /** {@inheritDoc} */
80      public Path getOutputDirectory() {
81          return outputDirectory;
82      }
83  
84      /** {@inheritDoc} */
85      public void setOutputDirectory(Path outputDirectory) {
86          this.outputDirectory = outputDirectory;
87      }
88  
89      /** {@inheritDoc} */
90      public List<Resource> getResources() {
91          return resources;
92      }
93  
94      /** {@inheritDoc} */
95      public void setResources(List<Resource> resources) {
96          this.resources = resources;
97      }
98  }