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/07/01 00:24:52 UTC

svn commit: r789987 - in /maven/plugins/trunk/maven-antrun-plugin/src: it/properties-test/ it/properties-test/src/ it/properties-test/src/main/ it/properties-test/src/main/java/ main/java/org/apache/maven/plugin/antrun/ site/apt/ site/apt/examples/

Author: pgier
Date: Tue Jun 30 22:24:51 2009
New Revision: 789987

URL: http://svn.apache.org/viewvc?rev=789987&view=rev
Log:
[MANTRUN-40] Pass properties to Ant so they can be used in external ant files.

Added:
    maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/
    maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml   (with props)
    maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml   (with props)
    maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/
    maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/
    maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/
    maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/Test.java   (with props)
Modified:
    maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java
    maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntPropertyHelper.java
    maven/plugins/trunk/maven-antrun-plugin/src/site/apt/examples/classpaths.apt
    maven/plugins/trunk/maven-antrun-plugin/src/site/apt/usage.apt

Added: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml?rev=789987&view=auto
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml (added)
+++ maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml Tue Jun 30 22:24:51 2009
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<project name="properties-test">
+
+    <target name="test">
+
+        <fail unless="maven.project.artifactId" message="Property not set ${project.artifactId}"/>
+        <fail unless="testProp1" message="Property not set ${testProp1}"/>
+        <fail unless="maven.dependency.junit.junit.jar.path" message="Property not set ${maven.dependency.junit.junit.jar.path}"/>
+      
+    </target>
+
+</project>

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml
------------------------------------------------------------------------------
    svn:executable = *

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/build.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml?rev=789987&view=auto
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml (added)
+++ maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml Tue Jun 30 22:24:51 2009
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  
+  <groupId>org.apache.maven.plugins.antrun</groupId>
+  <artifactId>properties-test</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>Test for passing properties to the ant build</name>
+  <description>Test for passing properties to the ant build</description>
+ 
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.2</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <version>@pom.version@</version>
+        <executions>
+          <execution>
+            <id>antrun</id>
+            <phase>generate-resources</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+            <configuration>
+              <tasks>
+
+                <ant antfile="${basedir}/build.xml" inheritRefs="true">
+                  <target name="test"/>
+                </ant>
+
+              </tasks>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <properties>
+    <testProp1>testProp1-value</testProp1>
+  </properties>
+</project>

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml
------------------------------------------------------------------------------
    svn:executable = *

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/Test.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/Test.java?rev=789987&view=auto
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/Test.java (added)
+++ maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/Test.java Tue Jun 30 22:24:51 2009
@@ -0,0 +1,7 @@
+public class Test {
+
+    public static void main(String args[]) {
+        System.out.println("done");
+    }
+
+}
\ No newline at end of file

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/it/properties-test/src/main/java/Test.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java?rev=789987&r1=789986&r2=789987&view=diff
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java (original)
+++ maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java Tue Jun 30 22:24:51 2009
@@ -24,6 +24,8 @@
 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;
@@ -109,6 +111,9 @@
 
             /* set maven.plugin.classpath with plugin dependencies */
             antProject.addReference( "maven.plugin.classpath", getPathFromArtifacts( pluginArtifacts, antProject ) );
+            
+            // The ant project needs actual properties vs. using expression evaluator when calling an external build file.
+            copyProperties( mavenProject, antProject );
 
             if ( getLog().isInfoEnabled() )
             {
@@ -168,4 +173,43 @@
         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 ) );
+        }
+        
+        // Add some of the common maven properties
+        antProject.setProperty( "maven.project.groupId", mavenProject.getGroupId() );
+        antProject.setProperty( "maven.project.artifactId", mavenProject.getArtifactId() );
+        antProject.setProperty( "maven.project.name", mavenProject.getName() );
+        antProject.setProperty( "maven.project.description", mavenProject.getDescription() );
+        antProject.setProperty( "maven.project.version", mavenProject.getVersion() );
+        antProject.setProperty( "maven.project.packaging", mavenProject.getPackaging() );
+        antProject.setProperty( "maven.project.build.directory", mavenProject.getBuild().getDirectory() );
+        antProject.setProperty( "maven.project.build.outputDirectory", mavenProject.getBuild().getOutputDirectory() );
+        antProject.setProperty( "maven.project.build.outputDirectory", mavenProject.getBuild().getTestOutputDirectory() );
+        antProject.setProperty( "maven.project.build.sourceDirectory", mavenProject.getBuild().getSourceDirectory() );
+        antProject.setProperty( "maven.project.build.testSourceDirectory", mavenProject.getBuild().getTestSourceDirectory() );
+        
+        // Add paths to depenedency artifacts
+        Set artifacts = mavenProject.getDependencyArtifacts();
+        for ( Iterator it = artifacts.iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+
+            String key = AntPropertyHelper.getDependencyArtifactPropertyName( artifact );
+
+            antProject.setProperty( key, artifact.getFile().getPath() );
+        }
+    }
 }

Modified: maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntPropertyHelper.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntPropertyHelper.java?rev=789987&r1=789986&r2=789987&view=diff
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntPropertyHelper.java (original)
+++ maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AntPropertyHelper.java Tue Jun 30 22:24:51 2009
@@ -42,7 +42,7 @@
 public class AntPropertyHelper
     extends PropertyHelper
 {
-    private static final String DEPENDENCY_PREFIX = "maven.dependency.";
+    public static final String DEPENDENCY_PREFIX = "maven.dependency.";
     private Log log;
     private ExpressionEvaluator exprEvaluator;
     private MavenProject mavenProject;
@@ -84,15 +84,28 @@
         {
             Artifact artifact = (Artifact) it.next();
 
-            String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
-                + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
-                + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
+            String key = getDependencyArtifactPropertyName( artifact );
 
             log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() );
 
             artifactMap.put( key, artifact.getFile().getPath() );
         }
     }
+    
+    /**
+     * Returns a property name for a dependency artifact.  The name is in the format
+     * maven.dependency.groupId.artifactId[.classifier].type.path
+     * 
+     * @param artifact
+     * @return
+     */
+    public static String getDependencyArtifactPropertyName( Artifact artifact )
+    {
+        String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
+            + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
+            + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
+        return key;
+    }
 
     /**
      * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean)

Modified: maven/plugins/trunk/maven-antrun-plugin/src/site/apt/examples/classpaths.apt
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/site/apt/examples/classpaths.apt?rev=789987&r1=789986&r2=789987&view=diff
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/site/apt/examples/classpaths.apt (original)
+++ maven/plugins/trunk/maven-antrun-plugin/src/site/apt/examples/classpaths.apt Tue Jun 30 22:24:51 2009
@@ -28,7 +28,15 @@
 
 Referencing the Maven Classpaths
 
- You can use these classpath references:
+ A property is set for each dependency with the format 
+ "maven.dependency.groupId.artifactId[.classifier].type.path".  For example, to show the path
+ to a dependency with groupId "org.apache" and artifactId "common-util", the following could be used.
+
+------
+<echo message="My Dependency JAR Path: ${mvn.dependency.org.apache.common-util.jar.path}"/> 
+------
+ 
+ You can also use these classpath references:
   
   * <<<maven.compile.classpath>>>
 
@@ -143,11 +151,3 @@
 </project>
 ------
 
-
- You can also use the path to each dependency artifact.  A property is set for each dependency with the
- format "maven.dependency.groupId.artifactId[.classifier].type.path".  For example to show the path
- to a dependency with groupId "org.apache" and artifactId "common-util", the following could be used.
-
-------
-<echo message="My Dependency JAR Path: ${mvn.dependency.org.apache.common-util.jar.path}"/> 
-------

Modified: maven/plugins/trunk/maven-antrun-plugin/src/site/apt/usage.apt
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/site/apt/usage.apt?rev=789987&r1=789986&r2=789987&view=diff
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/site/apt/usage.apt (original)
+++ maven/plugins/trunk/maven-antrun-plugin/src/site/apt/usage.apt Tue Jun 30 22:24:51 2009
@@ -150,6 +150,30 @@
   <<<\<testSourceRoot/\>>>> adds a single folder to the list of folders that get
   compiled with the test source code (<<<test-compile>>>).
 
+* Using maven properties
+
+  All of the properties available to maven are also available in the tasks
+  configuration.  However, you may want to call an external Ant build script
+  using the <ant> task.  To avoid name conflicts, only a subset of the properties
+  are passed to the external Ant build.  These include all properties defined in
+  the <properties> section of the POM.  It also includes prefixed versions of
+  some of the commonly used Maven properties.
+  
+------
+  maven.project.groupId
+  maven.project.artifactId
+  maven.project.name
+  etc.
+------
+  
+  If the maven property you want to use is not available in an external file, you
+  will have to redefine the property before calling <ant>.
+  
+------
+  <property name="maven.project.url" value="${project.url}"/>
+  <ant antfile="build.xml"/>
+------
+
 * Ant Expressions to Maven Expressions Mapping
 
   Some Ant expressions have their respective counterparts in Maven. Thus, one