5050import org .apache .tools .ant .taskdefs .Typedef ;
5151import org .apache .tools .ant .types .Path ;
5252import org .codehaus .plexus .configuration .PlexusConfiguration ;
53- import org .codehaus .plexus .configuration .PlexusConfigurationException ;
5453import org .codehaus .plexus .util .IOUtil ;
5554import org .codehaus .plexus .util .ReaderFactory ;
5655import org .codehaus .plexus .util .StringUtils ;
5756
5857/**
58+ * <p>
5959 * Maven AntRun Mojo.
6060 * <p>
6161 * This plugin provides the capability of calling Ant tasks from a POM by running the nested Ant tasks inside the
@@ -120,7 +120,7 @@ public class AntRunMojo
120120 * The Maven project object
121121 */
122122 @ Parameter ( defaultValue = "${project}" , readonly = true , required = true )
123- private MavenProject project ;
123+ private MavenProject mavenProject ;
124124
125125 /**
126126 * The Maven session object
@@ -230,16 +230,16 @@ public class AntRunMojo
230230 private boolean exportAntProperties ;
231231
232232 /**
233- * Specifies whether a failure in the Ant build leads to a failure of the Maven build. If this
234- * value is {@code false}, the Maven build will proceed even if the Ant build fails. If it is
235- * {@code true}, then the Maven build fails if the Ant build fails.
233+ * Specifies whether a failure in the Ant build leads to a failure of the Maven build. If this value is
234+ * {@code false}, the Maven build will proceed even if the Ant build fails. If it is {@code true}, then the Maven
235+ * build fails if the Ant build fails.
236236 *
237237 * @since 1.7
238238 */
239239 @ Parameter ( defaultValue = "true" )
240240 private boolean failOnError ;
241241
242- /** {@inheritDoc} */
242+ @ Override
243243 public void execute ()
244244 throws MojoExecutionException , MojoFailureException
245245 {
@@ -252,8 +252,6 @@ public void execute()
252252 return ;
253253 }
254254
255- MavenProject mavenProject = getMavenProject ();
256-
257255 if ( target == null )
258256 {
259257 getLog ().info ( "No Ant target defined - SKIPPED" );
@@ -268,86 +266,29 @@ public void execute()
268266 String antTargetName = target .getAttribute ( "name" , DEFAULT_ANT_TARGET_NAME );
269267 target .setAttribute ( "name" , antTargetName );
270268
269+ Project antProject = new Project ();
270+ antProject .addBuildListener ( getConfiguredBuildLogger () );
271271 try
272272 {
273- Project antProject = new Project ();
274273 File antBuildFile = writeTargetToProjectFile ( antTargetName );
275274 ProjectHelper .configureProject ( antProject , antBuildFile );
276275 antProject .init ();
277276
278- DefaultLogger antLogger = new MavenLogger ( getLog () );
279-
280- if ( getLog ().isDebugEnabled () )
281- {
282- antLogger .setMessageOutputLevel ( Project .MSG_DEBUG );
283- }
284- else if ( getLog ().isInfoEnabled () )
285- {
286- antLogger .setMessageOutputLevel ( Project .MSG_INFO );
287- }
288- else if ( getLog ().isWarnEnabled () )
289- {
290- antLogger .setMessageOutputLevel ( Project .MSG_WARN );
291- }
292- else if ( getLog ().isErrorEnabled () )
293- {
294- antLogger .setMessageOutputLevel ( Project .MSG_ERR );
295- }
296- else
297- {
298- antLogger .setMessageOutputLevel ( Project .MSG_VERBOSE );
299- }
300-
301- antProject .addBuildListener ( antLogger );
302277 antProject .setBaseDir ( mavenProject .getBasedir () );
303278
304- Path p = new Path ( antProject );
305- p .setPath ( StringUtils .join ( mavenProject .getCompileClasspathElements ().iterator (), File .pathSeparator ) );
306-
307- /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */
308- antProject .addReference ( MAVEN_REFID_PREFIX + "dependency.classpath" , p );
309- antProject .addReference ( MAVEN_REFID_PREFIX + "compile.classpath" , p );
310-
311- p = new Path ( antProject );
312- p .setPath ( StringUtils .join ( mavenProject .getRuntimeClasspathElements ().iterator (), File .pathSeparator ) );
313- antProject .addReference ( MAVEN_REFID_PREFIX + "runtime.classpath" , p );
314-
315- p = new Path ( antProject );
316- p .setPath ( StringUtils .join ( mavenProject .getTestClasspathElements ().iterator (), File .pathSeparator ) );
317- antProject .addReference ( MAVEN_REFID_PREFIX + "test.classpath" , p );
318-
319- /* set maven.plugin.classpath with plugin dependencies */
320- antProject .addReference ( MAVEN_REFID_PREFIX + "plugin.classpath" ,
321- getPathFromArtifacts ( pluginArtifacts , antProject ) );
322-
323- antProject .addReference ( DEFAULT_MAVEN_PROJECT_REFID , mavenProject );
324- antProject .addReference ( DEFAULT_MAVEN_PROJECT_REF_REFID , new MavenAntRunProject ( mavenProject ) );
325- antProject .addReference ( DEFAULT_MAVEN_PROJECT_HELPER_REFID , projectHelper );
326- antProject .addReference ( MAVEN_REFID_PREFIX + "local.repository" , localRepository );
279+ addAntProjectReferences ( mavenProject , antProject );
327280 initMavenTasks ( antProject );
328281
329282 // The Ant project needs actual properties vs. using expression evaluator when calling an external build
330283 // file.
331284 copyProperties ( mavenProject , antProject );
332285
333- if ( getLog ().isInfoEnabled () )
334- {
335- getLog ().info ( "Executing tasks" );
336- }
337-
286+ getLog ().info ( "Executing tasks" );
338287 antProject .executeTarget ( antTargetName );
339-
340- if ( getLog ().isInfoEnabled () )
341- {
342- getLog ().info ( "Executed tasks" );
343- }
288+ getLog ().info ( "Executed tasks" );
344289
345290 copyProperties ( antProject , mavenProject );
346291 }
347- catch ( DependencyResolutionRequiredException e )
348- {
349- throw new MojoExecutionException ( "DependencyResolutionRequiredException: " + e .getMessage (), e );
350- }
351292 catch ( BuildException e )
352293 {
353294 StringBuilder sb = new StringBuilder ();
@@ -384,14 +325,67 @@ private void checkDeprecatedParameterUsage( Object parameter, String name, Strin
384325 }
385326 }
386327
328+ private DefaultLogger getConfiguredBuildLogger ()
329+ {
330+ DefaultLogger antLogger = new MavenLogger ( getLog () );
331+ if ( getLog ().isDebugEnabled () )
332+ {
333+ antLogger .setMessageOutputLevel ( Project .MSG_DEBUG );
334+ }
335+ else if ( getLog ().isInfoEnabled () )
336+ {
337+ antLogger .setMessageOutputLevel ( Project .MSG_INFO );
338+ }
339+ else if ( getLog ().isWarnEnabled () )
340+ {
341+ antLogger .setMessageOutputLevel ( Project .MSG_WARN );
342+ }
343+ else if ( getLog ().isErrorEnabled () )
344+ {
345+ antLogger .setMessageOutputLevel ( Project .MSG_ERR );
346+ }
347+ else
348+ {
349+ antLogger .setMessageOutputLevel ( Project .MSG_VERBOSE );
350+ }
351+ return antLogger ;
352+ }
353+
354+ private void addAntProjectReferences ( MavenProject mavenProject , Project antProject )
355+ throws DependencyResolutionRequiredException
356+ {
357+ Path p = new Path ( antProject );
358+ p .setPath ( StringUtils .join ( mavenProject .getCompileClasspathElements ().iterator (), File .pathSeparator ) );
359+
360+ /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */
361+ antProject .addReference ( MAVEN_REFID_PREFIX + "dependency.classpath" , p );
362+ antProject .addReference ( MAVEN_REFID_PREFIX + "compile.classpath" , p );
363+
364+ p = new Path ( antProject );
365+ p .setPath ( StringUtils .join ( mavenProject .getRuntimeClasspathElements ().iterator (), File .pathSeparator ) );
366+ antProject .addReference ( MAVEN_REFID_PREFIX + "runtime.classpath" , p );
367+
368+ p = new Path ( antProject );
369+ p .setPath ( StringUtils .join ( mavenProject .getTestClasspathElements ().iterator (), File .pathSeparator ) );
370+ antProject .addReference ( MAVEN_REFID_PREFIX + "test.classpath" , p );
371+
372+ /* set maven.plugin.classpath with plugin dependencies */
373+ antProject .addReference ( MAVEN_REFID_PREFIX + "plugin.classpath" ,
374+ getPathFromArtifacts ( pluginArtifacts , antProject ) );
375+
376+ antProject .addReference ( DEFAULT_MAVEN_PROJECT_REFID , mavenProject );
377+ antProject .addReference ( DEFAULT_MAVEN_PROJECT_REF_REFID , new MavenAntRunProject ( mavenProject ) );
378+ antProject .addReference ( DEFAULT_MAVEN_PROJECT_HELPER_REFID , projectHelper );
379+ antProject .addReference ( MAVEN_REFID_PREFIX + "local.repository" , localRepository );
380+ }
381+
387382 /**
388383 * @param artifacts {@link Artifact} collection.
389384 * @param antProject {@link Project}
390385 * @return {@link Path}
391386 * @throws DependencyResolutionRequiredException In case of a failure.
392- *
393387 */
394- public Path getPathFromArtifacts ( Collection <Artifact > artifacts , Project antProject )
388+ private Path getPathFromArtifacts ( Collection <Artifact > artifacts , Project antProject )
395389 throws DependencyResolutionRequiredException
396390 {
397391 if ( artifacts == null )
@@ -459,7 +453,7 @@ public void copyProperties( MavenProject mavenProject, Project antProject )
459453 antProject .setProperty ( ( propertyPrefix + "localRepository" ), localRepository .toString () );
460454 antProject .setProperty ( ( propertyPrefix + "settings.localRepository" ), localRepository .getBasedir () );
461455
462- // Add properties for depenedency artifacts
456+ // Add properties for dependency artifacts
463457 Set <Artifact > depArtifacts = mavenProject .getArtifacts ();
464458 for ( Artifact artifact : depArtifacts )
465459 {
@@ -480,7 +474,7 @@ public void copyProperties( MavenProject mavenProject, Project antProject )
480474 /**
481475 * Copy properties from the Ant project to the Maven project.
482476 *
483- * @param antProject not null
477+ * @param antProject not null
484478 * @param mavenProject not null
485479 * @since 1.7
486480 */
@@ -501,25 +495,14 @@ public void copyProperties( Project antProject, MavenProject mavenProject )
501495 if ( mavenProperties .getProperty ( key ) != null )
502496 {
503497 getLog ().debug ( "Ant property '" + key + "=" + mavenProperties .getProperty ( key )
504- + "' clashs with an existing Maven property, "
505- + "SKIPPING this Ant property propagation." );
498+ + "' clashs with an existing Maven property, SKIPPING this Ant property propagation." );
506499 continue ;
507500 }
508501 // it is safe to call toString directly since the value cannot be null in Hashtable
509502 mavenProperties .setProperty ( key , entry .getValue ().toString () );
510503 }
511504 }
512505
513- /**
514- * Get the current Maven project
515- *
516- * @return current Maven project
517- */
518- public MavenProject getMavenProject ()
519- {
520- return this .project ;
521- }
522-
523506 /**
524507 * @param antProject {@link Project}
525508 */
@@ -545,7 +528,7 @@ private File writeTargetToProjectFile( String targetName )
545528 throws IOException
546529 {
547530 // The fileName should probably use the plugin executionId instead of the targetName
548- File buildFile = new File ( project .getBuild ().getDirectory (), "antrun/build-" + targetName + ".xml" );
531+ File buildFile = new File ( mavenProject .getBuild ().getDirectory (), "antrun/build-" + targetName + ".xml" );
549532 // noinspection ResultOfMethodCallIgnored
550533 buildFile .getParentFile ().mkdirs ();
551534
@@ -555,38 +538,6 @@ private File writeTargetToProjectFile( String targetName )
555538 return buildFile ;
556539 }
557540
558- /**
559- * Replace text in a StringBuilder. If the match text is not found, the StringBuilder is returned unchanged.
560- *
561- * @param text The string buffer containing the text
562- * @param match The string to match and remove
563- * @param with The string to insert
564- */
565- public void stringReplace ( StringBuilder text , String match , String with )
566- {
567- int index = text .indexOf ( match );
568- if ( index != -1 )
569- {
570- text .replace ( index , index + match .length (), with );
571- }
572- }
573-
574- /**
575- * @param antTargetConfig {@link PlexusConfiguration}
576- * @return The target name.
577- * @throws PlexusConfigurationException in case of not existing attribute.
578- */
579- public String checkTargetName ( PlexusConfiguration antTargetConfig )
580- throws PlexusConfigurationException
581- {
582- String targetName = antTargetConfig .getAttribute ( "name" );
583- if ( targetName == null )
584- {
585- targetName = DEFAULT_ANT_TARGET_NAME ;
586- }
587- return targetName ;
588- }
589-
590541 /**
591542 * @param buildException not null
592543 * @return the fragment XML part where the buildException occurs.
@@ -616,8 +567,7 @@ private String findFragment( BuildException buildException )
616567 if ( reader .getLineNumber () == buildException .getLocation ().getLineNumber () )
617568 {
618569 return "around Ant part ..." + line .trim () + "... @ " + buildException .getLocation ().getLineNumber ()
619- + ":" + buildException .getLocation ().getColumnNumber () + " in "
620- + antFile .getAbsolutePath ();
570+ + ":" + buildException .getLocation ().getColumnNumber () + " in " + antFile .getAbsolutePath ();
621571
622572 }
623573 }
0 commit comments