2

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?

Community
  • 1
  • 1
Diversity
  • 1,890
  • 13
  • 20
  • First your pom snippet does not look like it inherits from a parent which is the case if you are within the module of a larger multi module build...Furthermore the question is why are you using maven-antrun-plugin ? – khmarbaise May 07 '16 at 21:38
  • @khmarbaise Firstly, because i wanted to show just the relevent parts of the problem, in this case the pom snippets. Secondly i use the ant plugin because i need to build native C++, JNI, stuff and this way it seems for me to be the most pragmatic way. Maybe you can offer me a nice solution to build a c++ project.using maven, concerning the appropriate java client leveraging its functionallity. – Diversity May 07 '16 at 22:05
  • I can recommend to take a look at https://github.com/maven-nar/nar-maven-plugin ... – khmarbaise May 07 '16 at 22:49
  • yes ok thx. I know this plugin. It's the next step, a lot of configuration stuff. But can you give me a solution for my given problem and not any alternatives. Because the problem is really easy to solve right now by changing just the property manually. I asked the question because it seemed to me that it is pretty strange behaviour. Because concerning the references SO question there should be no problem. – Diversity May 07 '16 at 22:57
  • Are Ant properties allowed to contain hyphens? – Jens Piegsa May 08 '16 at 08:02
  • I have my doubts...I would suggest to use `target.version` instead of `target-version`... – khmarbaise May 08 '16 at 10:28

1 Answers1

2

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>
Diversity
  • 1,890
  • 13
  • 20