Maven antrun: pass maven properties to ant
Asked Answered
D

3

17

I am trying to pass maven properties (defined through profiles) to a antrun execution:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <dependencies>
        <!-- ... these are ok -->
    </dependencies>
    <executions>
        <execution>
            <configuration>
                <target>
                    <property name="ant_destDir" value="${destDir}" />
                    <property name="ant_serverDeploy" value="${serverDeploy}" />
                    <property name="ant_deployDir" value="${deployDir}" />
                    <property name="ant_userDeploy" value="${userDeploy}" />
                    <property name="ant_passwordDeploy" value="${passwordDeploy}" />
                    <!-- correct task definitions for sshexec and scp -->
                    <sshexec host="${serverDeploy}" username="${userDeploy}" 
                            password="${passwordDeploy}" trust="yes" 
                            command="some command" />
                    <scp remoteTodir="${userDeploy}@${serverDeploy}:${destDir}" 
                            password="${passwordDeploy}" trust="yes" sftp="true">
                        <fileset dir="${deployDir}" includes="*.jar" />
                    </scp>
                    <sshexec host="${serverDeploy}" username="${userDeploy}" 
                            password="${passwordDeploy}" trust="yes" 
                            command="some command" />
                </target>
            </configuration>
            <phase>install</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The properties are defined in profiles to allow for deployment in different servers (I know it's not the best possible approach, but this is the way things are done here), like this:

<profile>
    <id>aprofile</id>
    <properties>
        <property name="serverDeploy" value="somevalue" />
        <property name="userDeploy" value="someuser" />
        <property name="passwordDeploy" value="somepassword" /> 
        <!-- and so on -->
    </properties>
</profile>

My problem is that I can't get maven properties to work in ant plugin; I tried to add a <echoproperties> task in ant to see which properties I have and there is no trace of maven properties. Is it possible to use maven defined properties or should I use another approach? Any suggestion is welcome.

Edit: I modified the script as per first answer, it still doesn't work

Dennisedennison answered 8/8, 2013 at 14:30 Comment(0)
H
21

You can pass the properties by defining new Ant properties (using the property tag in your target within the configuration). So for example:

<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>com.test</groupId>
    <artifactId>test-module</artifactId>
    <name>test-module</name>
    <version>SNAPSHOT</version>

    <properties>
        <my.custom.property>false</my.custom.property>
    </properties>

    <profiles>
        <profile>
            <id>customProfile</id>
            <properties>
                <my.custom.property>true</my.custom.property>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <configuration>
                            <target>
                                <property name="antProperty" value="${my.custom.property}"/>
                                <echo message="Custom Ant Property is: ${antProperty}"/>
                                <echoproperties />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

When I execute mvn compile on this pom the output is:

main:
[echo] Custom Ant Property is: false
[echoproperties] #Ant properties
[echoproperties] #Thu Aug 08 17:17:30 CEST 2013
[echoproperties] ant.project.name=maven-antrun-
[echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010
[echoproperties] antProperty=false

and when the command is mvn -PcustomProfile compile then the output is:

main:
[echo] Custom Ant Property is: true
[echoproperties] #Ant properties
[echoproperties] #Thu Aug 08 17:18:30 CEST 2013
[echoproperties] ant.project.name=maven-antrun-
[echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010
[echoproperties] antProperty=true

This works using maven 3.0.5.

Heidelberg answered 8/8, 2013 at 14:50 Comment(4)
@RiccardoCossu I created a test project that just contained a single pom (I've edited my answer to include the complete pom). As described in the answer when I execute maven the property is correctly replaced in the antrun call. What version of maven are you using?Heidelberg
I tried your test project and it works; I still have to understand why it isn't working in my specific case... :-( I am using 3.0.4Dennisedennison
got it! I fell into a loop of confusion between ant and maven... I was declaring the property the "ant" way in maven part (with a tag named property, instead of the custom name)Dennisedennison
glad to hear you got it sorted.Heidelberg
M
4

Most properties are automatically passed along to ant, at least if you're running an inlined ant script. Some of the properties get renamed. I suggest running "mvn -X" and the antrun plugin prints a list of all the variable mappings into ant (things like basedir becomes project.basedir, etc.)

Misti answered 30/5, 2014 at 18:41 Comment(0)
C
3

On the newer versions of maven you can just use:

<taskdef  resource="net/sf/antcontrib/antcontrib.properties"
                    classpathref="maven.plugin.classpath" />

example:

   <build>
....    
       <plugins>
         ....
    <plugin>
         <artifactId>maven-antrun-plugin</artifactId>
         <executions>
            <execution>
            <phase>install</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
            <tasks>
              <taskdef  resource="net/sf/antcontrib/antcontrib.properties"
                                        classpathref="maven.plugin.classpath" />

              <echo message="Project name from Maven: ${project.name}" />

            </tasks>
        </configuration>
         </execution>
       </executions>
    </plugin>
         ....
      </plugins>
         ....
   </build>

<dependencies>
<dependency>
    <groupId>ant-contrib</groupId>
    <artifactId>ant-contrib</artifactId>
    <version>1.0b3</version>
    <exclusions>
        <exclusion>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>ant</groupId>
    <artifactId>ant-nodeps</artifactId>
    <version>1.6.5</version>
</dependency>
</dependencies>
Celiotomy answered 11/12, 2013 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.