© Balaji Varanasi 2019
B. VaranasiIntroducing Mavenhttps://doi.org/10.1007/978-1-4842-5410-3_8

8. Maven Release

Balaji Varanasi1 
(1)
Salt Lake City, UT, USA
 

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.

Launch your command line in administrator mode and navigate to the bin folder located under C: ools exus exus-3.18.1-01. Then run the command nexus /install Nexus_Repo_Manager. You will see the success message as illustrated in Figure 8-1.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig1_HTML.jpg
Figure 8-1

Success message when installing Nexus

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.

On the same command line, run the command nexus start to launch Nexus. Figure 8-2 shows the result of running this command.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig2_HTML.jpg
Figure 8-2

Starting Nexus

By default, Nexus runs on port 8081. Launch a web browser and navigate to Nexus at http://localhost:8081/. It will take several minutes, but eventually you should see the Nexus launch screen as shown in Figure 8-3.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig3_HTML.jpg
Figure 8-3

Nexus launch screen

Click the “Sign In” link on the top-right corner to log in to Nexus. You will be presented with a login modal containing the location to the file with autogenerated admin password as shown in Figure 8-4.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig4_HTML.png
Figure 8-4

Nexus login modal

Log in to Nexus with the username admin and password copied from admin.password file. You will be asked to change the password as shown in Figure 8-5. For the exercises in this book, I changed the password to admin123.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig5_HTML.jpg
Figure 8-5

Nexus change password screen

Now that Nexus is installed and running, let’s modify the gwsm project located under C:apressgswm-bookchapter8. You will start by adding a distributionManagement element in the pom.xml file, as shown in Listing 8-1. This element is used to provide repository information on where the project’s artifacts will be deployed. The repository subelement indicates the location where the released artifacts will be deployed. Similarly, the snapshotRepository element identifies the location where the SNAPSHOT versions of the project will be stored.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <dependencies>
            <!-- Content removed for brevity -->
        </dependencies>
        <distributionManagement>
           <repository>
              <id>nexusReleases</id>
              <name>Releases</name>
        <url>http://localhost:8081/repository/maven-releases</url>
           </repository>
           <snapshotRepository>
              <id>nexusSnapshots</id>
              <name>Snapshots</name>
              <url>http://localhost:8081/repository/maven-snapshots</url>
           </snapshotRepository>
        </distributionManagement>
        <build>
            <!-- Content removed for brevity -->
        </build>
</project>
Listing 8-1

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.

Like most repository managers, deployment to Nexus is a protected operation. For Maven to interact and deploy artifacts on Nexus, you need to provide user with the right access roles in the settings.xml file. Listing 8-2 shows the settings.xml file with the server information. As you can see, we are using admin user information to connect to Nexus. Notice that the IDs declared in the server tag – nexusReleases and nexusSnapshots – must match the IDs of the repository and snapshotRepository declared in the pom.xml file. Replace the contents of the settings.xml file in the C:Users<<USER_NAME>>.m2 folder with the code in Listing 8-2.
<?xml version="1.0" encoding="UTF-8" ?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
   <server>
      <id>nexusReleases</id>
      <username>admin</username>
      <password>admin123</password>
   </server>
   <server>
      <id>nexusSnapshots</id>
      <username>admin</username>
      <password>admin123</password>
   </server>
</servers>
</settings>
Listing 8-2

Settings.xml File with Server Information

This concludes the configuration steps for interacting with Nexus. At the command line, run the command mvn deploy under the directory C:apressgswm-bookchapter8gswm. Upon successful execution of the command, you will see the SNAPSHOT artifact under Nexus at http://localhost:8081/#browse/browse:maven-snapshots, as shown in Figure 8-6.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig6_HTML.jpg
Figure 8-6

SNAPSHOT artifact under Nexus

Project Release

Releasing a project is a complex process, and it typically involves the following steps:
  • 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.

Maven has a release plug-in that provides a standard mechanism for executing the preceding steps and releasing project artifacts. As you can see, as part of its release process, Maven heavily interacts with the source control system. In this section, you will be using Git as the source controls system and GitHub as the remote server that houses repositories. A typical interaction between Maven and GitHub is shown in Figure 8-7. Maven releases are typically performed on a developer or build machine. Maven requires Git client to be installed on such machines. These command-line tools allow Maven to interact with GitHub and perform operations such as checking out code, creating tags, and so forth.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig7_HTML.jpg
Figure 8-7

Interaction between Maven and GitHub

Before we delve deeper into the Maven release process, you need to set up the environment by completing the following steps:
  1. 1.

    Install Git client on your local machine.

     
  2. 2.

    Create a new remote repository on GitHub.

     
  3. 3.

    Check the project you will be using into the remote repository.

     

Git Client Installation

There are several Git clients that make it easy to interact with Git repositories. Popular ones include SourceTree ( www.sourcetreeapp.com/ ) and GitHub Desktop ( https://desktop.github.com/ ). In this book, we will be using the client that comes with Git SCM distribution. Navigate to https://git-scm.com/downloads and download the Windows version of Git distribution. Double-click the downloaded exe file and accept the default installation options. After the installation is complete, open a new command-line window and type git --version. You should see a message similar to Figure 8-8.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig8_HTML.jpg
Figure 8-8

Git version

Creating a GitHub Repository

GitHub is a collaborative development platform that allows you to host public and private Git repositories for free. Before you can create a new repository on GitHub, you need to create an account at https://github.com/join . Once you have logged into GitHub using your credentials, navigate to https://github.com/new and create a new repository as shown in Figure 8-9.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig9_HTML.jpg
Figure 8-9

New GitHub repository

Checking in Source Code

The final step in getting your environment ready for Maven release is checking in the gswm project under C:apressgswm-bookchapter8gswm to the newly created remove repository. Using your command line, navigate to the C:apressgswm-bookchapter8gswm folder and run the following commands sequentially. Make sure you use the right remote repository URL by replacing your GitHub account in the following remote add command:
  • 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

The Git push command will prompt you for your GitHub username and password. Successful completion of the push command should give the output shown in Figure 8-10.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig10_HTML.jpg
Figure 8-10

Output from the Git initial commit

Using your browser, navigate to your remote repository on GitHub and you will see the checked-in code. Figure 8-11 shows the expected browser screen.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig11_HTML.jpg
Figure 8-11

Project checked into GitHub

The preceding commands have pushed the code into the mater branch on GitHub. However, Maven release plug-in interacts with the code in the release branch. So, the final step in this setup is to create a new local release branch and push it to GitHub by running the following commands:
  • 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

The prepare goal , as the name suggests, prepares a project for release. As part of this stage, Maven performs the following operations:
  • 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.

To facilitate this, you would provide the SCM information in the project’s pom.xml file. Listing 8-3 shows the pom.xml file snippet with the SCM information.
<project>
  <modelVersion>4.0.0</modelVersion>
        <!-- Content removed for brevity -->
    <scm>
      <connection>scm:git:https://github.com/bava/intro-maven.git</connection>
      <developerConnection>scm:git:https://github.com/bava/intro-maven.git</developerConnection>
      <url>https://github.com/bava/intro-maven</url>
    </scm>
   <!-- Content removed for brevity -->
</project>
Listing 8-3

The pom.xml with SCM Information

Once you have updated the pom.xml file on your local machine, commit the modified file to GitHub by running the following commands:
git commit . -m "Added SCM Information"
git push origin release
In order for Maven to communicate successfully with the GitHub, it needs GitHub credentials. You provide that information in the settings.xml file, as shown in Listing 8-4. The ID for the server element is declared as GitHub , as it must match the hostname.
<?xml version="1.0" encoding="UTF-8" ?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
   <server>
      <id>nexusReleases</id>
      <username>admin</username>
      <password>admin123</password>
   </server>
   <server>
      <id>nexusSnapshots</id>
      <username>admin</username>
      <password>admin123</password>
   </server>
   <server>
     <id>github</id>
     <username>[your_github_account_name]</username>
     <password>[your_github_account_password]</password>
   </server>
 </servers>
</settings>
Listing 8-4

The settings.xml with GitHub Details

You now have all of the configuration required for Maven’s prepare goal. Listing 8-5 shows the results of running the prepare goal. Because the prepare goal was run in interactive mode, Maven will prompt you for the release version, release tag or label, and the new development version. Accept Maven’s proposed default values by pressing Enter for each prompt.
C:apressgswm-bookchapter8gswm>mvn release:prepare
[INFO] Scanning for projects...
[INFO]
[INFO] --------------< com.apress.gswmbook:gswm >--------------
[INFO] Building Getting Started with Maven 1.0.0-SNAPSHOT
[INFO] --- maven-release-plugin:2.5.3:prepare (default-cli) @ gswm ---
[INFO] Verifying that there are no local modifications...
[INFO] Executing: cmd.exe /X /C "git rev-parse --show-toplevel"
[INFO] Working directory: C:apressgswm-bookchapter8gswm
[INFO] Executing: cmd.exe /X /C "git status --porcelain ."
What is the release version for "Getting Started with Maven"? (com.apress.gswmbook:gswm) 1.0.0: :
What is SCM release tag or label for "Getting Started with Maven"? (com.apress.gswmbook:gswm) gswm-1.0.0: :
What is the new development version for "Getting Started with Maven"? (com.apress.gswmbook:gswm) 1.0.1-SNAPSHOT: :
[INFO] Checking in modified POMs...
[INFO] Tagging release with the label gswm-1.0.0...
[INFO] Executing: cmd.exe /X /C "git tag -F C:UsersavaraAppDataLocalTempmaven-scm-73613791.commit gswm-1.0.0"
[INFO] Executing: cmd.exe /X /C "git push https://github.com/bava/intro-maven.git refs/tags/gswm-1.0.0"
[INFO] Release preparation complete.
[INFO] BUILD SUCCESS
Listing 8-5

Maven prepare Command

Notice the Git commands getting executed as part of the prepare goal. Successful completion of the prepare goal will result in the creation of a Git tag, as shown in Figure 8-12. The pom.xml file in the gswm project will now have version 1.0.1-SNAPSHOT.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig12_HTML.jpg
Figure 8-12

Git tag created upon prepare execution

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.

The following phases are executed as part of perform goal:
  • 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.

The output of running the perform goal on gswm project is shown in Listing 8-6.
C:apressgswm-bookchapter8gswm>mvn release:perform
[INFO] Scanning for projects...
[INFO] -------------< com.apress.gswmbook:gswm >---------------
[INFO] Building Getting Started with Maven 1.0.1-SNAPSHOT
[INFO] --------------------[ jar ]-----------------------------
[INFO] --- maven-release-plugin:2.5.3:perform (default-cli) @ gswm ---
[INFO] Checking out the project to perform the release ...
[INFO] Executing: cmd.exe /X /C "git clone --branch gswm-1.0.0 https://github.com/bava/intro-maven.git C:apressgswm-bookchapter8gswm argetcheckout"
[INFO] Invoking perform goals in directory C:apressgswm-bookchapter8gswm argetcheckout
[INFO] Executing goals 'deploy'...
 [INFO] Building jar: C:apressgswm-bookchapter8gswm argetcheckout argetgswm-1.0.0-javadoc.jar
 [INFO] --- maven-install-plugin:2.4:install (default-install) @ gswm ---
 [INFO] Installing C:apressgswm-bookchapter8gswm argetcheckout argetgswm-1.0.0.jar to C:Usersavara.m2 epositorycomapressgswmbookgswm1.0.0gswm-1.0.0.jar
 [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ gswm ---
[INFO] Uploading to nexusReleases: http://localhost:8081/repository/maven-releases/com/apress/gswmbook/gswm/1.0.0/gswm-1.0.0.jar
[INFO] Uploaded to nexusReleases: http://localhost:8081/repository/maven-releases/com/apress/gswmbook/gswm/1.0.0/gswm-1.0.0.jar (2.4 kB at 14 kB/s)
[INFO] Uploading to nexusReleases: http://localhost:8081/repository/maven-releases/com/apress/gswmbook/gswm/1.0.0/gswm-1.0.0.pom
[INFO] Uploaded to nexusReleases: http://localhost:8081/repository/maven-releases/com/apress/gswmbook/gswm/1.0.0/gswm-1.0.0-javadoc.jar (22 kB at 84 kB/s)
[INFO] BUILD SUCCESS
Listing 8-6

Maven perform Command

This completes the release of the 1.0.0 version of the gswm project. The artifact ends up in the Nexus repository manager, as shown in Figure 8-13.
../images/332298_2_En_8_Chapter/332298_2_En_8_Fig13_HTML.jpg
Figure 8-13

Nexus with released artifact

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.

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

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