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