Automatically Generating a Maven POM file with ANT

This 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:

&lt;artifact:pom id="maven.project" groupId="com.example" version="${build.version}" artifactId="${project.name}" packaging="zip"&gt;
	&lt;remoteRepository id="releases" url="http://maven:8081/nexus/content/repositories/releases" /&gt;
&lt;/artifact:pom&gt;
&lt;artifact:install-provider artifactId="wagon-http-lightweight" version="1.0-beta-2" /&gt;
&lt;artifact:deploy file="deploy/${project.name}.zip">
    &lt;pom refid="maven.project" /&gt; 
&lt;/artifact:deploy&gt;

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:

&lt;!--  Generate a Dynamic POM file --&gt;
&lt;artifact:pom id="pom.tmp.ref" groupId="com.example" version="${build.version}" artifactId="${project.name}" packaging="zip" /&gt;
 
&lt;!--  Write the generated POM file out to a temporary file on disc --&gt;
&lt;tempfile property="pom.tmp.file" /&gt;
&lt;artifact:writepom pomRefId="pom.tmp.ref" file="${pom.tmp.file}" /&gt;
 
&lt;!--  Read the temporary file back in as the POM reference --&gt;
&lt;artifact:pom id="maven.project" file="${pom.tmp.file}" /&gt;
 
&lt;!-- We can now deploy, but we have to include a reference to the repository --&gt;
&lt;artifact:install-provider artifactId="wagon-http-lightweight" version="1.0-beta-2" /&gt;
&lt;artifact:deploy file="deploy/${project.name}.zip"&gt;
    &lt;pom refid="maven.project" /&gt; 
    &lt;remoteRepository id="releases" url="http://maven:8081/nexus/content/repositories/releases" /&gt;
&lt;/artifact:deploy&gt;
This entry was posted in ActionScript 2. Bookmark the permalink.

One Response to Automatically Generating a Maven POM file with ANT

  1. Erik says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">