»
November 08, 2009
»

Publishing Maven Artifacts

While mvn install publishes artifact to local ~/.m2/repository there is certainly good rationale to create a remote “company” maven repository (either using cool tools like nexus or with just simple HTTP server) to share built artifacts.

If you’re like me — just starting with Maven, plain old HTTP will be fine and when repository is ready (when mkdir /var/www/repository has done it’s job) then the next question is how to publish artifacts.

There are 2 ways (I’m aware of) to get built artifacts in repository:

  • maven-release-plugin
  • maven-deploy-plugin

Before diving in both of them pom.xml needs a tweaks.

Settings

Maven needs to know where your repository is. We need to add distributionManagement element:

<distributionManagement>
  <repository>
    <id>company-repository</id>
    <name>company m2 repository</name>
    <url>scp://deploy@company.server.com/var/www/repository</url>
  </repository>
</distributionManagement>

Example uses scp, few other protocols are supported.

And for maven-release-plugin we also need scm settings in place:

<scm>
  <connection>scm:hg:ssh://hg@company.server.com/hg/project</connection>
  <developerConnection>scm:hg:ssh://hg@company.server.com/hg/project</developerConnection>
</scm>

Deploy

Fine, lets deploy a SNAPSHOT release:

mvn clean deploy

This will clean, build and deploy artifact with current version (event if it is not marked as a SNAPSHOT). Note that it will also overwrite existing artifacts in repository if they exist.

Release

The release plugin is a lot smarter. The release process is divided in two steps — prepare and perform. Prepare works something like this (assuming artifactId is “zeeba”):

  • Updates current artifact version from 1.0-SNAPSHOT to 1.0
  • Creates a tag zeeba-1.0
  • Updates the version from 1.0 to 1.1-SNAPSHOT

After each of those tasks release:prepare also commits and pushes the change to scm. So after single release:prepare you’ll get 3 change-sets in scm

The perform side of the plugin is cloning repository based on saved release scm tag name in release.properties, rebuilding (goal ‘deploy’) and finally performing the actual release. So if release:perform fails it can be safely retried until it succeeds.

mvn release:prepare
mvn release:perform

Note, this plugin requires project to currently have SNAPSHOT version.

publishing/sonatype_maven_book.jpeg And I can’t recommend more the Maven: The Definitive Guide.
It is really good getting started guide for newcomers to maven.
 
Internet Explorer 6
Are you serious?