Deploy Snapshots and Stage Releases with Ant

Skip to end of metadata
Go to start of metadata

Maven repositories (even The Central Repository) are not limited to Maven users, Ant users can deploy artifacts to Maven repositories as well. First you need to manually create a pom.xml, which must contain all the elements specified in Central Sync Requirements.

Now your project directory should look like this:

build.xml
pom.xml
src/
lib/
...

Then install the Maven Ant Tasks. Download it from http://maven.apache.org/ant-tasks/download.html and put it into ~/.ant/lib/.

Now write your Ant build.xml like this:

<project name="ant-demo" default="deploy" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">

	<property name="src" location="src" />
	<property name="build" location="build" />
	<property name="dist" location="dist" />

	<!-- define Maven coordinates -->
	<property name="groupId" value="com.juvenxu" />
	<property name="artifactId" value="ant-demo" />
	<property name="version" value="1.0-SNAPSHOT" />

        <!-- define artifacts' name, which follows the convention of Maven -->
	<property name="maven-jar" value="${dist}/lib/${artifactId}-${version}.jar" />
	<property name="maven-javadoc-jar" value="${dist}/lib/${artifactId}-${version}-javadoc.jar" />
	<property name="maven-sources-jar" value="${dist}/lib/${artifactId}-${version}-sources.jar" />

        <!-- defined maven snapshots and staging repository id and url -->
	<property name="maven-snapshots-repository-id" value="sonatype-nexus-snapshots" />
	<property name="maven-snapshots-repository-url" value="https://oss.sonatype.org/content/repositories/snapshots/" />
	<property name="maven-staging-repository-id" value="sonatype-nexus-staging" />
	<property name="maven-staging-repository-url" value="https://oss.sonatype.org/service/local/staging/deploy/maven2/" />


	<target name="init">
		<mkdir dir="${build}" />
		<mkdir dir="${dist}/lib" />
	</target>

	<target name="compile" depends="init" description="compile the source ">
		<javac srcdir="${src}" destdir="${build}" />

	</target>

	<target name="dist" depends="compile" description="generate the distribution">

		<!-- build the main artifact -->
		<jar jarfile="${maven-jar}" basedir="${build}" />

		<!-- build the javadoc artifact -->
		<javadoc sourcepath="${src}" destdir="${dist}/javadoc" />
		<jar jarfile="${maven-javadoc-jar}">
			<fileset dir="${dist}/javadoc" />
		</jar>

		<!-- build the sources artifact -->
		<jar jarfile="${maven-sources-jar}">
			<fileset dir="${src}" />
		</jar>
	</target>

        <target name="deploy" depends="dist" description="deploy snapshot version to Maven snapshot repository">
		<artifact:mvn>
			<arg value="org.apache.maven.plugins:maven-deploy-plugin:2.6:deploy-file" />
			<arg value="-Durl=${maven-snapshots-repository-url}" />
			<arg value="-DrepositoryId=${maven-snapshots-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-jar}" />
		</artifact:mvn>
        </target>

        <!-- before this, update project version (both build.xml and pom.xml) from SNAPSHOT to RELEASE -->
	<target name="stage" depends="dist" description="deploy release version to Maven staging repository">
		<!-- sign and deploy the main artifact -->
		<artifact:mvn>
			<arg value="org.apache.maven.plugins:maven-gpg-plugin:1.3:sign-and-deploy-file" />
			<arg value="-Durl=${maven-staging-repository-url}" />
			<arg value="-DrepositoryId=${maven-staging-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-jar}" />
                        <arg value="-Pgpg" />
		</artifact:mvn>

		<!-- sign and deploy the sources artifact -->
		<artifact:mvn>
			<arg value="org.apache.maven.plugins:maven-gpg-plugin:1.3:sign-and-deploy-file" />
			<arg value="-Durl=${maven-staging-repository-url}" />
			<arg value="-DrepositoryId=${maven-staging-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-sources-jar}" />
			<arg value="-Dclassifier=sources" />
                        <arg value="-Pgpg" />
		</artifact:mvn>

		<!-- sign and deploy the javadoc artifact -->
		<artifact:mvn>
			<arg value="org.apache.maven.plugins:maven-gpg-plugin:1.3:sign-and-deploy-file" />
			<arg value="-Durl=${maven-staging-repository-url}" />
			<arg value="-DrepositoryId=${maven-staging-repository-id}" />
			<arg value="-DpomFile=pom.xml" />
			<arg value="-Dfile=${maven-javadoc-jar}" />
			<arg value="-Dclassifier=javadoc" />
                        <arg value="-Pgpg" />
		</artifact:mvn>
	</target>

	<target name="clean" description="clean up">
		<delete dir="${build}" />
		<delete dir="${dist}" />
	</target>

</project>

Some explanation to the code above:

  1. xmlns:artifact="antlib:org.apache.maven.artifact.ant" is mandatory for installing Maven Ant Tasks
  2. you need to package a main jar artifact, a javadoc jar artifact and a sources jar artifact
  3. the deploy task uses Maven to deploy your main artifacts to the snapshot repository
  4. the stage task uses Maven to sign and deploy artifacts, you also need to make sure GPG is correctly installed
  5. maven-snapshots-repository-url is the snapshot deploy URL, maven-snapshots-repository-id is used to identify it
  6. maven-staging-repository-url is the staging deploy URL, maven-staging-repository-id is used to identify it

To make the Ant script work, you also need to create a XML at ~/.m2/settings.xml, with content like this:

<settings>
  <servers>
    <server>
      <id>sonatype-nexus-snapshots</id>
      <username>your-username</username>
      <password>your-password</password>
    </server>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>your-username</username>
      <password>your-password</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <id>gpg</id>
      <properties>
        <gpg.passphrase>xxxxxx</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>
  1. the <server> element is used to configure nexus access credential, its id must be identical to the value of maven-snapshots-repository-id and maven-staging-repository-id property in build.xml.
  2. the <profile> element is used to provide your gpg passphrase, it's not a good idea to put it directly into build.xml, so you should put passphrase here, and <arg value="-Pgpg" /> in build.xml will make it work.

To deploy a SNAPSHOT, make sure the 'version' element in your pom.xml the 'version' property in your build.xml has a SNAPSHOT version value (like 1.0-SNAPSHOT), then run ant deploy.
To stage a release, make sure the 'version' element in your pom.xml the 'version' property in your build.xml has a RELEASE version value (like 1.0), then run ant stage.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.