1 | |
package org.apache.maven.plugin.ant; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.OutputStreamWriter; |
26 | |
import java.net.URL; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Arrays; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
import java.util.Properties; |
33 | |
|
34 | |
import org.apache.maven.artifact.Artifact; |
35 | |
import org.apache.maven.model.Repository; |
36 | |
import org.apache.maven.model.Resource; |
37 | |
import org.apache.maven.project.MavenProject; |
38 | |
import org.apache.maven.settings.Settings; |
39 | |
import org.apache.tools.ant.Main; |
40 | |
import org.codehaus.plexus.util.FileUtils; |
41 | |
import org.codehaus.plexus.util.IOUtil; |
42 | |
import org.codehaus.plexus.util.StringUtils; |
43 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
44 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
45 | |
import org.codehaus.plexus.util.xml.XmlWriterUtil; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public class AntBuildWriter |
60 | |
{ |
61 | |
|
62 | |
|
63 | |
|
64 | |
protected static final int DEFAULT_INDENTATION_SIZE = XmlWriterUtil.DEFAULT_INDENTATION_SIZE; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
protected static final String DEFAULT_BUILD_FILENAME = Main.DEFAULT_BUILD_FILENAME; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
protected static final String DEFAULT_MAVEN_BUILD_FILENAME = "maven-build.xml"; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
protected static final String DEFAULT_MAVEN_PROPERTIES_FILENAME = "maven-build.properties"; |
80 | |
|
81 | |
private MavenProject project; |
82 | |
|
83 | |
private ArtifactResolverWrapper artifactResolverWrapper; |
84 | |
|
85 | |
private File localRepository; |
86 | |
|
87 | |
private Settings settings; |
88 | |
|
89 | |
private boolean overwrite; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public AntBuildWriter( MavenProject project, ArtifactResolverWrapper artifactResolverWrapper, Settings settings, |
98 | |
boolean overwrite ) |
99 | 27 | { |
100 | 27 | this.project = project; |
101 | 27 | this.artifactResolverWrapper = artifactResolverWrapper; |
102 | 27 | this.localRepository = new File( artifactResolverWrapper.getLocalRepository().getBasedir() ); |
103 | 27 | this.settings = settings; |
104 | 27 | this.overwrite = overwrite; |
105 | 27 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
protected void writeBuildXmls() |
113 | |
throws IOException |
114 | |
{ |
115 | 27 | writeGeneratedBuildXml(); |
116 | 27 | writeBuildXml(); |
117 | 27 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
protected void writeBuildProperties() |
126 | |
throws IOException |
127 | |
{ |
128 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
129 | |
{ |
130 | 0 | return; |
131 | |
} |
132 | |
|
133 | 27 | Properties properties = new Properties(); |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | 27 | addProperty( properties, "maven.build.finalName", AntBuildWriterUtil.toRelative( project.getBasedir(), project |
140 | |
.getBuild().getFinalName() ) ); |
141 | |
|
142 | |
|
143 | 27 | addProperty( properties, "maven.build.dir", AntBuildWriterUtil.toRelative( project.getBasedir(), project.getBuild() |
144 | |
.getDirectory() ) ); |
145 | 27 | addProperty( properties, "project.build.directory", "${maven.build.dir}" ); |
146 | |
|
147 | |
|
148 | 27 | addProperty( properties, "maven.build.outputDir", "${maven.build.dir}/" |
149 | |
+ AntBuildWriterUtil.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ), |
150 | |
project.getBuild().getOutputDirectory() ) ); |
151 | 27 | addProperty( properties, "project.build.outputDirectory", "${maven.build.outputDir}" ); |
152 | |
|
153 | |
|
154 | 27 | if ( !project.getCompileSourceRoots().isEmpty() ) |
155 | |
{ |
156 | 27 | String[] compileSourceRoots = (String[]) project.getCompileSourceRoots().toArray( new String[0] ); |
157 | 54 | for ( int i = 0; i < compileSourceRoots.length; i++ ) |
158 | |
{ |
159 | 27 | addProperty( properties, "maven.build.srcDir." + i, AntBuildWriterUtil.toRelative( project.getBasedir(), |
160 | |
compileSourceRoots[i] ) ); |
161 | |
} |
162 | |
} |
163 | |
|
164 | 27 | if ( project.getBuild().getResources() != null ) |
165 | |
{ |
166 | 27 | Resource[] array = (Resource[]) project.getBuild().getResources().toArray( new Resource[0] ); |
167 | 54 | for ( int i = 0; i < array.length; i++ ) |
168 | |
{ |
169 | 27 | addProperty( properties, "maven.build.resourceDir." + i, AntBuildWriterUtil.toRelative( project.getBasedir(), |
170 | |
array[i].getDirectory() ) ); |
171 | |
} |
172 | |
} |
173 | |
|
174 | |
|
175 | 27 | addProperty( properties, "maven.build.testOutputDir", "${maven.build.dir}/" |
176 | |
+ AntBuildWriterUtil.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ), |
177 | |
project.getBuild().getTestOutputDirectory() ) ); |
178 | |
|
179 | 27 | if ( !project.getTestCompileSourceRoots().isEmpty() ) |
180 | |
{ |
181 | 27 | String[] compileSourceRoots = (String[]) project.getTestCompileSourceRoots().toArray( new String[0] ); |
182 | 54 | for ( int i = 0; i < compileSourceRoots.length; i++ ) |
183 | |
{ |
184 | 27 | addProperty( properties, "maven.build.testDir." + i, AntBuildWriterUtil.toRelative( project.getBasedir(), |
185 | |
compileSourceRoots[i] ) ); |
186 | |
} |
187 | |
} |
188 | |
|
189 | 27 | if ( project.getBuild().getTestResources() != null ) |
190 | |
{ |
191 | 27 | Resource[] array = (Resource[]) project.getBuild().getTestResources().toArray( new Resource[0] ); |
192 | 54 | for ( int i = 0; i < array.length; i++ ) |
193 | |
{ |
194 | 27 | addProperty( properties, "maven.build.testResourceDir." + i, AntBuildWriterUtil |
195 | |
.toRelative( project.getBasedir(), array[i].getDirectory() ) ); |
196 | |
} |
197 | |
} |
198 | |
|
199 | 27 | addProperty( properties, "maven.test.reports", "${maven.build.dir}/test-reports" ); |
200 | |
|
201 | 27 | addProperty( properties, "maven.reporting.outputDirectory", "${maven.build.dir}/site" ); |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | 27 | addProperty( properties, "maven.settings.offline", String.valueOf( settings.isOffline() ) ); |
208 | 27 | addProperty( properties, "maven.settings.interactiveMode", String.valueOf( settings.isInteractiveMode() ) ); |
209 | 27 | addProperty( properties, "maven.repo.local", getLocalRepositoryPath() ); |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | 27 | if ( project.getProperties() != null ) |
216 | |
{ |
217 | 27 | for ( Iterator it = project.getProperties().entrySet().iterator(); it.hasNext(); ) |
218 | |
{ |
219 | 9 | Map.Entry property = (Map.Entry) it.next(); |
220 | 9 | addProperty( properties, property.getKey().toString(), property.getValue().toString() ); |
221 | |
} |
222 | |
} |
223 | |
|
224 | 27 | FileOutputStream os = |
225 | |
new FileOutputStream( new File( project.getBasedir(), DEFAULT_MAVEN_PROPERTIES_FILENAME ) ); |
226 | |
try |
227 | |
{ |
228 | 27 | properties.store( os, "Generated by Maven Ant Plugin - DO NOT EDIT THIS FILE!" ); |
229 | |
} |
230 | |
finally |
231 | |
{ |
232 | 27 | IOUtil.close( os ); |
233 | 27 | } |
234 | 27 | } |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
private void writeGeneratedBuildXml() |
243 | |
throws IOException |
244 | |
{ |
245 | |
|
246 | 27 | File outputFile = new File( project.getBasedir(), DEFAULT_MAVEN_BUILD_FILENAME ); |
247 | |
|
248 | 27 | String encoding = "UTF-8"; |
249 | |
|
250 | 27 | OutputStreamWriter w = new OutputStreamWriter( new FileOutputStream( outputFile ), encoding ); |
251 | |
|
252 | 27 | XMLWriter writer = new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", DEFAULT_INDENTATION_SIZE ), encoding, |
253 | |
null ); |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | 27 | AntBuildWriterUtil.writeHeader( writer ); |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | 27 | writer.startElement( "project" ); |
266 | 27 | writer.addAttribute( "name", project.getArtifactId() + "-from-maven" ); |
267 | 27 | writer.addAttribute( "default", "package" ); |
268 | 27 | writer.addAttribute( "basedir", "." ); |
269 | |
|
270 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | 27 | writeProperties( writer ); |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | 27 | writeBuildPathDefinition( writer ); |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | 27 | writeCleanTarget( writer ); |
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | 27 | List compileSourceRoots = AntBuildWriterUtil.removeEmptyCompileSourceRoots( project.getCompileSourceRoots() ); |
295 | 27 | writeCompileTarget( writer, compileSourceRoots ); |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | 27 | List testCompileSourceRoots = AntBuildWriterUtil.removeEmptyCompileSourceRoots( project |
302 | |
.getTestCompileSourceRoots() ); |
303 | 27 | writeCompileTestsTarget( writer, testCompileSourceRoots ); |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | 27 | writeTestTargets( writer, testCompileSourceRoots ); |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | 27 | writeJavadocTarget( writer ); |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | 27 | writePackageTarget( writer ); |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | 27 | writeGetDepsTarget( writer ); |
325 | |
|
326 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
327 | |
|
328 | 27 | writer.endElement(); |
329 | |
|
330 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
331 | |
|
332 | 27 | IOUtil.close( w ); |
333 | 27 | } |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
private void writeBuildXml() |
342 | |
throws IOException |
343 | |
{ |
344 | 27 | File outputFile = new File( project.getBasedir(), DEFAULT_BUILD_FILENAME ); |
345 | |
|
346 | 27 | if ( outputFile.exists() && !overwrite ) |
347 | |
{ |
348 | 27 | return; |
349 | |
} |
350 | |
|
351 | 0 | String encoding = "UTF-8"; |
352 | |
|
353 | 0 | OutputStreamWriter w = new OutputStreamWriter( new FileOutputStream( outputFile ), encoding ); |
354 | |
|
355 | 0 | XMLWriter writer = new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", DEFAULT_INDENTATION_SIZE ), encoding, |
356 | |
null ); |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | 0 | AntBuildWriterUtil.writeAntVersionHeader( writer ); |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | 0 | writer.startElement( "project" ); |
369 | 0 | writer.addAttribute( "name", project.getArtifactId() ); |
370 | 0 | writer.addAttribute( "default", "package" ); |
371 | 0 | writer.addAttribute( "basedir", "." ); |
372 | |
|
373 | 0 | XmlWriterUtil.writeLineBreak( writer ); |
374 | |
|
375 | 0 | XmlWriterUtil.writeCommentText( writer, "Import " + DEFAULT_MAVEN_BUILD_FILENAME |
376 | |
+ " into the current project", 1 ); |
377 | |
|
378 | 0 | writer.startElement( "import" ); |
379 | 0 | writer.addAttribute( "file", DEFAULT_MAVEN_BUILD_FILENAME ); |
380 | 0 | writer.endElement(); |
381 | |
|
382 | 0 | XmlWriterUtil.writeLineBreak( writer, 1, 1 ); |
383 | |
|
384 | 0 | XmlWriterUtil.writeCommentText( writer, "Help target", 1 ); |
385 | |
|
386 | 0 | writer.startElement( "target" ); |
387 | 0 | writer.addAttribute( "name", "help" ); |
388 | |
|
389 | 0 | writer.startElement( "echo" ); |
390 | 0 | writer.addAttribute( "message", "Please run: $ant -projecthelp" ); |
391 | 0 | writer.endElement(); |
392 | |
|
393 | 0 | writer.endElement(); |
394 | |
|
395 | 0 | XmlWriterUtil.writeLineBreak( writer, 2 ); |
396 | |
|
397 | 0 | writer.endElement(); |
398 | |
|
399 | 0 | XmlWriterUtil.writeLineBreak( writer ); |
400 | |
|
401 | 0 | IOUtil.close( w ); |
402 | 0 | } |
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
private void writeProperties( XMLWriter writer ) |
410 | |
{ |
411 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
412 | |
{ |
413 | 0 | return; |
414 | |
} |
415 | |
|
416 | |
|
417 | |
|
418 | 27 | XmlWriterUtil.writeCommentText( writer, "Build environment properties", 1 ); |
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | 27 | writer.startElement( "property" ); |
425 | 27 | writer.addAttribute( "file", "${user.home}/.m2/maven.properties" ); |
426 | 27 | writer.endElement(); |
427 | |
|
428 | 27 | writer.startElement( "property" ); |
429 | 27 | writer.addAttribute( "file", DEFAULT_MAVEN_PROPERTIES_FILENAME ); |
430 | 27 | writer.endElement(); |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | 27 | XmlWriterUtil.writeLineBreak( writer, 2, 1 ); |
437 | |
|
438 | 27 | writer.startElement( "property" ); |
439 | 27 | writer.addAttribute( "name", "maven.build.finalName" ); |
440 | 27 | writer.addAttribute( "value", project.getBuild().getFinalName() ); |
441 | 27 | writer.endElement(); |
442 | |
|
443 | 27 | writer.startElement( "property" ); |
444 | 27 | writer.addAttribute( "name", "maven.build.dir" ); |
445 | 27 | writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), project.getBuild().getDirectory() ) ); |
446 | 27 | writer.endElement(); |
447 | |
|
448 | 27 | writer.startElement( "property" ); |
449 | 27 | writer.addAttribute( "name", "maven.build.outputDir" ); |
450 | 27 | writer.addAttribute( "value", "${maven.build.dir}/" |
451 | |
+ AntBuildWriterUtil.toRelative( new File( project.getBuild().getDirectory() ), project.getBuild() |
452 | |
.getOutputDirectory() ) ); |
453 | 27 | writer.endElement(); |
454 | |
|
455 | 27 | if ( !project.getCompileSourceRoots().isEmpty() ) |
456 | |
{ |
457 | 27 | String[] compileSourceRoots = (String[]) project.getCompileSourceRoots().toArray( new String[0] ); |
458 | 54 | for ( int i = 0; i < compileSourceRoots.length; i++ ) |
459 | |
{ |
460 | 27 | writer.startElement( "property" ); |
461 | 27 | writer.addAttribute( "name", "maven.build.srcDir." + i ); |
462 | 27 | writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), compileSourceRoots[i] ) ); |
463 | 27 | writer.endElement(); |
464 | |
} |
465 | |
} |
466 | |
|
467 | 27 | if ( project.getBuild().getResources() != null ) |
468 | |
{ |
469 | 27 | Resource[] array = (Resource[]) project.getBuild().getResources().toArray( new Resource[0] ); |
470 | 54 | for ( int i = 0; i < array.length; i++ ) |
471 | |
{ |
472 | 27 | writer.startElement( "property" ); |
473 | 27 | writer.addAttribute( "name", "maven.build.resourceDir." + i ); |
474 | 27 | writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), array[i].getDirectory() ) ); |
475 | 27 | writer.endElement(); |
476 | |
} |
477 | |
} |
478 | |
|
479 | 27 | writer.startElement( "property" ); |
480 | 27 | writer.addAttribute( "name", "maven.build.testOutputDir" ); |
481 | 27 | writer.addAttribute( "value", "${maven.build.dir}/" |
482 | |
+ AntBuildWriterUtil.toRelative( new File( project.getBuild().getDirectory() ), project.getBuild() |
483 | |
.getTestOutputDirectory() ) ); |
484 | 27 | writer.endElement(); |
485 | |
|
486 | 27 | if ( !project.getTestCompileSourceRoots().isEmpty() ) |
487 | |
{ |
488 | 27 | String[] compileSourceRoots = (String[]) project.getTestCompileSourceRoots().toArray( new String[0] ); |
489 | 54 | for ( int i = 0; i < compileSourceRoots.length; i++ ) |
490 | |
{ |
491 | 27 | writer.startElement( "property" ); |
492 | 27 | writer.addAttribute( "name", "maven.build.testDir." + i ); |
493 | 27 | writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), compileSourceRoots[i] ) ); |
494 | 27 | writer.endElement(); |
495 | |
} |
496 | |
} |
497 | |
|
498 | 27 | if ( project.getBuild().getTestResources() != null ) |
499 | |
{ |
500 | 27 | Resource[] array = (Resource[]) project.getBuild().getTestResources().toArray( new Resource[0] ); |
501 | 54 | for ( int i = 0; i < array.length; i++ ) |
502 | |
{ |
503 | 27 | writer.startElement( "property" ); |
504 | 27 | writer.addAttribute( "name", "maven.build.testResourceDir." + i ); |
505 | 27 | writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), array[i].getDirectory() ) ); |
506 | 27 | writer.endElement(); |
507 | |
} |
508 | |
} |
509 | |
|
510 | 27 | writer.startElement( "property" ); |
511 | 27 | writer.addAttribute( "name", "maven.test.reports" ); |
512 | 27 | writer.addAttribute( "value", "${maven.build.dir}/test-reports" ); |
513 | 27 | writer.endElement(); |
514 | |
|
515 | 27 | String reportingOutputDir = project.getReporting().getOutputDirectory(); |
516 | |
|
517 | 27 | if ( !new File( reportingOutputDir ).isAbsolute() ) |
518 | |
{ |
519 | 0 | reportingOutputDir = new File( project.getBasedir(), reportingOutputDir ).getAbsolutePath(); |
520 | |
} |
521 | 27 | writer.startElement( "property" ); |
522 | 27 | writer.addAttribute( "name", "maven.reporting.outputDirectory" ); |
523 | 27 | writer.addAttribute( "value", "${maven.build.dir}/" |
524 | |
+ AntBuildWriterUtil.toRelative( new File( project.getBuild().getDirectory() ), reportingOutputDir ) ); |
525 | 27 | writer.endElement(); |
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | 27 | XmlWriterUtil.writeLineBreak( writer, 2, 1 ); |
532 | |
|
533 | 27 | writer.startElement( "property" ); |
534 | 27 | writer.addAttribute( "name", "maven.repo.local" ); |
535 | 27 | writer.addAttribute( "value", "${user.home}/.m2/repository" ); |
536 | 27 | writer.endElement(); |
537 | |
|
538 | 27 | writer.startElement( "property" ); |
539 | 27 | writer.addAttribute( "name", "maven.settings.offline" ); |
540 | 27 | writer.addAttribute( "value", String.valueOf( settings.isOffline() ) ); |
541 | 27 | writer.endElement(); |
542 | |
|
543 | 27 | writer.startElement( "property" ); |
544 | 27 | writer.addAttribute( "name", "maven.settings.interactiveMode" ); |
545 | 27 | writer.addAttribute( "value", String.valueOf( settings.isInteractiveMode() ) ); |
546 | 27 | writer.endElement(); |
547 | |
|
548 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
549 | 27 | } |
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
private String getLocalRepositoryPath() |
558 | |
{ |
559 | 27 | String userHome = System.getProperty( "user.home" ); |
560 | 27 | String defaultPath = ( userHome + "/.m2/repository" ).replace( '\\', '/' ); |
561 | 27 | String actualPath = localRepository.getAbsolutePath().replace( '\\', '/' ); |
562 | 27 | if ( actualPath.equals( defaultPath ) ) |
563 | |
{ |
564 | 0 | return "${user.home}/.m2/repository"; |
565 | |
} |
566 | |
else |
567 | |
{ |
568 | 27 | return localRepository.getAbsolutePath(); |
569 | |
} |
570 | |
} |
571 | |
|
572 | |
|
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
private void writeBuildPathDefinition( XMLWriter writer ) |
578 | |
{ |
579 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
580 | |
{ |
581 | 0 | return; |
582 | |
} |
583 | |
|
584 | 27 | XmlWriterUtil.writeCommentText( writer, "Defining classpaths", 1 ); |
585 | |
|
586 | 27 | writer.startElement( "path" ); |
587 | 27 | writer.addAttribute( "id", "build.classpath" ); |
588 | 27 | writer.startElement( "fileset" ); |
589 | 27 | writer.addAttribute( "dir", "${maven.repo.local}" ); |
590 | 27 | if ( !project.getCompileArtifacts().isEmpty() ) |
591 | |
{ |
592 | 27 | for ( Iterator i = project.getCompileArtifacts().iterator(); i.hasNext(); ) |
593 | |
{ |
594 | 27 | Artifact artifact = (Artifact) i.next(); |
595 | 27 | String path = artifactResolverWrapper.getLocalArtifactPath( artifact ); |
596 | 27 | writer.startElement( "include" ); |
597 | 27 | writer.addAttribute( "name", path ); |
598 | 27 | writer.endElement(); |
599 | |
} |
600 | |
} |
601 | |
else |
602 | |
{ |
603 | 0 | writer.startElement( "include" ); |
604 | 0 | writer.addAttribute( "name", "*.jar" ); |
605 | 0 | writer.endElement(); |
606 | |
} |
607 | 27 | writer.endElement(); |
608 | 27 | writer.endElement(); |
609 | |
|
610 | 27 | writer.startElement( "path" ); |
611 | 27 | writer.addAttribute( "id", "build.test.classpath" ); |
612 | 27 | writer.startElement( "fileset" ); |
613 | 27 | writer.addAttribute( "dir", "${maven.repo.local}" ); |
614 | 27 | if ( !project.getTestArtifacts().isEmpty() ) |
615 | |
{ |
616 | 27 | for ( Iterator i = project.getTestArtifacts().iterator(); i.hasNext(); ) |
617 | |
{ |
618 | 27 | Artifact artifact = (Artifact) i.next(); |
619 | 27 | String path = artifactResolverWrapper.getLocalArtifactPath( artifact ); |
620 | 27 | writer.startElement( "include" ); |
621 | 27 | writer.addAttribute( "name", path ); |
622 | 27 | writer.endElement(); |
623 | |
} |
624 | |
} |
625 | |
else |
626 | |
{ |
627 | 0 | writer.startElement( "include" ); |
628 | 0 | writer.addAttribute( "name", "*.jar" ); |
629 | 0 | writer.endElement(); |
630 | |
} |
631 | 27 | writer.endElement(); |
632 | 27 | writer.endElement(); |
633 | |
|
634 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
635 | 27 | } |
636 | |
|
637 | |
|
638 | |
|
639 | |
|
640 | |
|
641 | |
|
642 | |
private void writeCleanTarget( XMLWriter writer ) |
643 | |
{ |
644 | 27 | XmlWriterUtil.writeCommentText( writer, "Cleaning up target", 1 ); |
645 | |
|
646 | 27 | writer.startElement( "target" ); |
647 | 27 | writer.addAttribute( "name", "clean" ); |
648 | 27 | writer.addAttribute( "description", "Clean the output directory" ); |
649 | |
|
650 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
651 | |
{ |
652 | 0 | if ( project.getModules() != null ) |
653 | |
{ |
654 | 0 | for ( Iterator it = project.getModules().iterator(); it.hasNext(); ) |
655 | |
{ |
656 | 0 | String moduleSubPath = (String) it.next(); |
657 | 0 | AntBuildWriterUtil.writeAntTask( writer, project, moduleSubPath, "clean" ); |
658 | |
} |
659 | |
} |
660 | |
} |
661 | |
else |
662 | |
{ |
663 | 27 | writer.startElement( "delete" ); |
664 | 27 | writer.addAttribute( "dir", "${maven.build.dir}" ); |
665 | 27 | writer.endElement(); |
666 | |
} |
667 | |
|
668 | 27 | writer.endElement(); |
669 | |
|
670 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
671 | 27 | } |
672 | |
|
673 | |
|
674 | |
|
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
private void writeCompileTarget( XMLWriter writer, List compileSourceRoots ) |
681 | |
throws IOException |
682 | |
{ |
683 | 27 | XmlWriterUtil.writeCommentText( writer, "Compilation target", 1 ); |
684 | |
|
685 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
686 | |
{ |
687 | 0 | writer.startElement( "target" ); |
688 | 0 | writer.addAttribute( "name", "compile" ); |
689 | 0 | writer.addAttribute( "description", "Compile the code" ); |
690 | 0 | if ( project.getModules() != null ) |
691 | |
{ |
692 | 0 | for ( Iterator it = project.getModules().iterator(); it.hasNext(); ) |
693 | |
{ |
694 | 0 | String moduleSubPath = (String) it.next(); |
695 | 0 | AntBuildWriterUtil.writeAntTask( writer, project, moduleSubPath, "compile" ); |
696 | |
} |
697 | |
} |
698 | 0 | writer.endElement(); |
699 | |
} |
700 | |
else |
701 | |
{ |
702 | 27 | writer.startElement( "target" ); |
703 | 27 | writer.addAttribute( "name", "compile" ); |
704 | 27 | writer.addAttribute( "depends", "get-deps" ); |
705 | 27 | writer.addAttribute( "description", "Compile the code" ); |
706 | |
|
707 | 27 | writeCompileTasks( writer, "${maven.build.outputDir}", compileSourceRoots, |
708 | |
project.getBuild().getResources(), null, false ); |
709 | |
|
710 | 27 | writer.endElement(); |
711 | |
} |
712 | |
|
713 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
714 | 27 | } |
715 | |
|
716 | |
|
717 | |
|
718 | |
|
719 | |
|
720 | |
|
721 | |
|
722 | |
|
723 | |
private void writeCompileTestsTarget( XMLWriter writer, List testCompileSourceRoots ) |
724 | |
throws IOException |
725 | |
{ |
726 | 27 | XmlWriterUtil.writeCommentText( writer, "Test-compilation target", 1 ); |
727 | |
|
728 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
729 | |
{ |
730 | 0 | writer.startElement( "target" ); |
731 | 0 | writer.addAttribute( "name", "compile-tests" ); |
732 | 0 | writer.addAttribute( "description", "Compile the test code" ); |
733 | 0 | if ( project.getModules() != null ) |
734 | |
{ |
735 | 0 | for ( Iterator it = project.getModules().iterator(); it.hasNext(); ) |
736 | |
{ |
737 | 0 | String moduleSubPath = (String) it.next(); |
738 | 0 | AntBuildWriterUtil.writeAntTask( writer, project, moduleSubPath, "compile-tests" ); |
739 | |
} |
740 | |
} |
741 | 0 | writer.endElement(); |
742 | |
} |
743 | |
else |
744 | |
{ |
745 | 27 | writer.startElement( "target" ); |
746 | 27 | writer.addAttribute( "name", "compile-tests" ); |
747 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "depends", "compile", 2 ); |
748 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "description", "Compile the test code", 2 ); |
749 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "unless", "maven.test.skip", 2 ); |
750 | |
|
751 | 27 | writeCompileTasks( writer, "${maven.build.testOutputDir}", testCompileSourceRoots, |
752 | |
project.getBuild().getTestResources(), "${maven.build.outputDir}", true ); |
753 | |
|
754 | 27 | writer.endElement(); |
755 | |
} |
756 | |
|
757 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
758 | 27 | } |
759 | |
|
760 | |
|
761 | |
|
762 | |
|
763 | |
|
764 | |
|
765 | |
|
766 | |
private void writeTestTargets( XMLWriter writer, List testCompileSourceRoots ) |
767 | |
throws IOException |
768 | |
{ |
769 | 27 | XmlWriterUtil.writeCommentText( writer, "Run all tests", 1 ); |
770 | |
|
771 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
772 | |
{ |
773 | 0 | writer.startElement( "target" ); |
774 | 0 | writer.addAttribute( "name", "test" ); |
775 | 0 | writer.addAttribute( "description", "Run the test cases" ); |
776 | 0 | if ( project.getModules() != null ) |
777 | |
{ |
778 | 0 | for ( Iterator it = project.getModules().iterator(); it.hasNext(); ) |
779 | |
{ |
780 | 0 | String moduleSubPath = (String) it.next(); |
781 | 0 | AntBuildWriterUtil.writeAntTask( writer, project, moduleSubPath, "test" ); |
782 | |
} |
783 | |
} |
784 | 0 | writer.endElement(); |
785 | |
} |
786 | |
else |
787 | |
{ |
788 | 27 | writer.startElement( "target" ); |
789 | 27 | writer.addAttribute( "name", "test" ); |
790 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "depends", "compile-tests, junit-missing", 2 ); |
791 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "unless", "junit.skipped", 2 ); |
792 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "description", "Run the test cases", 2 ); |
793 | |
|
794 | 27 | if ( !testCompileSourceRoots.isEmpty() ) |
795 | |
{ |
796 | 0 | writer.startElement( "mkdir" ); |
797 | 0 | writer.addAttribute( "dir", "${maven.test.reports}" ); |
798 | 0 | writer.endElement(); |
799 | |
|
800 | 0 | writer.startElement( "junit" ); |
801 | 0 | writer.addAttribute( "printSummary", "yes" ); |
802 | 0 | writer.addAttribute( "haltonerror", "true" ); |
803 | 0 | writer.addAttribute( "haltonfailure", "true" ); |
804 | 0 | writer.addAttribute( "fork", "true" ); |
805 | 0 | writer.addAttribute( "dir", "." ); |
806 | |
|
807 | 0 | writer.startElement( "sysproperty" ); |
808 | 0 | writer.addAttribute( "key", "basedir" ); |
809 | 0 | writer.addAttribute( "value", "." ); |
810 | 0 | writer.endElement(); |
811 | |
|
812 | 0 | writer.startElement( "formatter" ); |
813 | 0 | writer.addAttribute( "type", "xml" ); |
814 | 0 | writer.endElement(); |
815 | |
|
816 | 0 | writer.startElement( "formatter" ); |
817 | 0 | writer.addAttribute( "type", "plain" ); |
818 | 0 | writer.addAttribute( "usefile", "false" ); |
819 | 0 | writer.endElement(); |
820 | |
|
821 | 0 | writer.startElement( "classpath" ); |
822 | 0 | writer.startElement( "path" ); |
823 | 0 | writer.addAttribute( "refid", "build.test.classpath" ); |
824 | 0 | writer.endElement(); |
825 | 0 | writer.startElement( "pathelement" ); |
826 | 0 | writer.addAttribute( "location", "${maven.build.outputDir}" ); |
827 | 0 | writer.endElement(); |
828 | 0 | writer.startElement( "pathelement" ); |
829 | 0 | writer.addAttribute( "location", "${maven.build.testOutputDir}" ); |
830 | 0 | writer.endElement(); |
831 | 0 | writer.endElement(); |
832 | |
|
833 | 0 | writer.startElement( "batchtest" ); |
834 | 0 | writer.addAttribute( "todir", "${maven.test.reports}" ); |
835 | 0 | writer.addAttribute( "unless", "test" ); |
836 | |
|
837 | 0 | List includes = getTestIncludes(); |
838 | 0 | List excludes = getTestExcludes(); |
839 | |
|
840 | 0 | writeTestFilesets( writer, testCompileSourceRoots, includes, excludes ); |
841 | |
|
842 | 0 | writer.endElement(); |
843 | |
|
844 | 0 | writer.startElement( "batchtest" ); |
845 | 0 | writer.addAttribute( "todir", "${maven.test.reports}" ); |
846 | 0 | writer.addAttribute( "if", "test" ); |
847 | |
|
848 | 0 | includes = Arrays.asList( new String[] { "**/${test}.java" } ); |
849 | |
|
850 | 0 | writeTestFilesets( writer, testCompileSourceRoots, includes, excludes ); |
851 | |
|
852 | 0 | writer.endElement(); |
853 | |
|
854 | 0 | writer.endElement(); |
855 | |
} |
856 | 27 | writer.endElement(); |
857 | |
|
858 | 27 | XmlWriterUtil.writeLineBreak( writer, 2, 1 ); |
859 | |
|
860 | 27 | writer.startElement( "target" ); |
861 | 27 | writer.addAttribute( "name", "test-junit-present" ); |
862 | |
|
863 | 27 | writer.startElement( "available" ); |
864 | 27 | writer.addAttribute( "classname", "junit.framework.Test" ); |
865 | 27 | writer.addAttribute( "property", "junit.present" ); |
866 | 27 | writer.endElement(); |
867 | |
|
868 | 27 | writer.endElement(); |
869 | |
|
870 | 27 | XmlWriterUtil.writeLineBreak( writer, 2, 1 ); |
871 | |
|
872 | 27 | writer.startElement( "target" ); |
873 | 27 | writer.addAttribute( "name", "test-junit-status" ); |
874 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "depends", "test-junit-present", 2 ); |
875 | 27 | writer.startElement( "condition" ); |
876 | 27 | writer.addAttribute( "property", "junit.missing" ); |
877 | 27 | writer.startElement( "and" ); |
878 | 27 | writer.startElement( "isfalse" ); |
879 | 27 | writer.addAttribute( "value", "${junit.present}" ); |
880 | 27 | writer.endElement(); |
881 | 27 | writer.startElement( "isfalse" ); |
882 | 27 | writer.addAttribute( "value", "${maven.test.skip}" ); |
883 | 27 | writer.endElement(); |
884 | 27 | writer.endElement(); |
885 | 27 | writer.endElement(); |
886 | 27 | writer.startElement( "condition" ); |
887 | 27 | writer.addAttribute( "property", "junit.skipped" ); |
888 | 27 | writer.startElement( "or" ); |
889 | 27 | writer.startElement( "isfalse" ); |
890 | 27 | writer.addAttribute( "value", "${junit.present}" ); |
891 | 27 | writer.endElement(); |
892 | 27 | writer.startElement( "istrue" ); |
893 | 27 | writer.addAttribute( "value", "${maven.test.skip}" ); |
894 | 27 | writer.endElement(); |
895 | 27 | writer.endElement(); |
896 | 27 | writer.endElement(); |
897 | 27 | writer.endElement(); |
898 | |
|
899 | 27 | XmlWriterUtil.writeLineBreak( writer, 2, 1 ); |
900 | |
|
901 | 27 | writer.startElement( "target" ); |
902 | 27 | writer.addAttribute( "name", "junit-missing" ); |
903 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "depends", "test-junit-status", 2 ); |
904 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "if", "junit.missing", 2 ); |
905 | |
|
906 | 27 | writer.startElement( "echo" ); |
907 | 27 | writer.writeText( StringUtils.repeat( "=", 35 ) + " WARNING " + StringUtils.repeat( "=", 35 ) ); |
908 | 27 | writer.endElement(); |
909 | |
|
910 | 27 | writer.startElement( "echo" ); |
911 | 27 | writer.writeText( " JUnit is not present in your $ANT_HOME/lib directory. Tests not executed." ); |
912 | 27 | writer.endElement(); |
913 | |
|
914 | 27 | writer.startElement( "echo" ); |
915 | 27 | writer.writeText( StringUtils.repeat( "=", 79 ) ); |
916 | 27 | writer.endElement(); |
917 | |
|
918 | 27 | writer.endElement(); |
919 | |
} |
920 | |
|
921 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
922 | 27 | } |
923 | |
|
924 | |
|
925 | |
|
926 | |
|
927 | |
|
928 | |
|
929 | |
private List getTestIncludes() |
930 | |
throws IOException |
931 | |
{ |
932 | 0 | List includes = getSelectorList( AntBuildWriterUtil.getMavenSurefirePluginOptions( project, "includes", null ) ); |
933 | 0 | if ( includes == null || includes.isEmpty() ) |
934 | |
{ |
935 | 0 | includes = Arrays.asList( new String[] { "**/Test*.java", "**/*Test.java", "**/*TestCase.java" } ); |
936 | |
} |
937 | 0 | return includes; |
938 | |
} |
939 | |
|
940 | |
|
941 | |
|
942 | |
|
943 | |
|
944 | |
|
945 | |
private List getTestExcludes() |
946 | |
throws IOException |
947 | |
{ |
948 | 0 | List excludes = getSelectorList( AntBuildWriterUtil.getMavenSurefirePluginOptions( project, "excludes", null ) ); |
949 | 0 | if ( excludes == null || excludes.isEmpty() ) |
950 | |
{ |
951 | 0 | excludes = Arrays.asList( new String[] { "**/*Abstract*Test.java" } ); |
952 | |
} |
953 | 0 | return excludes; |
954 | |
} |
955 | |
|
956 | |
|
957 | |
|
958 | |
|
959 | |
|
960 | |
|
961 | |
|
962 | |
|
963 | |
|
964 | |
private void writeTestFilesets( XMLWriter writer, List testCompileSourceRoots, List includes, List excludes ) |
965 | |
{ |
966 | 0 | for ( int i = 0; i < testCompileSourceRoots.size(); i++ ) |
967 | |
{ |
968 | 0 | writer.startElement( "fileset" ); |
969 | 0 | writer.addAttribute( "dir", "${maven.build.testDir." + i + "}" ); |
970 | |
|
971 | 0 | AntBuildWriterUtil.writeIncludesExcludes( writer, includes, excludes ); |
972 | 0 | writer.endElement(); |
973 | |
} |
974 | 0 | } |
975 | |
|
976 | |
|
977 | |
|
978 | |
|
979 | |
|
980 | |
|
981 | |
|
982 | |
private void writeJavadocTarget( XMLWriter writer ) |
983 | |
throws IOException |
984 | |
{ |
985 | 27 | XmlWriterUtil.writeCommentText( writer, "Javadoc target", 1 ); |
986 | |
|
987 | 27 | writer.startElement( "target" ); |
988 | 27 | writer.addAttribute( "name", "javadoc" ); |
989 | 27 | writer.addAttribute( "description", "Generates the Javadoc of the application" ); |
990 | |
|
991 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
992 | |
{ |
993 | 0 | if ( project.getModules() != null ) |
994 | |
{ |
995 | 0 | for ( Iterator it = project.getModules().iterator(); it.hasNext(); ) |
996 | |
{ |
997 | 0 | String moduleSubPath = (String) it.next(); |
998 | 0 | AntBuildWriterUtil.writeAntTask( writer, project, moduleSubPath, "javadoc" ); |
999 | |
} |
1000 | |
} |
1001 | |
} |
1002 | |
else |
1003 | |
{ |
1004 | 27 | AntBuildWriterUtil.writeJavadocTask( writer, project, artifactResolverWrapper ); |
1005 | |
} |
1006 | |
|
1007 | 27 | writer.endElement(); |
1008 | |
|
1009 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
1010 | 27 | } |
1011 | |
|
1012 | |
|
1013 | |
|
1014 | |
|
1015 | |
|
1016 | |
|
1017 | |
|
1018 | |
private void writePackageTarget( XMLWriter writer ) |
1019 | |
throws IOException |
1020 | |
{ |
1021 | 27 | String synonym = null; |
1022 | 27 | XmlWriterUtil.writeCommentText( writer, "Package target", 1 ); |
1023 | |
|
1024 | 27 | writer.startElement( "target" ); |
1025 | 27 | writer.addAttribute( "name", "package" ); |
1026 | |
|
1027 | 27 | if ( !AntBuildWriterUtil.isPomPackaging( project ) ) |
1028 | |
{ |
1029 | 27 | writer.addAttribute( "depends", "compile,test" ); |
1030 | |
} |
1031 | 27 | writer.addAttribute( "description", "Package the application" ); |
1032 | |
|
1033 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
1034 | |
{ |
1035 | 0 | if ( project.getModules() != null ) |
1036 | |
{ |
1037 | 0 | for ( Iterator it = project.getModules().iterator(); it.hasNext(); ) |
1038 | |
{ |
1039 | 0 | String moduleSubPath = (String) it.next(); |
1040 | 0 | AntBuildWriterUtil.writeAntTask( writer, project, moduleSubPath, "package" ); |
1041 | |
} |
1042 | |
} |
1043 | |
} |
1044 | |
else |
1045 | |
{ |
1046 | 27 | if ( AntBuildWriterUtil.isJarPackaging( project ) ) |
1047 | |
{ |
1048 | 27 | AntBuildWriterUtil.writeJarTask( writer, project ); |
1049 | 27 | synonym = "jar"; |
1050 | |
} |
1051 | 0 | else if ( AntBuildWriterUtil.isEarPackaging( project ) ) |
1052 | |
{ |
1053 | 0 | AntBuildWriterUtil.writeEarTask( writer, project, artifactResolverWrapper ); |
1054 | 0 | synonym = "ear"; |
1055 | |
} |
1056 | 0 | else if ( AntBuildWriterUtil.isWarPackaging( project ) ) |
1057 | |
{ |
1058 | 0 | AntBuildWriterUtil.writeWarTask( writer, project, artifactResolverWrapper ); |
1059 | 0 | synonym = "war"; |
1060 | |
} |
1061 | |
else |
1062 | |
{ |
1063 | 0 | writer.startElement( "echo" ); |
1064 | 0 | writer.addAttribute( "message", "No Ant task exists for the packaging '" + project.getPackaging() |
1065 | |
+ "'. " + "You could overrided the Ant package target in your build.xml." ); |
1066 | 0 | writer.endElement(); |
1067 | |
} |
1068 | |
} |
1069 | |
|
1070 | 27 | writer.endElement(); |
1071 | |
|
1072 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
1073 | |
|
1074 | 27 | if ( synonym != null ) |
1075 | |
{ |
1076 | 27 | XmlWriterUtil.writeCommentText( writer, |
1077 | |
"A dummy target for the package named after the type it creates", 1 ); |
1078 | 27 | writer.startElement( "target" ); |
1079 | 27 | writer.addAttribute( "name", synonym ); |
1080 | 27 | writer.addAttribute( "depends", "package" ); |
1081 | 27 | writer.addAttribute( "description", "Builds the " + synonym + " for the application" ); |
1082 | 27 | writer.endElement(); |
1083 | |
|
1084 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
1085 | |
} |
1086 | 27 | } |
1087 | |
|
1088 | |
private void writeCompileTasks( XMLWriter writer, String outputDirectory, List compileSourceRoots, |
1089 | |
List resources, String additionalClassesDirectory, boolean isTest ) |
1090 | |
throws IOException |
1091 | |
{ |
1092 | 54 | writer.startElement( "mkdir" ); |
1093 | 54 | writer.addAttribute( "dir", outputDirectory ); |
1094 | 54 | writer.endElement(); |
1095 | |
|
1096 | 54 | if ( !compileSourceRoots.isEmpty() ) |
1097 | |
{ |
1098 | 0 | writer.startElement( "javac" ); |
1099 | 0 | writer.addAttribute( "destdir", outputDirectory ); |
1100 | 0 | Map[] includes = AntBuildWriterUtil.getMavenCompilerPluginOptions( project, "includes", null ); |
1101 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "includes", getCommaSeparatedList( includes, |
1102 | |
"include" ), 3 ); |
1103 | 0 | Map[] excludes = AntBuildWriterUtil.getMavenCompilerPluginOptions( project, "excludes", null ); |
1104 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "excludes", getCommaSeparatedList( excludes, |
1105 | |
"exclude" ), 3 ); |
1106 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "encoding", AntBuildWriterUtil |
1107 | |
.getMavenCompilerPluginBasicOption( project, "encoding", null ), 3 ); |
1108 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "nowarn", AntBuildWriterUtil |
1109 | |
.getMavenCompilerPluginBasicOption( project, "showWarnings", "false" ), 3 ); |
1110 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "debug", AntBuildWriterUtil |
1111 | |
.getMavenCompilerPluginBasicOption( project, "debug", "true" ), 3 ); |
1112 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "optimize", AntBuildWriterUtil |
1113 | |
.getMavenCompilerPluginBasicOption( project, "optimize", "false" ), 3 ); |
1114 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "deprecation", AntBuildWriterUtil |
1115 | |
.getMavenCompilerPluginBasicOption( project, "showDeprecation", "true" ), 3 ); |
1116 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "target", AntBuildWriterUtil |
1117 | |
.getMavenCompilerPluginBasicOption( project, "target", "1.1" ), 3 ); |
1118 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "verbose", AntBuildWriterUtil |
1119 | |
.getMavenCompilerPluginBasicOption( project, "verbose", "false" ), 3 ); |
1120 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "fork", AntBuildWriterUtil |
1121 | |
.getMavenCompilerPluginBasicOption( project, "fork", "false" ), 3 ); |
1122 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "memoryMaximumSize", AntBuildWriterUtil |
1123 | |
.getMavenCompilerPluginBasicOption( project, "meminitial", null ), 3 ); |
1124 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "memoryInitialSize", AntBuildWriterUtil |
1125 | |
.getMavenCompilerPluginBasicOption( project, "maxmem", null ), 3 ); |
1126 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "javac", "source", AntBuildWriterUtil |
1127 | |
.getMavenCompilerPluginBasicOption( project, "source", "1.3" ), 3 ); |
1128 | |
|
1129 | 0 | String[] compileSourceRootsArray = (String[]) compileSourceRoots.toArray( new String[0] ); |
1130 | 0 | for ( int i = 0; i < compileSourceRootsArray.length; i++ ) |
1131 | |
{ |
1132 | 0 | writer.startElement( "src" ); |
1133 | 0 | writer.startElement( "pathelement" ); |
1134 | 0 | if ( isTest ) |
1135 | |
{ |
1136 | 0 | writer.addAttribute( "location", "${maven.build.testDir." + i + "}" ); |
1137 | |
} |
1138 | |
else |
1139 | |
{ |
1140 | 0 | writer.addAttribute( "location", "${maven.build.srcDir." + i + "}" ); |
1141 | |
} |
1142 | 0 | writer.endElement(); |
1143 | 0 | writer.endElement(); |
1144 | |
} |
1145 | |
|
1146 | 0 | if ( additionalClassesDirectory == null ) |
1147 | |
{ |
1148 | 0 | writer.startElement( "classpath" ); |
1149 | 0 | if ( isTest ) |
1150 | |
{ |
1151 | 0 | writer.addAttribute( "refid", "build.test.classpath" ); |
1152 | |
} |
1153 | |
else |
1154 | |
{ |
1155 | 0 | writer.addAttribute( "refid", "build.classpath" ); |
1156 | |
} |
1157 | 0 | writer.endElement(); |
1158 | |
} |
1159 | |
else |
1160 | |
{ |
1161 | 0 | writer.startElement( "classpath" ); |
1162 | 0 | writer.startElement( "path" ); |
1163 | 0 | if ( isTest ) |
1164 | |
{ |
1165 | 0 | writer.addAttribute( "refid", "build.test.classpath" ); |
1166 | |
} |
1167 | |
else |
1168 | |
{ |
1169 | 0 | writer.addAttribute( "refid", "build.classpath" ); |
1170 | |
} |
1171 | 0 | writer.endElement(); |
1172 | 0 | writer.startElement( "pathelement" ); |
1173 | 0 | writer.addAttribute( "location", additionalClassesDirectory ); |
1174 | 0 | writer.endElement(); |
1175 | 0 | writer.endElement(); |
1176 | |
} |
1177 | |
|
1178 | 0 | writer.endElement(); |
1179 | |
} |
1180 | |
|
1181 | 54 | Resource[] array = (Resource[]) resources.toArray( new Resource[0] ); |
1182 | 108 | for ( int i = 0; i < array.length; i++ ) |
1183 | |
{ |
1184 | 54 | Resource resource = array[i]; |
1185 | |
|
1186 | 54 | if ( new File( resource.getDirectory() ).exists() ) |
1187 | |
{ |
1188 | 45 | String outputDir = outputDirectory; |
1189 | 45 | if ( resource.getTargetPath() != null && resource.getTargetPath().length() > 0 ) |
1190 | |
{ |
1191 | 0 | outputDir = outputDir + "/" + resource.getTargetPath(); |
1192 | |
|
1193 | 0 | writer.startElement( "mkdir" ); |
1194 | 0 | writer.addAttribute( "dir", outputDir ); |
1195 | 0 | writer.endElement(); |
1196 | |
} |
1197 | |
|
1198 | 45 | writer.startElement( "copy" ); |
1199 | 45 | writer.addAttribute( "todir", outputDir ); |
1200 | |
|
1201 | 45 | writer.startElement( "fileset" ); |
1202 | 45 | if ( isTest ) |
1203 | |
{ |
1204 | 18 | writer.addAttribute( "dir", "${maven.build.testResourceDir." + i + "}" ); |
1205 | |
} |
1206 | |
else |
1207 | |
{ |
1208 | 27 | writer.addAttribute( "dir", "${maven.build.resourceDir." + i + "}" ); |
1209 | |
} |
1210 | |
|
1211 | 45 | AntBuildWriterUtil.writeIncludesExcludes( writer, resource.getIncludes(), resource.getExcludes() ); |
1212 | |
|
1213 | 45 | writer.endElement(); |
1214 | |
|
1215 | 45 | writer.endElement(); |
1216 | |
} |
1217 | |
} |
1218 | 54 | } |
1219 | |
|
1220 | |
|
1221 | |
|
1222 | |
|
1223 | |
|
1224 | |
|
1225 | |
private void writeGetDepsTarget( XMLWriter writer ) |
1226 | |
{ |
1227 | 27 | if ( AntBuildWriterUtil.isPomPackaging( project ) ) |
1228 | |
{ |
1229 | 0 | return; |
1230 | |
} |
1231 | |
|
1232 | 27 | XmlWriterUtil.writeCommentText( writer, "Download dependencies target", 1 ); |
1233 | |
|
1234 | 27 | writer.startElement( "target" ); |
1235 | 27 | writer.addAttribute( "name", "test-offline" ); |
1236 | |
|
1237 | 27 | writer.startElement( "condition" ); |
1238 | 27 | writer.addAttribute( "property", "maven.mode.offline" ); |
1239 | 27 | writer.startElement( "equals" ); |
1240 | 27 | writer.addAttribute( "arg1", "${maven.settings.offline}" ); |
1241 | 27 | writer.addAttribute( "arg2", "true" ); |
1242 | 27 | writer.endElement(); |
1243 | 27 | writer.endElement(); |
1244 | 27 | writer.endElement(); |
1245 | |
|
1246 | 27 | XmlWriterUtil.writeLineBreak( writer, 2, 1 ); |
1247 | |
|
1248 | 27 | writer.startElement( "target" ); |
1249 | 27 | writer.addAttribute( "name", "get-deps" ); |
1250 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "depends", "test-offline", 2 ); |
1251 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "description", "Download all dependencies", 2 ); |
1252 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "target", "unless", "maven.mode.offline", 2 ); |
1253 | |
|
1254 | 27 | writer.startElement( "mkdir" ); |
1255 | 27 | writer.addAttribute( "dir", "${maven.repo.local}" ); |
1256 | 27 | writer.endElement(); |
1257 | |
|
1258 | 27 | String basedir = project.getBasedir().getAbsolutePath(); |
1259 | |
|
1260 | |
|
1261 | 27 | for ( Iterator i = project.getTestArtifacts().iterator(); i.hasNext(); ) |
1262 | |
{ |
1263 | 27 | Artifact artifact = (Artifact) i.next(); |
1264 | |
|
1265 | |
|
1266 | 27 | String path = artifactResolverWrapper.getLocalArtifactPath( artifact ); |
1267 | |
|
1268 | 27 | if ( !new File( path ).exists() ) |
1269 | |
{ |
1270 | 27 | File parentDirs = new File( path ).getParentFile(); |
1271 | 27 | if ( parentDirs != null ) |
1272 | |
{ |
1273 | 27 | writer.startElement( "mkdir" ); |
1274 | |
|
1275 | 27 | writer.addAttribute( "dir", "${maven.repo.local}/" + parentDirs.getPath().replace( '\\', '/' ) ); |
1276 | 27 | writer.endElement(); |
1277 | |
} |
1278 | |
|
1279 | 27 | for ( Iterator j = project.getRepositories().iterator(); j.hasNext(); ) |
1280 | |
{ |
1281 | 27 | Repository repository = (Repository) j.next(); |
1282 | 27 | String url = repository.getUrl(); |
1283 | |
|
1284 | 27 | String localDir = getProjectRepoDirectory( url, basedir ); |
1285 | 27 | if ( localDir != null ) |
1286 | |
{ |
1287 | 0 | if ( localDir.length() > 0 && !localDir.endsWith( "/" ) ) |
1288 | |
{ |
1289 | 0 | localDir += '/'; |
1290 | |
} |
1291 | |
|
1292 | 0 | writer.startElement( "copy" ); |
1293 | 0 | writer.addAttribute( "file", localDir + path ); |
1294 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "copy", "tofile", "${maven.repo.local}/" + path, 3 ); |
1295 | 0 | AntBuildWriterUtil.addWrapAttribute( writer, "copy", "failonerror", "false", 3 ); |
1296 | 0 | writer.endElement(); |
1297 | |
} |
1298 | |
else |
1299 | |
{ |
1300 | 27 | writer.startElement( "get" ); |
1301 | 27 | writer.addAttribute( "src", url + '/' + path ); |
1302 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "get", "dest", "${maven.repo.local}/" + path, 3 ); |
1303 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "get", "usetimestamp", "false", 3 ); |
1304 | 27 | AntBuildWriterUtil.addWrapAttribute( writer, "get", "ignoreerrors", "true", 3 ); |
1305 | 27 | writer.endElement(); |
1306 | |
} |
1307 | |
} |
1308 | |
} |
1309 | |
} |
1310 | |
|
1311 | 27 | writer.endElement(); |
1312 | |
|
1313 | 27 | XmlWriterUtil.writeLineBreak( writer ); |
1314 | 27 | } |
1315 | |
|
1316 | |
|
1317 | |
|
1318 | |
|
1319 | |
|
1320 | |
|
1321 | |
|
1322 | |
|
1323 | |
|
1324 | |
|
1325 | |
|
1326 | |
static String getProjectRepoDirectory( String repoUrl, String projectDir ) |
1327 | |
{ |
1328 | |
try |
1329 | |
{ |
1330 | |
|
1331 | |
|
1332 | |
|
1333 | |
|
1334 | |
|
1335 | |
|
1336 | 108 | if ( repoUrl.regionMatches( true, 0, "file://", 0, 7 ) ) |
1337 | |
{ |
1338 | 36 | String temp = repoUrl.substring( 7 ); |
1339 | 36 | if ( !temp.startsWith( "/" ) && !temp.regionMatches( true, 0, "localhost/", 0, 10 ) ) |
1340 | |
{ |
1341 | 27 | repoUrl = "file:///" + temp; |
1342 | |
} |
1343 | |
} |
1344 | 108 | String path = FileUtils.toFile( new URL( repoUrl ) ).getPath(); |
1345 | 72 | if ( path.startsWith( projectDir ) ) |
1346 | |
{ |
1347 | 63 | path = path.substring( projectDir.length() ).replace( '\\', '/' ); |
1348 | 63 | if ( path.startsWith( "/" ) ) |
1349 | |
{ |
1350 | 45 | path = path.substring( 1 ); |
1351 | |
} |
1352 | 63 | if ( path.endsWith( "/" ) ) |
1353 | |
{ |
1354 | 0 | path = path.substring( 0, path.length() - 1 ); |
1355 | |
} |
1356 | 63 | return path; |
1357 | |
} |
1358 | |
} |
1359 | 36 | catch ( Exception e ) |
1360 | |
{ |
1361 | |
|
1362 | 9 | } |
1363 | 45 | return null; |
1364 | |
} |
1365 | |
|
1366 | |
|
1367 | |
|
1368 | |
|
1369 | |
|
1370 | |
|
1371 | |
|
1372 | |
|
1373 | |
|
1374 | |
|
1375 | |
|
1376 | |
|
1377 | |
private static void addProperty( Properties properties, String name, String value ) |
1378 | |
{ |
1379 | 414 | properties.put( name, StringUtils.isNotEmpty( value ) ? value : "" ); |
1380 | 414 | } |
1381 | |
|
1382 | |
|
1383 | |
|
1384 | |
|
1385 | |
|
1386 | |
|
1387 | |
private static String getCommaSeparatedList( Map[] includes, String key ) |
1388 | |
{ |
1389 | 0 | if ( ( includes == null ) || ( includes.length == 0 ) ) |
1390 | |
{ |
1391 | 0 | return null; |
1392 | |
} |
1393 | |
|
1394 | 0 | StringBuffer sb = new StringBuffer(); |
1395 | 0 | for ( int i = 0; i < includes.length; i++ ) |
1396 | |
{ |
1397 | 0 | String s = (String) includes[i].get( key ); |
1398 | 0 | if ( StringUtils.isEmpty( s ) ) |
1399 | |
{ |
1400 | 0 | continue; |
1401 | |
} |
1402 | |
|
1403 | 0 | sb.append( s ); |
1404 | |
|
1405 | 0 | if ( i < ( includes.length - 1 ) ) |
1406 | |
{ |
1407 | 0 | sb.append( "," ); |
1408 | |
} |
1409 | |
} |
1410 | |
|
1411 | 0 | if ( sb.length() == 0 ) |
1412 | |
{ |
1413 | 0 | return null; |
1414 | |
} |
1415 | |
|
1416 | 0 | return sb.toString(); |
1417 | |
} |
1418 | |
|
1419 | |
|
1420 | |
|
1421 | |
|
1422 | |
|
1423 | |
|
1424 | |
|
1425 | |
|
1426 | |
|
1427 | |
|
1428 | |
|
1429 | |
|
1430 | |
|
1431 | |
|
1432 | |
|
1433 | |
|
1434 | |
|
1435 | |
private static List getSelectorList( Map[] options ) |
1436 | |
{ |
1437 | 0 | List list = new ArrayList(); |
1438 | 0 | if ( options != null && options.length > 0 ) |
1439 | |
{ |
1440 | 0 | for ( int i = 0; i < options.length; i++ ) |
1441 | |
{ |
1442 | 0 | Map option = options[i]; |
1443 | 0 | list.addAll( option.values() ); |
1444 | |
} |
1445 | |
} |
1446 | 0 | return list; |
1447 | |
} |
1448 | |
|
1449 | |
} |