Diversity
Diversity

Reputation: 1900

Unable to pass maven property to ant run plugin

I have a problem passing a custom maven property (version) to an script executed by maven using the maven-antrun-plugin.

I have already read Maven antrun: pass maven properties to ant but this didn't solve the problem.

There is one difference between my problem and the one given within the mentioned SO question.

In my case the maven-antrun-plugin is executed as a child of an parent project, where the property is defined as a global property for all modules.

The maven-antrun-plugin is one module of the project.

Parent pom snippet looks like this:

<project ...> 
  <!-- Parent -->
  <modelVersion>4.0.0</modelVersion>
  <groupId>xxx</groupId>
  <artifactId>xxx</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
     <my.project.version>0.0.3-SNAPSHOT</my.project.version>
     <maven.compiler.source>1.7</maven.compiler.source>
     <maven.compiler.target>1.7</maven.compiler.target>
     <java.version>1.7</java.version>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

ant run pom looks like this. The property is named target.version

<project>
      <!-- Module -->
 <parent>
    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent> 

 <build>
    <plugins>
         <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                  <execution>
                    <id>ant-magic</id>
                    <phase>prepare-package</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                    <configuration>
                      <tasks>
                        <property name="compile_classpath" 
                                  refid="maven.compile.classpath"/>
                        <property name="outputDir"
                                  value="${project.build.outputDirectory}"/>
                        <property name="sourceDir"
                                  value="${project.build.sourceDirectory}"/>
                        <property name="compile_classpath" refid="maven.compile.classpath"/>

                        <property name="target-version" refid="${my.project.version}"/>
                        <echo message="Passed target version to ant build script: ${target-version}"/>      

                        <ant antfile="${basedir}/src/main/ant/create-dist.xml"
                             target="deploy-ea"/>
                      </tasks>
                    </configuration>
                  </execution>
                </executions>
              </plugin>
        </plugins>
</build>
</project>

The following maven build error occurs during the excution of the build of the child module which contains the antrun-maven-plugin.

`An Ant BuildException has occured: Reference 0.0.3-SNAPSHOT` not found

which is interesting because this is the value of the maven property.

I also tried just to use the simple property name as argument for the

refid attribute 

In my case

<property name="target-version" refid="my.project.version"/>

Which leads to the following error:

   An Ant BuildException has occured: Reference my.project.version not found

The ant script uses the property as follows

<property name="myproject-jar" value="mymodulename-${target-version}.jar"/>

Do I have a mistake within my configuration or is the problem that the property is defined within the parent pom. And therefor maybe a maven lifecycle issue?

Upvotes: 2

Views: 2900

Answers (1)

Diversity
Diversity

Reputation: 1900

I used the wrong attibute name refid for the property-Tag. Changing the attribute to value solved the problem. Additionally i replaced the tasks tag to target because it is marked as deprecated. But this had no influence to the given problem.

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>ant-magic</id>
                        <phase>prepare-package</phase> 
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <property name="compile_classpath" 
                                          refid="maven.compile.classpath"/>
                                <property name="outputDir"
                                          value="${project.build.outputDirectory}"/>
                                <property name="sourceDir"
                                          value="${project.build.sourceDirectory}"/>
                                <property name="compile_classpath" refid="maven.compile.classpath"/>

                                <property name="target-version" value="${my.project.version}"/> <!-- PROBLEM refid should be value-->
                                <echo message="Passed target version to ant build script: ${target-version}"/>      

                                <ant antfile="${basedir}/src/main/ant/create-dist.xml"
                                     target="deploy-ea"/>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Upvotes: 3

Related Questions