Access a maven-ant-tasks dependency's properties (declared in its pom) from ant

637 views Asked by At

I'm using maven-ant-tasks for a project, and I've run into the need to reference properties declared in a dependency's pom from Ant.

Specifically, I'm depending on waffle-jna:

<property name="waffle-jna-version" value="1.7" />
<artifact:dependencies ...>
  <dependency groupId="com.github.dblock.waffle" artifactId="waffle-jna" version="${waffle-jna-version}">
</artifact:dependencies>

and in its pom.xml, com.github.dblock.waffle:waffle-jna declares:

<properties>
  <guava.version>18.0</guava.version>
  <jna.version>4.1.0</jna.version>
  <servlet.version>2.5</servlet.version>
  <slf4j.version>1.7.7</slf4j.version>
</properties>

I need to find out what jna and slf4j versions the version of Waffle I'm building against uses, and then add some related dependencies (e.g. a runtime dependency on slf4j-simple) with the same versions.

I'm aware that I can instead exclude the dependencies for Waffle in maven-ant-tasks then declare them with my own versions at the top level, but I'd prefer to follow the versions that are used by the Waffle version I'm using.

Is there any way to tell maven-ant-tasks to expose properties declared in a dependency pom to Ant?


I tried importing the pom, with an additional dependency on the pom plus an <artifact:pom>:

<artifact:dependencies>
  <dependency groupId="com.github.dblock.waffle" artifactId="waffle-jna" version="${waffle-jna-version}" type="pom"/>
</artifact:dependencies>

<!-- Expose the Waffle POM so we can reference its version attributes -->
<artifact:pom file="${com.github.dblock.waffle:waffle-jna:pom}" inheritAllProperties="true"/>

... but while it sets the property for the path to the pom correctly:

ant -debug
....
Setting project property: com.github.dblock.waffle:waffle-jna:pom -> /home/craig/.m2/repository/com/github/dblock/waffle/waffle-jna/1.7/waffle-jna-1.7.pom
Adding reference: com.github.dblock.waffle:waffle-jna:pom
....
[artifact:pom] Maven Ant Tasks version: 2.1.3

BUILD FAILED
/home/craig/projects/2Q/pgjdbc/build.xml:148: java.lang.NullPointerException
        at java.util.Hashtable.get(Hashtable.java:334)
        at org.apache.tools.ant.Project$AntRefTable.getReal(Project.java:2409)
        at org.apache.tools.ant.Project$AntRefTable.access$000(Project.java:2394)
        at org.apache.tools.ant.Project.addReference(Project.java:1973)
        at org.apache.maven.artifact.ant.Pom.doExecute(Pom.java:423)
        at org.apache.maven.artifact.ant.AbstractArtifactTask.execute(AbstractArtifactTask.java:751)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
....

so I'm at my wits end. I'd rather just use Maven for the whole lot, but unfortunately that's not currently a decision I can make unilaterally.

1

There are 1 answers

2
333kenshin On BEST ANSWER

Your pom import statement above:

<!-- Expose the Waffle POM so we can reference its version attributes -->
<artifact:pom file="${com.github.dblock.waffle:waffle-jna:pom}" inheritAllProperties="true"/>

differs slightly from what's in the maven ant-tasks pom documentation:

<artifact:pom id="mypom" file="pom.xml" />

You're missing the id="mypom" assignment.

It will let you reference properties by name like ${mypom.properties.jna.version}

Note that you must specify the path to the properties node explicitly; properties aren't added directly under mypom, but appear under mypom.properties, mirroring the pom.xml structure. (This also means that inherited properties from parent poms won't be exposed).