Releasing a Maven project

The ultimate goal of any project is the release. After development is complete and bugs are fixed, it is time to release the project. Different projects are released in different ways. Web projects are released by deploying them to the web server. Other projects may be packaged into executable JARs. Still others may be packaged as executables or installers. If the project is a library or a dependency used in other projects, then it needs to be made available suitably.

As we have seen before, we use the SNAPSHOT version during development. When the project has to be released, this version now needs to be replaced with a concrete version.

One of the most advanced features of Maven is its support to do a project release. Let us explore this.

How to do it...

  1. Open a project for which you want to do a release (project-with-release).
  2. Verify if the SCM details are present in the pom file:
    <scm>
          <developerConnection>scm:git:https://bitbucket.org/maruhgar/apache-maven-cookbook</developerConnection>
           <url>https://bitbucket.org/maruhgar/apache-maven-cookbook</url>
        <tag>HEAD</tag>
      </scm>
  3. Add the plugin definition in order to specify the latest version of the plugin:
       <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.5.1</version>
          </plugin>
        </plugins>
  4. Run the following Maven command:
    mvn release:prepare –DpushChanges=false
    

    By default, changes made by the plugin are pushed to the repository. If you do not want that, set the pushChanges configuration option to false.

  5. Choose the default values when prompted.
    How to do it...

    You could choose the default values for the release version, the SCM tag and new development version, or provide your values.

  6. Observe the output:
    How to do it...

    Maven runs a number of commands that modify the pom file. Then, it checks in the changes into the repository.

  7. Now run the following command:
    mvn release:perform –Dgoals=package –DlocalCheckout=true
    

    By default, the perform goal of the Maven Release plugin runs the deploy goal to deploy the project to the specified repository. If you do not have a remote repository to deploy to, or want to run a different goal as part of the release, you can specify it using the goals configuration. In the preceding case, we have set it to run the package goal.

    Also, to do the release, Maven checks out the tag created by the prepare goal from the repository. If you want Maven to check out the local copy instead, you could do so by setting the localCheckout configuration to true.

  8. Observe the output:
    How to do it...
  9. Ensure that the release binaries are created in the target/checkout/project-with-release/target folder:
    How to do it...

How it works...

There are two steps to making a release—prepare and perform.

When the prepare goal of the Maven Release plugin is run, it does the following:

  • Checks there are no uncommitted changes
  • Checks that the project does not have any SNAPSHOT dependencies
  • Changes the version of the SNAPSHOT project; you will be prompted to confirm or override the default
  • Adds a tag element to the scm element and computes the value (by default, HEAD)
  • Runs the verify goal to ensure that the changes do not break anything
  • Commits the modified pom to the SCM
  • Tags the code in SCM with a version name (you will be prompted to confirm or override the default):
    How it works...
  • Bumps the version in the pom to the new SNAPSHOT value (from 1.0-SNAPSHOT; this would be 1.1-SNAPSHOT); you will be prompted to confirm or override this
  • Commits the modified pom to SCM

As you can see, once the goal is met, you will have an updated SCM with a tag with the release version and the HEAD with the next SNAPSHOT version. A release.properties file is also created. It contains information that is needed for the perform goal.

How it works...

The second platform does as follows:

  • The perform goal uses the information in release.properties to check out from the SCM tag that was created earlier
  • It then runs the specified goal on the checked-out project (by default, deploy)
  • This generates the release binaries

Once the build is successful, release.properties and other backup files created by the Release plugin are removed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.105.215