Maven Release in 5 Minutes

Maven Release in 5 Minutes

This is a very short summary of how Maven release works and how to configure it.

Prerequisite

You should know what Maven lifecycle, phase, goal and plugin is.

Release Life Cycle

maven-release-plugin does not bind to any phase. This means your project won’t be released when you run a phase. Instead, you have to explicitly run the goals of the plugin.

The two most important goals are:

  • release:prepare - create a release tag in the scm, create a release.properties file and backup files, and update the project version. Full description
  • release:perform - checkout the release tag and upload to a repository. Full description

Note
release:prepare is idempotent, so multiple runs doesn’t change anything. To re-prepare, add command line option -Dresume=false, which will delete release.properties and backup files.
Alternatively, you can run mvn release:clean release:prepare.

Perform Release

What is performed during release can be configured with the goals configuration.

E.g., if you want to upload artifacts to Sonatype, you can add nexus-staging-maven-plugin, which will perform the upload in deploy phase and release (to Maven central) with nexus-staging:release, like this:

<plugin>
  <groupId>org.sonatype.plugins</groupId>
  <artifactId>nexus-staging-maven-plugin</artifactId>
  <version>1.6.6</version>
  <!-- This plugin is an extension to the deploy phase, adding a new deployment type -->
  <extensions>true</extensions>
  <configuration>
    <!-- The server ID configured in settings.xml  -->
    <serverId>ossrh-server</serverId>
    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
    <!-- Not important for snapshots -->
    <autoReleaseAfterClose>false</autoReleaseAfterClose>
  </configuration>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-release-plugin</artifactId>
  <version>2.5.3</version>
  <configuration>
    <autoVersionSubmodules>true</autoVersionSubmodules>
    <useReleaseProfile>false</useReleaseProfile>
    <releaseProfiles>release</releaseProfiles>
    <!-- This will release the artifacts after staging them -->
    <goals>deploy nexus-staging:release</goals>
  </configuration>
</plugin>

Configure SCM Provider

Maven already supports many scm systems. The only thing you need to do is to make sure the <scm> tag is configured correctly. The release plugin uses it to pull and push changes.

Here’s an example for git:

<scm>
  <connection>scm:git:git@github.com:ryan-ju/resteasy-demo-01.git</connection>
  <developerConnection>scm:git:git@github.com:ryan-ju/resteasy-demo-01.git</developerConnection>
  <url>git@github.com:ryan-ju/resteasy-demo-01.git</url>
  <tag>HEAD</tag>
</scm>
Tag Meaning
<connection> read-only
<developerConnection> read-write
<url> publicly visible
<tag> the tag to checkout to make the release

Rollback Release

release:rollback allows you to rollback a release, but only if you haven’t run release:clean. More details

Generate Release POM

release:prepare-with-pom generates a release-pom.xml, which contains all resolved dependencies, so all dependencies (including transitive) are explicitly defined.

Dry Run

All goals can be dry run with -DdryRun=true. This will still generate the properties and backup files, but won’t change any code or commit anything.

References

maven-release-plugin doc: http://maven.apache.org/maven-release/maven-release-plugin/index.html
Maven scm overview: https://maven.apache.org/scm/scms-overview.html
Maven scm providers: https://maven.apache.org/scm/maven-scm-providers
Maven <scm><tag>: http://stackoverflow.com/a/23719418/842860
Maven <scm> tag: http://stackoverflow.com/a/9518682/842860

Written with StackEdit.

No comments:

Post a Comment