6

I've embedded the following code within my POM:

<plugin name="test">
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
      <executions>
        <execution>
          <phase>validate</phase>
            <configuration>
              <tasks>
                <pathconvert targetos="unix" property="project.build.directory.portable">
                  <path location="${project.build.directory}"/>
                </pathconvert>
              </tasks>
            </configuration>
          <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

I then reference ${project.build.directory.portable} from the run project action but it comes back as null. Executing <echo> within the Ant block shows the correct value. What am I doing wrong?

Gili
  • 86,244
  • 97
  • 390
  • 689

4 Answers4

14

For completeness, the mentioned feature was implemented in the maven-antrun-plugin in October 2010.

The configuration parameter you are looking for is exportAntProperties.

Example of use:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7-SNAPSHOT</version>
    <executions>
        <execution>
            <phase>process-resources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <exec outputproperty="svnversion"
                        executable="svnversion">
                        <arg value=".." />
                    </exec>
                </target>
                <exportAntProperties>true</exportAntProperties>
            </configuration>
        </execution>
    </executions>
</plugin>

As a side note, at the time of this post (2011-10-20), the official plugin documentation didn't have this option documented. To get the help for 'versionXYZ' of the plugin:

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-antrun-plugin:versionXYZ -Ddetail
Alberto
  • 5,021
  • 4
  • 46
  • 69
  • Hm, is it really work? I have some troubles according to this [issue](http://maven.40175.n5.nabble.com/exportAntProperties-not-working-in-3-0-4-td5732071.html) – Dmytro Danilenkov Jan 06 '14 at 08:49
  • 1
    try to put the exportAntProperties as global configuration and not as the execution configuration – Joram Jan 13 '14 at 13:07
  • 2
    I noticed that this won't override properties already declared in the POM, but if I don't declare the property my linter (using IntelliJ) complains about the unknown property symbol. Is there a way to tell it to ignore this, or to tell the plugin to override an existing property? – David Pisoni Sep 11 '15 at 20:07
5

The version 1.7 of the maven-antrun-plugin worked for me to pass a property from ant to maven (and from mvn to ant). Some sample code that calculates an md5 checksum of a file and later stores it into a property that is accessed by mvn at a later time:

<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
    <execution>
        <id>ant-md5</id>
        <phase>initialize</phase>
        <goals>
        <goal>run</goal>
        </goals>
    <configuration>

<target>
    <property name="compile_classpath" refid="maven.compile.classpath"/>
    <property name="outputDir" value="${project.build.outputDirectory}"/>
    <property name="sourceDir" value="${project.build.sourceDirectory}"/>
    <checksum  file="${sourceDir}/com/blah/db/blah.java" property="blah.md5db"/>
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
</execution>
</executions>

The property is accessible in later with ${blah.md5db} in a java file.

nurieta
  • 1,615
  • 15
  • 6
0

From the plugin documentation here:

Try to add the maven prefix, so you have <path location="${maven.project.build.directory}"/> instead

If that doesn't work, you may need to explictly redefine the property yourself:

<property name="maven.project.build.dir" value="${project.build.directory}"/>
<path location="${maven.project.build.directory}"/>
Greg Case
  • 3,200
  • 1
  • 19
  • 17
-1

I don't think you can set a property from Ant that will be visible from Maven. You should write a Mojo.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Put it another way: how would you ensure that ${basedir} contains unix-style slashes even under Windows? – Gili Jun 18 '10 at 15:37
  • @Gili I'm not under Windows so I can't test things extensively (sorry, too lazy to start a VM) but I don't think you can. I still don't get why you need this but if it was the case, I would inject the `basedir` property in a Mojo, rewrite it using unix style slashes and expose it under another property. – Pascal Thivent Jun 18 '10 at 15:52
  • Sure but that would require me to write a new mojo. Isn't there an elegant way to do this with the existing plugins? – Gili Jun 19 '10 at 23:57
  • This doesn't seem to be possible from the Ant plugin :( – Gili Jul 13 '10 at 03:01
  • 4
    Since October 2010 this is possible. See [my answer below](http://stackoverflow.com/questions/3066252/exporting-maven-properties-from-ant-code/7836089#7836089) – Alberto Oct 20 '11 at 12:46