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