64
126
private List pluginArtifacts;
67
* The XML for the Ant task. You can add anything you can add
68
* between <target> and </target> in a build.xml.
70
* @parameter expression="${tasks}"
75
* This folder is added to the list of those folders
76
* containing source to be compiled. Use this if your
77
* ant script generates source code.
129
* The local Maven repository
131
* @parameter expression="${localRepository}"
134
protected ArtifactRepository localRepository;
137
* String to prepend to project and dependency property names.
139
* @parameter default-value=""
142
private String propertyPrefix;
145
* The xml tag prefix to use for the built in Ant tasks. This prefix needs to be prepended to each task referenced
146
* in the antrun target config. For example, a prefix of "mvn" means that the attachartifact task is referenced by
147
* "<mvn:attachartifact>" The default value of an empty string means that no prefix is used for the tasks.
149
* @parameter default-value=""
152
private String customTaskPrefix = "";
155
* The name of a property containing the list of all dependency versions. This is used for the removing the versions
156
* from the filenames.
158
* @parameter default-value="maven.project.dependencies.versions"
160
private String versionsPropertyName;
163
* The XML for the Ant task. You can add anything you can add between <target> and </target> in a
166
* @deprecated Use target instead
169
private PlexusConfiguration tasks;
172
* The XML for the Ant target. You can add anything you can add between <target> and </target> in a
178
private PlexusConfiguration target;
181
* This folder is added to the list of those folders containing source to be compiled. Use this if your ant script
182
* generates source code.
79
184
* @parameter expression="${sourceRoot}"
185
* @deprecated Use the build-helper-maven-plugin to bind source directories
81
187
private File sourceRoot;
84
* This folder is added to the list of those folders
85
* containing source to be compiled for testing. Use this if your
190
* This folder is added to the list of those folders containing source to be compiled for testing. Use this if your
86
191
* ant script generates test source code.
88
193
* @parameter expression="${testSourceRoot}"
194
* @deprecated Use the build-helper-maven-plugin to bind test source directories
90
196
private File testSourceRoot;
95
201
public void execute()
96
202
throws MojoExecutionException
98
executeTasks( tasks, project, pluginArtifacts );
204
MavenProject mavenProject = getMavenProject();
208
getLog().warn( "Parameter tasks is deprecated, use target instead" );
212
if ( target == null )
214
getLog().info( "No ant target defined - SKIPPED" );
218
if ( propertyPrefix == null )
225
Project antProject = new Project();
226
File antBuildFile = this.writeTargetToProjectFile( );
227
ProjectHelper.configureProject( antProject, antBuildFile );
230
DefaultLogger antLogger = new DefaultLogger();
231
antLogger.setOutputPrintStream( System.out );
232
antLogger.setErrorPrintStream( System.err );
233
antLogger.setMessageOutputLevel( getLog().isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO );
235
antProject.addBuildListener( antLogger );
236
antProject.setBaseDir( mavenProject.getBasedir() );
238
Path p = new Path( antProject );
239
p.setPath( StringUtils.join( mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator ) );
241
/* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */
242
antProject.addReference( "maven.dependency.classpath", p );
243
antProject.addReference( "maven.compile.classpath", p );
245
p = new Path( antProject );
246
p.setPath( StringUtils.join( mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator ) );
247
antProject.addReference( "maven.runtime.classpath", p );
249
p = new Path( antProject );
250
p.setPath( StringUtils.join( mavenProject.getTestClasspathElements().iterator(), File.pathSeparator ) );
251
antProject.addReference( "maven.test.classpath", p );
253
/* set maven.plugin.classpath with plugin dependencies */
254
antProject.addReference( "maven.plugin.classpath", getPathFromArtifacts( pluginArtifacts, antProject ) );
256
antProject.addReference( DEFAULT_MAVEN_PROJECT_REFID, getMavenProject() );
257
antProject.addReference( DEFAULT_MAVEN_PROJECT_HELPER_REFID, projectHelper );
258
antProject.addReference( "maven.local.repository", localRepository );
259
initMavenTasks( antProject );
261
// The ant project needs actual properties vs. using expression evaluator when calling an external build
263
copyProperties( mavenProject, antProject );
265
if ( getLog().isInfoEnabled() )
267
getLog().info( "Executing tasks" );
270
antProject.executeTarget( antTargetName );
272
if ( getLog().isInfoEnabled() )
274
getLog().info( "Executed tasks" );
277
catch ( DependencyResolutionRequiredException e )
279
throw new MojoExecutionException( "DependencyResolutionRequiredException: " + e.getMessage(), e );
281
catch ( BuildException e )
283
throw new MojoExecutionException( "An Ant BuildException has occured: " + e.getMessage(), e );
285
catch ( Exception e )
287
throw new MojoExecutionException( "Error executing ant tasks: " + e.getMessage(), e );
100
290
if ( sourceRoot != null )
102
292
getLog().info( "Registering compile source root " + sourceRoot );
103
project.addCompileSourceRoot( sourceRoot.toString() );
293
getMavenProject().addCompileSourceRoot( sourceRoot.toString() );
106
296
if ( testSourceRoot != null )
108
298
getLog().info( "Registering compile test source root " + testSourceRoot );
109
project.addTestCompileSourceRoot( testSourceRoot.toString() );
299
getMavenProject().addTestCompileSourceRoot( testSourceRoot.toString() );
307
* @throws DependencyResolutionRequiredException
309
public Path getPathFromArtifacts( Collection artifacts, Project antProject )
310
throws DependencyResolutionRequiredException
312
if ( artifacts == null )
314
return new Path( antProject );
317
List list = new ArrayList( artifacts.size() );
318
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
320
Artifact a = (Artifact) i.next();
321
File file = a.getFile();
324
throw new DependencyResolutionRequiredException( a );
326
list.add( file.getPath() );
329
Path p = new Path( antProject );
330
p.setPath( StringUtils.join( list.iterator(), File.pathSeparator ) );
336
* Copy properties from the maven project to the ant project.
338
* @param mavenProject
341
public void copyProperties( MavenProject mavenProject, Project antProject )
343
Properties mavenProps = mavenProject.getProperties();
344
Iterator iter = mavenProps.keySet().iterator();
345
while ( iter.hasNext() )
347
String key = (String) iter.next();
348
antProject.setProperty( key, mavenProps.getProperty( key ) );
351
// Set the POM file as the ant.file for the tasks run directly in Maven.
352
antProject.setProperty( "ant.file", mavenProject.getFile().getAbsolutePath() );
354
// Add some of the common maven properties
355
getLog().debug("Setting properties with prefix: " + propertyPrefix );
356
antProject.setProperty( ( propertyPrefix + "project.groupId" ), mavenProject.getGroupId() );
357
antProject.setProperty( ( propertyPrefix + "project.artifactId" ), mavenProject.getArtifactId() );
358
antProject.setProperty( ( propertyPrefix + "project.name" ), mavenProject.getName() );
359
antProject.setProperty( ( propertyPrefix + "project.description" ), mavenProject.getDescription() );
360
antProject.setProperty( ( propertyPrefix + "project.version" ), mavenProject.getVersion() );
361
antProject.setProperty( ( propertyPrefix + "project.packaging" ), mavenProject.getPackaging() );
362
antProject.setProperty( ( propertyPrefix + "project.build.directory" ), mavenProject.getBuild().getDirectory() );
363
antProject.setProperty( ( propertyPrefix + "project.build.outputDirectory" ),
364
mavenProject.getBuild().getOutputDirectory() );
365
antProject.setProperty( ( propertyPrefix + "project.build.testOutputDirectory" ),
366
mavenProject.getBuild().getTestOutputDirectory() );
367
antProject.setProperty( ( propertyPrefix + "project.build.sourceDirectory" ),
368
mavenProject.getBuild().getSourceDirectory() );
369
antProject.setProperty( ( propertyPrefix + "project.build.testSourceDirectory" ),
370
mavenProject.getBuild().getTestSourceDirectory() );
371
antProject.setProperty( ( propertyPrefix + "localRepository" ), localRepository.toString() );
372
antProject.setProperty( ( propertyPrefix + "settings.localRepository" ), localRepository.getBasedir() );
374
// Add properties for depenedency artifacts
375
Set depArtifacts = mavenProject.getArtifacts();
376
for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
378
Artifact artifact = (Artifact) it.next();
380
String propName = artifact.getDependencyConflictId();
382
antProject.setProperty( propertyPrefix + propName, artifact.getFile().getPath() );
385
// Add a property containing the list of versions for the mapper
386
StringBuffer versionsBuffer = new StringBuffer();
387
for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
389
Artifact artifact = (Artifact) it.next();
391
versionsBuffer.append( artifact.getVersion() + File.pathSeparator );
393
antProject.setProperty( versionsPropertyName, versionsBuffer.toString() );
395
// Add properties in deprecated format to depenedency artifacts
396
// This should be removed in future versions of the antrun plugin.
397
for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
399
Artifact artifact = (Artifact) it.next();
401
String propName = getDependencyArtifactPropertyName( artifact );
403
antProject.setProperty( propName, artifact.getFile().getPath() );
408
* Prefix for legacy property format.
409
* @deprecated This should only be used for generating the old property format.
411
public static final String DEPENDENCY_PREFIX = "maven.dependency.";
414
* Returns a property name for a dependency artifact. The name is in the format
415
* maven.dependency.groupId.artifactId[.classifier].type.path
418
* @return property name
419
* @deprecated The dependency conflict ID should be used as the property name.
421
public static String getDependencyArtifactPropertyName( Artifact artifact )
423
String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
424
+ ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
425
+ ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
430
* Get the current Maven project
432
* @return current Maven project
434
public MavenProject getMavenProject()
439
public void initMavenTasks( Project antProject )
441
getLog().debug( "Initialize Maven Ant Tasks" );
442
Typedef typedef = new Typedef();
443
typedef.setProject( antProject );
444
typedef.setResource( ANTLIB );
445
if ( ! customTaskPrefix.equals( "" ) )
447
typedef.setURI( TASK_URI );
453
* Write the ant target and surrounding tags to a temporary file
455
* @throws PlexusConfigurationException
457
private File writeTargetToProjectFile()
458
throws IOException, PlexusConfigurationException
460
// Have to use an XML writer because in Maven 2.x the PlexusConfig toString() method loses XML attributes
461
StringWriter writer = new StringWriter();
462
AntrunXmlPlexusConfigurationWriter xmlWriter = new AntrunXmlPlexusConfigurationWriter();
463
xmlWriter.write( target, writer );
465
StringBuffer antProjectConfig = writer.getBuffer();
467
// replace deprecated tasks tag with standard Ant target
468
stringReplace( antProjectConfig, "<tasks", "<target" );
469
stringReplace( antProjectConfig, "</tasks", "</target" );
471
antTargetName = target.getAttribute( "name" );
473
if ( antTargetName == null )
475
antTargetName = DEFAULT_ANT_TARGET_NAME;
476
stringReplace( antProjectConfig, "<target", "<target name=\"" + antTargetName + "\"" );
480
if ( ! customTaskPrefix.trim().equals( "" ) )
482
xmlns = "xmlns:" + customTaskPrefix + "=\"" + TASK_URI + "\"";
485
final String xmlHeader = "<?xml version=\"1.0\" encoding=\"" + UTF_8 + "\" ?>\n";
486
antProjectConfig.insert( 0, xmlHeader );
487
final String projectOpen = "<project name=\"maven-antrun-\" default=\"" + antTargetName + "\" " + xmlns +" >\n";
488
int index = antProjectConfig.indexOf( "<target" );
489
antProjectConfig.insert( index, projectOpen );
491
final String projectClose = "\n</project>";
492
antProjectConfig.append( projectClose );
494
// The fileName should probably use the plugin executionId instead of the targetName
495
String fileName = "build-" + antTargetName + ".xml";
496
File buildFile = new File( project.getBuild().getDirectory(), "/antrun/" + fileName );
498
buildFile.getParentFile().mkdirs();
499
FileUtils.fileWrite( buildFile.getAbsolutePath(), UTF_8, antProjectConfig.toString() );
504
* Replace text in a StringBuffer. If the match text is not found, the StringBuffer
505
* is returned unchanged.
507
* @param text The string buffer containing the text
508
* @param match The string to match and remove
509
* @param with The string to insert
511
public void stringReplace( StringBuffer text, String match, String with )
513
int index = text.indexOf( match );
516
text.replace( index, index + match.length(), with );
520
public String checkTargetName( PlexusConfiguration antTargetConfig )
521
throws PlexusConfigurationException
523
String targetName = antTargetConfig.getAttribute( "name" );
524
if ( targetName == null )
526
targetName = DEFAULT_ANT_TARGET_NAME;