Automatically Generating a Maven POM file with ANT
26th January, 2010 – 7:20 pmThis one tripped me up for a while and doesn’t seem to be mentioned in the Maven ANT Tasks Documentation. I was working on a build process which makes use of Maven soley for deploying and versioning releases and ANT for the rest of the build. One thing I wanted to eliminate was the large amount of POM files which were scattered around the project, all of which were basically the same and could most certainly be auto-generated.
The Maven ANT Tasks indicate that the <pom /> task can be used to create an “in-memory” pom which can then be used in the <deploy /> task. With that in mind, I wrote the following:
<remoteRepository id="releases" url="http://maven:8081/nexus/content/repositories/releases" />
</artifact:pom>
<artifact:install-provider artifactId="wagon-http-lightweight" version="1.0-beta-2" />
<artifact:deploy file="deploy/${project.name}.zip">
<pom refid="maven.project" />
</artifact:deploy>
However, when this ran, the build failed with the following error: “A distributionManagement element or remoteRepository element is required to deploy“. Very confusing, especially as I specified the <remoteRepository /> node as a child of the <pom />. I concluded that this was not a problem with the <deploy /> task as when I dump the contents of “maven.pom” out to disk via <writepom /> I could see that, sure enough, the <repositories /> node was not being created in the POM file! This is where I hit a brick wall and I can only presume it’s a bug with the current Maven ANT Tasks (2.1.0).
With that in mind, I tried modifying the <deploy /> task, adding the <remoteRepository /> node to that instead – again, no joy, same error.
In the end I got it to work with a little bit of a workaround involving temp files – this works fine for me but it’s not ideal, hopefully someone out there has encountered this one before:
<artifact:pom id="pom.tmp.ref" groupId="com.example" version="${build.version}" artifactId="${project.name}" packaging="zip" />
<!– Write the generated POM file out to a temporary file on disc –>
<tempfile property="pom.tmp.file" />
<artifact:writepom pomRefId="pom.tmp.ref" file="${pom.tmp.file}" />
<!– Read the temporary file back in as the POM reference –>
<artifact:pom id="maven.project" file="${pom.tmp.file}" />
<!– We can now deploy, but we have to include a reference to the repository –>
<artifact:install-provider artifactId="wagon-http-lightweight" version="1.0-beta-2" />
<artifact:deploy file="deploy/${project.name}.zip">
<pom refid="maven.project" />
<remoteRepository id="releases" url="http://maven:8081/nexus/content/repositories/releases" />
</artifact:deploy>
One Response to “Automatically Generating a Maven POM file with ANT”
I think there might be a few gotchas that could get you on the right track.
First, when creating the pom, you should use the distributionManagement element of the POM (see http://maven.apache.org/pom.html#Distribution_Management) for specifying repositories. The remoteRepository element that you specified is not a POM element but rather a sub element to the artifact:deploy ant task (which is how you correctly used it in the “workaround”). I agree that the error message you got is not crystal clear in that regard.
Also, if you specify the trim=”false” attribute on the writepom-task, it will write out the whole pom, by default it for some reason trims “unnecessary” information such as build configuration, repositories, and profiles. (See the bottom of http://maven.apache.org/ant-tasks/examples/write-pom.html for details).
Hope that helps.
By Erik on Mar 11, 2010