1 package org.apache.maven.plugin.compiler;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
42
43
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
52
53
54 @Parameter ( property = "maven.test.skip" )
55 private boolean skip;
56
57
58
59
60 @Parameter ( defaultValue = "${project.testCompileSourceRoots}", readonly = true, required = true )
61 private List<String> compileSourceRoots;
62
63
64
65
66 @Parameter ( defaultValue = "${project.testClasspathElements}", required = true, readonly = true )
67 private List<String> classpathElements;
68
69
70
71
72 @Parameter ( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true )
73 private File outputDirectory;
74
75
76
77
78 @Parameter
79 private Set<String> testIncludes = new HashSet<String>();
80
81
82
83
84 @Parameter
85 private Set<String> testExcludes = new HashSet<String>();
86
87
88
89
90
91
92 @Parameter ( property = "maven.compiler.testSource" )
93 private String testSource;
94
95
96
97
98
99
100 @Parameter ( property = "maven.compiler.testTarget" )
101 private String testTarget;
102
103
104
105
106
107
108
109
110
111
112
113
114
115 @Parameter
116 private Map<String, String> testCompilerArguments;
117
118
119
120
121
122
123
124
125
126
127
128
129 @Parameter
130 private String testCompilerArgument;
131
132
133
134
135
136
137
138
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
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 }