View Javadoc
1   package org.apache.maven.plugin.compiler;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.LifecyclePhase;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.apache.maven.plugins.annotations.ResolutionScope;
27  import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
28  import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
29  import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
30  
31  import java.io.File;
32  import java.util.Collections;
33  import java.util.HashSet;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  
38  /**
39   * Compiles application test sources.
40   *
41   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
42   * @version $Id: TestCompilerMojo.html 944784 2015-03-23 18:41:16Z khmarbaise $
43   * @since 2.0
44   */
45  @Mojo( name = "testCompile", defaultPhase = LifecyclePhase.TEST_COMPILE, threadSafe = true,
46                  requiresDependencyResolution = ResolutionScope.TEST )
47  public class TestCompilerMojo
48      extends AbstractCompilerMojo
49  {
50      /**
51       * Set this to 'true' to bypass compilation of test sources.
52       * Its use is NOT RECOMMENDED, but quite convenient on occasion.
53       */
54      @Parameter ( property = "maven.test.skip" )
55      private boolean skip;
56  
57      /**
58       * The source directories containing the test-source to be compiled.
59       */
60      @Parameter ( defaultValue = "${project.testCompileSourceRoots}", readonly = true, required = true )
61      private List<String> compileSourceRoots;
62  
63      /**
64       * Project test classpath.
65       */
66      @Parameter ( defaultValue = "${project.testClasspathElements}", required = true, readonly = true )
67      private List<String> classpathElements;
68  
69      /**
70       * The directory where compiled test classes go.
71       */
72      @Parameter ( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true )
73      private File outputDirectory;
74  
75      /**
76       * A list of inclusion filters for the compiler.
77       */
78      @Parameter
79      private Set<String> testIncludes = new HashSet<String>();
80  
81      /**
82       * A list of exclusion filters for the compiler.
83       */
84      @Parameter
85      private Set<String> testExcludes = new HashSet<String>();
86  
87      /**
88       * The -source argument for the test Java compiler.
89       *
90       * @since 2.1
91       */
92      @Parameter ( property = "maven.compiler.testSource" )
93      private String testSource;
94  
95      /**
96       * The -target argument for the test Java compiler.
97       *
98       * @since 2.1
99       */
100     @Parameter ( property = "maven.compiler.testTarget" )
101     private String testTarget;
102 
103 
104     /**
105      * <p>
106      * Sets the arguments to be passed to test compiler (prepending a dash) if fork is set to true.
107      * </p>
108      * <p>
109      * This is because the list of valid arguments passed to a Java compiler
110      * varies based on the compiler version.
111      * </p>
112      *
113      * @since 2.1
114      */
115     @Parameter
116     private Map<String, String> testCompilerArguments;
117 
118     /**
119      * <p>
120      * Sets the unformatted argument string to be passed to test compiler if fork is set to true.
121      * </p>
122      * <p>
123      * This is because the list of valid arguments passed to a Java compiler
124      * varies based on the compiler version.
125      * </p>
126      *
127      * @since 2.1
128      */
129     @Parameter
130     private String testCompilerArgument;
131 
132     /**
133      * <p>
134      * Specify where to place generated source files created by annotation processing.
135      * Only applies to JDK 1.6+
136      * </p>
137      *
138      * @since 2.2
139      */
140     @Parameter ( defaultValue = "${project.build.directory}/generated-test-sources/test-annotations" )
141     private File generatedTestSourcesDirectory;
142 
143 
144     public void execute()
145         throws MojoExecutionException, CompilationFailureException
146     {
147         if ( skip )
148         {
149             getLog().info( "Not compiling test sources" );
150         }
151         else
152         {
153             super.execute();
154         }
155     }
156 
157     protected List<String> getCompileSourceRoots()
158     {
159         return compileSourceRoots;
160     }
161 
162     protected List<String> getClasspathElements()
163     {
164         return classpathElements;
165     }
166 
167     protected File getOutputDirectory()
168     {
169         return outputDirectory;
170     }
171 
172     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
173     {
174         SourceInclusionScanner scanner;
175 
176         if ( testIncludes.isEmpty() && testExcludes.isEmpty() )
177         {
178             scanner = new StaleSourceScanner( staleMillis );
179         }
180         else
181         {
182             if ( testIncludes.isEmpty() )
183             {
184                 testIncludes.add( "**/*.java" );
185             }
186             scanner = new StaleSourceScanner( staleMillis, testIncludes, testExcludes );
187         }
188 
189         return scanner;
190     }
191 
192     protected SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding )
193     {
194         SourceInclusionScanner scanner;
195 
196         // it's not defined if we get the ending with or without the dot '.'
197         String defaultIncludePattern = "**/*" + ( inputFileEnding.startsWith( "." ) ? "" : "." ) + inputFileEnding;
198 
199         if ( testIncludes.isEmpty() && testExcludes.isEmpty() )
200         {
201             testIncludes = Collections.singleton( defaultIncludePattern );
202             scanner = new SimpleSourceInclusionScanner( testIncludes, Collections.<String>emptySet() );
203         }
204         else
205         {
206             if ( testIncludes.isEmpty() )
207             {
208                 testIncludes.add( defaultIncludePattern );
209             }
210             scanner = new SimpleSourceInclusionScanner( testIncludes, testExcludes );
211         }
212 
213         return scanner;
214     }
215 
216     protected String getSource()
217     {
218         return testSource == null ? source : testSource;
219     }
220 
221     protected String getTarget()
222     {
223         return testTarget == null ? target : testTarget;
224     }
225 
226     protected String getCompilerArgument()
227     {
228         return testCompilerArgument == null ? compilerArgument : testCompilerArgument;
229     }
230 
231     protected Map<String, String> getCompilerArguments()
232     {
233         return testCompilerArguments == null ? compilerArguments : testCompilerArguments;
234     }
235 
236     protected File getGeneratedSourcesDirectory()
237     {
238         return generatedTestSourcesDirectory;
239     }
240 
241     @Override
242     protected boolean isTestCompile()
243     {
244         return true;
245     }
246 
247 }