Maven provides the release plugin that automates steps involved with releasing software. Before we deep dive into the Maven release process, we will set up and configure Nexus repository and use Maven to publish artifacts to Nexus.
Integration with Nexus
Repository managers are a key part of Maven deployment in enterprises. Repository managers act as a proxy of public repositories, facilitate artifact sharing and team collaboration, ensure build stability, and enable the governance of artifacts used in the enterprise.
Sonatype Nexus repository manager is a popular open source software that allows you to maintain internal repositories and access external repositories. It allows repositories to be grouped and accessed via a single URL. This enables the repository administrator to add and remove new repositories behind the scenes without requiring developers to change the configuration on their computers. Additionally, it provides hosting capabilities for sites generated using Maven site and artifact search capabilities.
Before we look at integrating Maven with Nexus, you will need to install Nexus on your local machine. Nexus is distributed as an archive, and it comes bundled with a Jetty instance. Download the Nexus distribution (.zip version for Windows) from Sonatype’s web site at https://help.sonatype.com/repomanager3/download . At the time of this writing, version 3.18.1-01 of Nexus is available. Unzip the file, and place the contents on your machine. In this book, we assume the contents to be under C: ools exus folder.
Note
Most enterprises typically have repository managers installed and available on a central server. If you already have access to a repository manager, skip this part of the installation.
Note
Nexus 3.18 requires JRE 8 to function properly. Make sure you have version 8 of JDK/JRE installed on your local machine. Also, make sure that JAVA_HOME is pointing to version 8 of the JDK.
The pom.xml with distributionManagement
Note
Out of the box, Nexus comes with Releases and Snapshots repositories. By default, SNAPSHOT artifacts will be stored in the Snapshots Repository, and release artifacts will be stored in the Releases repository.
Settings.xml File with Server Information
Project Release
Verify that there are no uncommitted changes on the local machine.
Remove SNAPSHOT from the version in the pom.xml file.
Make sure that project is not using any SNAPSHOT dependencies.
Check in the modified pom.xml file to your source control.
Create a source control tag of the source code.
Build a new version of the artifact, and deploy it to a repository manager.
Increment the version in the pom.xml file, and prepare for the next development cycle.
- 1.
Install Git client on your local machine.
- 2.
Create a new remote repository on GitHub.
- 3.
Check the project you will be using into the remote repository.
Git Client Installation
Creating a GitHub Repository
Checking in Source Code
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/<<your_git_hub_account>>/intro-maven.git
git push -u origin master
git checkout –b release
git push origin release
Maven Release
Releasing an artifact using Maven’s release process requires using two important goals: prepare and perform. Additionally, the release plug-in provides a clean goal that comes in handy when things go wrong.
Prepare Goal
check-poms: Checks that the version in the pom.xml file has SNAPSHOT in it.
scm-check-modifications: Checks if there are any uncommitted changes.
check-dependency-snapshots: Checks the pom file to see if there are any SNAPSHOT dependencies. It is a best practice for your project to use released dependencies. Any SNAPSHOT dependencies found in the pom.xml file will result in release failure.
map-release-versions: When prepare is run in an interactive mode, the user is prompted for a release version.
map-development-versions: When prepare is run in an interactive mode, the user is prompted for the next development version.
generate-release-poms: Generates the release pom file.
scm-commit-release: Commits the release of the pom file to the SCM.
scm-tag: Creates a release tag for the code in the SCM.
rewrite-poms-for-development: The pom file is updated for the new development cycle.
remove-release-poms: Deletes the pom file generated for the release.
scm-commit-development: Submits the pom.xml file with the development version.
end-release: Completes the prepare phase of the release.
The pom.xml with SCM Information
The settings.xml with GitHub Details
Maven prepare Command
Clean Goal
The prepare goal performs a lot of activities and generates temporary files, such as release.properties and pom.xml.releaseBackup, as part of its execution. Upon successful completion, it cleans up those temporary files. Sometimes the prepare goal might fail (e.g., is unable to connect to Git) and leave the project in a dirty state. This is where the release plug-in’s clean goal comes into the picture. As the name suggests, it deletes any temporary files generated as part of release execution.
Note
The release plug-in’s clean goal must be used only when the prepare goal fails.
Perform Goal
The perform goal is responsible for checking out code from the newly created tag and builds and deploys the released code into the remote repository.
verify-completed-prepare-phases: This validates that a prepare phase has been executed prior to running the perform goal.
checkout-project-from-scm: Checks out the released code from the SCM tag.
run-perform-goal: Executes the goals associated with perform. The default goal is deploy.
Maven perform Command
Summary
Internal repository managers such as Nexus allow enterprises to adopt Maven completely. In addition to serving as public repository proxies, they enable component sharing and governance. This chapter looked at integrating Maven with Nexus and walked you through the process of deploying an artifact to Nexus. You also learned Maven’s release process and its different phases.
In the next chapter, we will learn the concepts of continuous integration (CI) and install and configure Jenkins – a popular open source CI tool.