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