Maven Release to Nexus – Maven发布到Nexus

最后修改: 2013年 5月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In the previous article of this series, we set up a deployment process with Maven to Nexus. In this article, we’ll configure the Release Process with Maven – both in the pom of the project as well as in a Jenkins job.

在本系列的上一篇文章中,我们设置了用Maven部署到Nexus的过程。本文中,我们将在项目的pom以及Jenkins作业中配置Maven的发布流程。

2. Repository in the pom

2.在pom中的存储库

In order for Maven to be able to release to a Nexus Repository Server, we need to define the repository information via the distributionManagement element:

为了让Maven能够向Nexus存储库服务器发布,我们需要通过distributionManagement元素来定义存储库信息。

<distributionManagement>
   <repository>
      <id>nexus-releases</id>
      <url>http://localhost:8081/nexus/content/repositories/releases</url>
   </repository>
</distributionManagement>

The hosted Releases Repository comes out of the box on Nexus, so there is no need to create it explicitly.

托管的发布库在Nexus上是开箱即用的,所以不需要明确创建它。

3. SCM in the Maven pom

3.Maven pom中的SCM

The Release process will interact with the Source Control of the project – this means we first need to define the <scm> element in our pom.xml:

发布过程将与项目的源代码控制互动–这意味着我们首先需要在我们的<scm>元素中定义pom.xml

<scm>
   <connection>scm:git:https://github.com/user/project.git</connection>
   <url>http://github.com/user/project</url>
   <developerConnection>scm:git:https://github.com/user/project.git</developerConnection>
</scm>

Or, using the git protocol:

或者,使用git协议。

<scm>
   <connection>scm:git:git@github.com:user/project.git</connection>
   <url>scm:git:git@github.com:user/project.git</url>
   <developerConnection>scm:git:git@github.com:user/project.git</developerConnection>
</scm>

4. The Release Plugin

4.发布插件

The standard Maven plugin used by a Release Process is the maven-release-plugin – the configuration for this plugin is minimal:

发布流程使用的标准Maven插件是maven-release-plugin–该插件的配置很简单。

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-release-plugin</artifactId>
   <version>2.4.2</version>
   <configuration>
      <tagNameFormat>v@{project.version}</tagNameFormat>
      <autoVersionSubmodules>true</autoVersionSubmodules>
      <releaseProfiles>releases</releaseProfiles>
   </configuration>
</plugin>

What is important here is that the releaseProfiles configuration will actually force a Maven profile – the releases profile – to become active during the Release process.

重要的是,releaseProfiles配置实际上会在发布过程中强迫一个Maven配置文件–releases配置文件–变得活跃。

It is in this process that the nexus-staging-maven-plugin is used to perform a deploy to the nexus-releases Nexus repository:

在这个过程中,nexus-staging-maven-plugin被用来向nexus-releases Nexus仓库进行部署。

<profiles>
   <profile>
      <id>releases</id>
      <build>
         <plugins>
            <plugin>
               <groupId>org.sonatype.plugins</groupId>
               <artifactId>nexus-staging-maven-plugin</artifactId>
               <version>1.5.1</version>
               <executions>
                  <execution>
                     <id>default-deploy</id>
                     <phase>deploy</phase>
                     <goals>
                        <goal>deploy</goal>
                     </goals>
                  </execution>
               </executions>
               <configuration>
                  <serverId>nexus-releases</serverId>
                  <nexusUrl>http://localhost:8081/nexus/</nexusUrl>
                  <skipStaging>true</skipStaging>
               </configuration>
            </plugin>
         </plugins>
      </build>
   </profile>
</profiles>

The plugin is configured to perform the Release process without the staging mechanism, same as previously, for the Deployment process (skipStaging=true).

该插件被配置为在没有暂存机制的情况下执行发布流程,与之前的一样,用于部署流程(skipStaging=true)。

And also similar to the Deployment process, Releasing to Nexus is a secured operation – so we’re going to use the Out of the Box deployment user form Nexus again.

而且与部署过程类似,释放到Nexus是一个安全的操作–所以我们要再次使用Out of the Box deployment用户表单Nexus。

We also need to configure the credentials for the nexus-releases server in the global settings.xml (%USER_HOME%/.m2/settings.xml):

我们还需要在全局的settings.xml%USER_HOME%/.m2/settings.xml)中配置nexus-releases服务器的凭证。

<servers>
   <server>
      <id>nexus-releases</id>
      <username>deployment</username>
      <password>the_pass_for_the_deployment_user</password>
   </server>
</servers>

This is the full configuration

这是完整的配置

5. The Release Process

5.释放过程

Let’s break apart the release process into small and focused steps. We are performing a Release when the current version of the project is a SNAPSHOT version – say 0.1-SNAPSHOT.

让我们把发布过程分解成几个小的、有针对性的步骤。当项目的当前版本是SNAPSHOT版本时,我们要进行发布,比如0.1-SNAPSHOT

5.1. Release:Clean

5.1.发布:干净

Cleaning a Release will:

清理释放将。

  • delete the release descriptor (release.properties)
  • delete any backup POM files

5.2. release:prepare

5.2.发布:准备

Next part of the Release process is Preparing the Release; this will:

发布过程的下一部分是准备发布;这将。

  • perform some checks – there should be no uncommitted changes and the project should depend on no SNAPSHOT dependencies
  • change the version of the project in the pom file to a full release number (remove SNAPSHOT suffix) – in our example – 0.1
  • run the project test suites
  • commit and push the changes
  • create the tag out of this non-SNAPSHOT versioned code
  • increase the version of the project in the pom – in our example – 0.2-SNAPSHOT
  • commit and push the changes

5.3. release:perform

5.3.发布:执行

The latter part of the Release process is Performing the Release; this will:

发布过程的后半部分是执行发布;这将。

  • checkout release tag from SCM
  • build and deploy released code

This second step of the process relies on the output of the Prepare step – the release.properties.

这个过程的第二步依赖于 “准备 “步骤的输出–release.properties

6. On Jenkins

6.关于詹金斯

Jenkins can perform the release process in one of two ways – it can either use it’s own release plugins, or it can simply run perform the release with a standard maven job running the correct release steps.

Jenkins可以通过两种方式之一执行发布过程–它可以使用自己的发布插件,也可以简单地通过标准的maven作业运行正确的发布步骤来执行发布。

The existing Jenkins plugins focused on the release process are:

现有的专注于发布过程的Jenkins插件有。

However, since the Maven command for performing the release is simple enough, we can simply define a standard Jenkins job to perform the operation – no plugins necessary.

然而,由于执行发布的Maven命令足够简单,我们可以简单地定义一个标准的Jenkins作业来执行该操作–不需要插件。

So, for a new Jenkins job (Build a maven2/3 project) – we”ll define 2 String parameters: releaseVersion=0.1 and developmentVersion=0.2-SNAPSHOT.

因此,对于一个新的Jenkins作业(构建一个maven2/3项目)–我们将定义两个字符串参数。releaseVersion=0.1developmentVersion=0.2-SNAPSHOT

At the Build configuration section, we can simply configure the following Maven command to run:

Build配置部分,我们可以简单配置以下Maven命令的运行。

Release:Clean release:prepare release:perform 
   -DreleaseVersion=${releaseVersion} -DdevelopmentVersion=${developmentVersion}

When running a parametrized job, Jenkins will prompt the user to specify values for these parameters – so each time we run the job we need to fill in the right values for releaseVersion and developmentVersion.

当运行一个参数化的作业时,Jenkins会提示用户指定这些参数的值–所以每次运行作业时,我们都需要为releaseVersiondevelopmentVersion填上正确的值。

Also, it’s worth using the Workspace Cleanup Plugin and check the Delete workspace before build starts option for this build. However keep in mind that the perform step of the Release should necessarily be run by the same command as the preparestep – this is because the latter perform step will use the release.properties file created by prepare. This means that we cannot have a Jenkins Job running prepare and another running perform.

此外,值得使用Workspace Cleanup Plugin,并为该构建勾选Delete workspace before build starts选项。但是请记住,Release的perform步骤必须由prepare步骤的同一命令运行–这是因为后者的perform步骤将使用由prepare创建的release.properties>文件。这意味着我们不能让一个Jenkins作业运行prepare和另一个运行perform

7. Conclusion

7.结论

This article showed how to implement the process of Releasing a Maven project with or without Jenkins. Similar to Deployment, this process is using the nexus-staging-maven-plugin to interact with Nexus and focuses on a git project.

本文展示了如何在有无Jenkins的情况下实现发布Maven项目的过程。与部署类似,该过程使用nexus-staging-maven-plugin与Nexus进行交互,重点关注一个git项目。