You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by pg...@apache.org on 2009/06/17 17:46:59 UTC

svn commit: r785685 - in /maven/ant-tasks/trunk/src: main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java main/java/org/apache/maven/artifact/ant/Pom.java site/apt/reference.apt

Author: pgier
Date: Wed Jun 17 15:46:59 2009
New Revision: 785685

URL: http://svn.apache.org/viewvc?rev=785685&view=rev
Log:
[MANTTASKS-153] Allow properties to be passed from Ant to a Maven POM.

Modified:
    maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java
    maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/Pom.java
    maven/ant-tasks/trunk/src/site/apt/reference.apt

Modified: maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java
URL: http://svn.apache.org/viewvc/maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java?rev=785685&r1=785684&r2=785685&view=diff
==============================================================================
--- maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java (original)
+++ maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java Wed Jun 17 15:46:59 2009
@@ -514,7 +514,7 @@
 
     public Pom buildPom( ArtifactRepository localArtifactRepository )
     {
-        if ( pomRefId != null && pom != null )
+        if ( pomRefId != null && this.pom != null )
         {
             throw new BuildException( "You cannot specify both a POM element and a pomrefid element" );
         }
@@ -689,6 +689,9 @@
             Thread.currentThread().setContextClassLoader( originalClassLoader );
         }
     }
-
+    
+    /**
+     * The main entry point for the task.
+     */
     protected abstract void doExecute();
 }

Modified: maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/Pom.java
URL: http://svn.apache.org/viewvc/maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/Pom.java?rev=785685&r1=785684&r2=785685&view=diff
==============================================================================
--- maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/Pom.java (original)
+++ maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/Pom.java Wed Jun 17 15:46:59 2009
@@ -31,9 +31,11 @@
 import org.apache.maven.model.Repository;
 import org.apache.maven.model.Scm;
 import org.apache.maven.profiles.ProfileManager;
+import org.apache.maven.project.DefaultProjectBuilderConfiguration;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectBuilder;
 import org.apache.maven.project.MavenProjectHelper;
+import org.apache.maven.project.ProjectBuilderConfiguration;
 import org.apache.maven.project.ProjectBuildingException;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
@@ -42,8 +44,10 @@
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Properties;
 
 /**
  * A POM typedef.
@@ -68,6 +72,8 @@
     private File file;
 
     private List profiles = new ArrayList();
+    
+    private boolean inheritAllProperties = true;
 
     /**
      * The property interceptor.
@@ -164,11 +170,10 @@
         if ( file != null )
         {
             addAntRepositoriesToProfileManager();
-
+            ProjectBuilderConfiguration builderConfig = this.createProjectBuilderConfig( localRepository );
             try
             {
-
-                mavenProject = builder.build( file, localRepository, getActivatedProfiles() );
+                mavenProject = builder.build( file, builderConfig );
             }
             catch ( ProjectBuildingException e )
             {
@@ -325,15 +330,15 @@
         MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
         initialise( projectBuilder, localRepo );
 
-        Project project = getProject();
+        Project antProject = getProject();
 
         // Add a reference to this task/type
-        project.addReference( antId, this );
+        antProject.addReference( antId, this );
 
         // Register the property interceptor
-        PropertyHelper phelper = PropertyHelper.getPropertyHelper( project );
+        PropertyHelper phelper = PropertyHelper.getPropertyHelper( antProject );
         helper.setNext( phelper.getNext() );
-        helper.setProject( project );
+        helper.setProject( antProject );
         phelper.setNext( helper );
     }
 
@@ -450,4 +455,67 @@
         }
         return profileManager;
     }
+    
+    /** 
+     * Create a project builder configuration to be used when initializing the maven project.
+     * 
+     * @return
+     */
+    private ProjectBuilderConfiguration createProjectBuilderConfig( ArtifactRepository localArtifactRepository)
+    {
+        ProjectBuilderConfiguration builderConfig = new DefaultProjectBuilderConfiguration();
+        builderConfig.setLocalRepository( localArtifactRepository );
+        builderConfig.setGlobalProfileManager( this.getActivatedProfiles() );
+        builderConfig.setUserProperties( getAntProjectProperties() );
+        builderConfig.setExecutionProperties(  getAntProjectProperties()  );
+        
+        return builderConfig;
+    }
+
+
+    /**
+     * Convert the Hashtable of Ant project properties to a Properties object
+     * 
+     * @return The Ant project properties
+     */
+    public Properties getAntProjectProperties()
+    {
+        Properties properties = new Properties();
+        Hashtable propsTable = null;
+        if ( this.isInheritAllProperties() )
+        {
+            propsTable = getProject().getProperties();
+        }
+        else
+        {
+            propsTable = getProject().getUserProperties();
+        }
+        Iterator propsIter = propsTable.keySet().iterator();
+        
+        while ( propsIter.hasNext() )
+        {
+            String key = (String)propsIter.next();
+            String value = (String)propsTable.get( key );
+            properties.setProperty( key, value );
+        }
+        
+        return properties;
+    }
+
+    /**
+     * If set to true, all properties are passed to the maven pom.
+     * If set to false, only user properties are passed to the pom.
+     * 
+     * @param inheritAllProperties
+     */
+    public void setInheritAllProperties( boolean inheritAllProperties )
+    {
+        this.inheritAllProperties = inheritAllProperties;
+    }
+
+    public boolean isInheritAllProperties()
+    {
+        return inheritAllProperties;
+    }
+    
 }

Modified: maven/ant-tasks/trunk/src/site/apt/reference.apt
URL: http://svn.apache.org/viewvc/maven/ant-tasks/trunk/src/site/apt/reference.apt?rev=785685&r1=785684&r2=785685&view=diff
==============================================================================
--- maven/ant-tasks/trunk/src/site/apt/reference.apt (original)
+++ maven/ant-tasks/trunk/src/site/apt/reference.apt Wed Jun 17 15:46:59 2009
@@ -227,13 +227,15 @@
 
   The POM element will load a POM file and make it available as a reference for the other tasks or as properties.
 
-*------------------+--------------------------------------------------------+--------------+
-| <<Attribute>>    | <<Description>>                                        | <<Required>> |
-*------------------+--------------------------------------------------------+--------------+
+*------------------+--------------------------------------------------------+--------------+-------------+
+| <<Attribute>>    | <<Description>>                                        | <<Required>> | << Since >> |
+*------------------+--------------------------------------------------------+--------------+-------------+
 | <<<file>>>       | The file of the POM to load.                           | Yes          |
 *------------------+--------------------------------------------------------+--------------+
 | <<<id>>>         | The reference ID of this POM.                          | No           |
 *------------------+--------------------------------------------------------+--------------+
+| <<<inheritAllProperties>>>  | If set to true, all Ant properties will be passed to the Maven POM.  If set to false, only user (command-line) properties will be passed.  Defaults to true.   | No    | 2.1.0 |
+*------------------+--------------------------------------------------------+--------------+
 | <<<settingsFile>>>  | The settings file to use. Defaults to <<<$\{user.home\}/.ant/settings.xml>>> or if that doesn't exist <<<$\{user.home\}/.m2/settings.xml>>>. | No | 2.0.6 |
 *---------------------+--------------------------------------------------------------------------+-------------------------------------------------+-------------+