Author: pgier
Date: Wed Jun  2 20:47:53 2010
New Revision: 950756

URL: http://svn.apache.org/viewvc?rev=950756&view=rev
Log:
[MANTRUN-141] Merge the AbstractAntMojo with AntRunMojo

Removed:
    
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java
Modified:
    
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java
    
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java
    
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntRunMojo.java

Modified: 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java?rev=950756&r1=950755&r2=950756&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java
 (original)
+++ 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java
 Wed Jun  2 20:47:53 2010
@@ -2,7 +2,7 @@ package org.apache.maven.ant.tasks;
 
 import java.io.File;
 
-import org.apache.maven.plugin.antrun.AbstractAntMojo;
+import org.apache.maven.plugin.antrun.AntRunMojo;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
 import org.apache.tools.ant.BuildException;
@@ -36,12 +36,12 @@ public class AttachArtifactTask
     /**
      * The refId of the maven project.
      */
-    private String mavenProjectRefId = 
AbstractAntMojo.DEFAULT_MAVEN_PROJECT_REFID;
+    private String mavenProjectRefId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
 
     /**
      * The refId of the maven project helper component.
      */
-    private String mavenProjectHelperRefId = 
AbstractAntMojo.DEFAULT_MAVEN_PROJECT_HELPER_REFID;
+    private String mavenProjectHelperRefId = 
AntRunMojo.DEFAULT_MAVEN_PROJECT_HELPER_REFID;
 
     /**
      * The file to attach.

Modified: 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java?rev=950756&r1=950755&r2=950756&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java
 (original)
+++ 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java
 Wed Jun  2 20:47:53 2010
@@ -29,7 +29,7 @@ import org.apache.maven.ant.tasks.suppor
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
-import org.apache.maven.plugin.antrun.AbstractAntMojo;
+import org.apache.maven.plugin.antrun.AntRunMojo;
 import org.apache.maven.project.MavenProject;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
@@ -50,7 +50,7 @@ public class DependencyFilesetsTask
     /**
      * The project ref Id of the project being used.
      */
-    private String mavenProjectId = 
AbstractAntMojo.DEFAULT_MAVEN_PROJECT_REFID;
+    private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID;
 
     /**
      * The id to store the dependencies fileset.

Modified: 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntRunMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntRunMojo.java?rev=950756&r1=950755&r2=950756&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntRunMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntRunMojo.java
 Wed Jun  2 20:47:53 2010
@@ -20,9 +20,31 @@ package org.apache.maven.plugin.antrun;
  */
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
 
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.antrun.components.AntTargetConverter;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectHelper;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DefaultLogger;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.PropertyHelper;
 import org.apache.tools.ant.Target;
+import org.apache.tools.ant.taskdefs.Typedef;
+import org.apache.tools.ant.types.Path;
+import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.util.StringUtils;
+
 
 /**
  * Maven AntRun Mojo.
@@ -42,9 +64,81 @@ import org.apache.tools.ant.Target;
  * @requiresDependencyResolution test
  */
 public class AntRunMojo
-    extends AbstractAntMojo
+    extends AbstractMojo
 {
     /**
+     * The refid used to store the Maven project object in the Ant build.
+     */
+    public final static String DEFAULT_MAVEN_PROJECT_REFID = "maven.project";
+
+    /**
+     * The refid used to store the Maven project object in the Ant build.
+     */
+    public final static String DEFAULT_MAVEN_PROJECT_HELPER_REFID = 
"maven.project.helper";
+
+    /**
+     * The path to The XML file containing the definition of the Maven tasks.
+     */
+    public final static String ANTLIB = 
"org/apache/maven/ant/tasks/antlib.xml";
+
+    /**
+     * The URI which defines the built in Ant tasks
+     */
+    public final static String TASK_URI = "antlib:org.apache.maven.ant.tasks";
+    
+    /**
+     * The Maven project object
+     *
+     * @parameter expression="${project}"
+     * @readonly
+     */
+    private MavenProject project;
+
+    /**
+     * The Maven project helper object
+     *
+     * @component
+     */
+    private MavenProjectHelper projectHelper;
+
+    /**
+     * The plugin dependencies.
+     *
+     * @parameter expression="${plugin.artifacts}"
+     * @required
+     * @readonly
+     */
+    private List pluginArtifacts;
+
+    /**
+     * The local Maven repository
+     * 
+     * @parameter expression="${localRepository}"
+     * @readonly
+     */
+    protected ArtifactRepository localRepository;
+
+    /**
+     * String to prepend to project and dependency property names.
+     * @parameter default-value=""
+     */
+    private String propertyPrefix;
+    
+    /**
+     * The xml namespace to use for the built in Ant tasks.
+     * @parameter default-value="mvn"
+     */
+    private String taskNamespace;
+    
+    /**
+     * The name of a property containing the list of all dependency versions.
+     * This is used for the removing the versions from the filenames.
+     * 
+     * @parameter default-value="maven.project.dependencies.versions"
+     */
+    private String versionsPropertyName;
+    
+    /**
      * The XML for the Ant task. You can add anything you can add
      * between <target> and </target> in a build.xml.
      *
@@ -78,7 +172,88 @@ public class AntRunMojo
     public void execute()
         throws MojoExecutionException
     {
-        executeTasks( tasks );
+        MavenProject mavenProject = getMavenProject();
+        
+        if ( tasks == null )
+        {
+            getLog().info( "No ant tasks defined - SKIPPED" );
+            return;
+        }
+        
+        if ( propertyPrefix == null )
+        {
+            propertyPrefix = "";
+        }
+
+        try
+        {
+            //TODO refactor - place the manipulation of the 
expressionEvaluator into a separated class.
+            ExpressionEvaluator exprEvaluator = (ExpressionEvaluator) 
tasks.getProject()
+                .getReference( 
AntTargetConverter.MAVEN_EXPRESSION_EVALUATOR_ID );
+
+            Project antProject = tasks.getProject();
+
+            PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper( 
antProject );
+            propertyHelper.setNext( new AntPropertyHelper( exprEvaluator, 
mavenProject.getArtifacts(), getLog() ) );
+
+            DefaultLogger antLogger = new DefaultLogger();
+            antLogger.setOutputPrintStream( System.out );
+            antLogger.setErrorPrintStream( System.err );
+            antLogger.setMessageOutputLevel( getLog().isDebugEnabled() ? 
Project.MSG_DEBUG : Project.MSG_INFO );
+
+            antProject.addBuildListener( antLogger );
+            antProject.setBaseDir( mavenProject.getBasedir() );
+
+            Path p = new Path( antProject );
+            p.setPath( StringUtils.join( 
mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator ) );
+
+            /* maven.dependency.classpath it's deprecated as it's equal to 
maven.compile.classpath */
+            antProject.addReference( "maven.dependency.classpath", p );
+            antProject.addReference( "maven.compile.classpath", p );
+
+            p = new Path( antProject );
+            p.setPath( StringUtils.join( 
mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator ) );
+            antProject.addReference( "maven.runtime.classpath", p );
+
+            p = new Path( antProject );
+            p.setPath( StringUtils.join( 
mavenProject.getTestClasspathElements().iterator(), File.pathSeparator ) );
+            antProject.addReference( "maven.test.classpath", p );
+
+            /* set maven.plugin.classpath with plugin dependencies */
+            antProject.addReference( "maven.plugin.classpath", 
getPathFromArtifacts( pluginArtifacts, antProject ) );
+            
+            antProject.addReference( DEFAULT_MAVEN_PROJECT_REFID, 
getMavenProject() );
+            antProject.addReference( DEFAULT_MAVEN_PROJECT_HELPER_REFID, 
projectHelper );
+            antProject.addReference( "maven.local.repository", localRepository 
);
+            initMavenTasks( antProject );
+            
+            // The ant project needs actual properties vs. using expression 
evaluator when calling an external build file.
+            copyProperties( mavenProject, antProject );
+
+            if ( getLog().isInfoEnabled() )
+            {
+                getLog().info( "Executing tasks" );
+            }
+
+            tasks.execute();
+
+            if ( getLog().isInfoEnabled() )
+            {
+                getLog().info( "Executed tasks" );
+            }
+        }
+        catch ( DependencyResolutionRequiredException e )
+        {
+            throw new MojoExecutionException( 
"DependencyResolutionRequiredException: " + e.getMessage(), e );
+        }
+        catch ( BuildException e )
+        {
+            throw new MojoExecutionException( "An Ant BuildException has 
occured: " + e.getMessage(), e );
+        }
+        catch ( Exception e )
+        {
+            throw new MojoExecutionException( "Error executing ant tasks: " + 
e.getMessage(), e );
+        }
 
         if ( sourceRoot != null )
         {
@@ -93,4 +268,120 @@ public class AntRunMojo
         }
     }
     
+    /**
+     * @param artifacts
+     * @param antProject
+     * @return a path
+     * @throws DependencyResolutionRequiredException
+     */
+    public Path getPathFromArtifacts( Collection artifacts, Project antProject 
)
+        throws DependencyResolutionRequiredException
+    {
+        if ( artifacts == null )
+        {
+            return new Path( antProject );
+        }
+
+        List list = new ArrayList( artifacts.size() );
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            File file = a.getFile();
+            if ( file == null )
+            {
+                throw new DependencyResolutionRequiredException( a );
+            }
+            list.add( file.getPath() );
+        }
+
+        Path p = new Path( antProject );
+        p.setPath( StringUtils.join( list.iterator(), File.pathSeparator ) );
+
+        return p;
+    }
+
+    /**
+     * Copy properties from the maven project to the ant project.
+     * @param mavenProject
+     * @param antProject
+     */
+    public void copyProperties( MavenProject mavenProject, Project antProject )
+    {
+        Properties mavenProps = mavenProject.getProperties();
+        Iterator iter = mavenProps.keySet().iterator();
+        while ( iter.hasNext() )
+        {
+            String key = (String)iter.next();
+            antProject.setProperty( key, mavenProps.getProperty( key ) );
+        }
+        
+        // Set the POM file as the ant.file for the tasks run directly in 
Maven.
+        antProject.setProperty( "ant.file", 
mavenProject.getFile().getAbsolutePath() );
+        
+        // Add some of the common maven properties
+        System.out.println( (propertyPrefix+ "project.artifactId"));
+        antProject.setProperty( ( propertyPrefix + "project.groupId" ), 
mavenProject.getGroupId() );
+        antProject.setProperty( ( propertyPrefix + "project.artifactId" ), 
mavenProject.getArtifactId() );
+        antProject.setProperty( ( propertyPrefix + "project.name" ), 
mavenProject.getName() );
+        antProject.setProperty( ( propertyPrefix + "project.description" ), 
mavenProject.getDescription() );
+        antProject.setProperty( ( propertyPrefix + "project.version" ), 
mavenProject.getVersion() );
+        antProject.setProperty( ( propertyPrefix + "project.packaging" ), 
mavenProject.getPackaging() );
+        antProject.setProperty( ( propertyPrefix + "project.build.directory" 
), mavenProject.getBuild().getDirectory() );
+        antProject.setProperty( ( propertyPrefix + 
"project.build.outputDirectory" ), mavenProject.getBuild().getOutputDirectory() 
);
+        antProject.setProperty( ( propertyPrefix + 
"project.build.testOutputDirectory" ), 
mavenProject.getBuild().getTestOutputDirectory() );
+        antProject.setProperty( ( propertyPrefix + 
"project.build.sourceDirectory" ), mavenProject.getBuild().getSourceDirectory() 
);
+        antProject.setProperty( ( propertyPrefix + 
"project.build.testSourceDirectory" ), 
mavenProject.getBuild().getTestSourceDirectory() );
+        
+        // Add properties for depenedency artifacts
+        Set depArtifacts = mavenProject.getArtifacts();
+        for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+
+            String propName = artifact.getDependencyConflictId();
+
+            antProject.setProperty( propertyPrefix + propName, 
artifact.getFile().getPath() );
+        }
+        
+        // Add a property containing the list of versions for the mapper
+        StringBuffer versionsBuffer = new StringBuffer();
+        for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+
+            versionsBuffer.append( artifact.getVersion() + File.pathSeparator 
);
+        }
+        antProject.setProperty( versionsPropertyName, 
versionsBuffer.toString() );
+        
+        
+        // Add properties in deprecated format to depenedency artifacts
+        // This should be removed in future versions of the antrun plugin.
+        for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+
+            String propName = 
AntPropertyHelper.getDependencyArtifactPropertyName( artifact );
+
+            antProject.setProperty( propName, artifact.getFile().getPath() );
+        }
+    }
+
+    /**
+     * Get the current Maven project
+     * @return current Maven project
+     */
+    public MavenProject getMavenProject()
+    {
+        return this.project;
+    }
+    
+    public void initMavenTasks( Project antProject )
+    {
+        getLog().debug( "Initialize Maven Ant Tasks" );
+        Typedef typedef = new Typedef();
+        typedef.setProject( antProject );
+        typedef.setResource( ANTLIB );
+        //typedef.setURI( TASK_URI );
+        typedef.execute();
+    }
 }


Reply via email to