View Javadoc

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