对于java的一些公用依赖包,我们通常需要发布在私有的nexus平台中,以便其他项目组使用。
1)我们首先需要搭建自己的nexus平台。
2)用户授权,指定用户具有release权限。
3)调整maven中的setting.xml文件。
4)调整project中的pom.xml文件。
1、setting.xml配置样例:
<?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">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
<mirrors>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus_public</id>
<name>Team Repository</name>
<url>http://nexus.test.com.cn/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true></enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<url>http://nexus.test.com.cn/nexus/content/repositories/snapshots</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus_public</id>
<url>http://nexus.test.com.cn/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
<server>标签中配置nexus服务器端用户授权信息,通常我们需要配置两个:releases和snapshots,当然如果你的jar不需要进行snapshots发布,也可以不用配置。<profile>中配置project中引用jar时需要访问nexus服务器的方式。
2、pom.xml样例
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<description>jdbc utils</description>
<groupId>com.test.utils</groupId>
<artifactId>jdbc-utils</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jdbc-utils</name>
<properties>
<jedis.version />
<java.version>1.8</java.version>
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<scm>
<developerConnection>scm:git:ssh://git@code.test.com.cn:3590/yourname/jdbc-utils.git</developerConnection>
<url>scm:git:ssh://git@code.test.com.cn:3590/yourname/jdbc-utils.git</url>
<connection>scm:git:ssh://git@code.test.com.cn:3590/yourname/jdbc-utils.git</connection>
<!-- 此git地址上生成tag标签名,在release:prepare时会提示输入,此处可以不用配置 -->
<tag>jdbc-utils-1.0</tag>
</scm>
<distributionManagement>
<!-- if you use public-release -->
<repository>
<id>releases</id>
<name>releases</name>
<url>http://nexus.test.com.cn/nexus/content/repositories/releases/</url>
</repository>
<!-- if you use snapshots. -->
<snapshotRepository>
<id>snapshots</id>
<name>snapshots</name>
<url>http://nexus.test.com.cn/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
</project>
1)项目的版本需要以“-SNAPSHOT”结尾,否则在release时将会抛出:
You don't have a SNAPSHOT project in the reactor projects list.
2)项目中需要配置“maven-release-plugin”,否则将无法执行命令,且此plugin的版本最好是最新的、且与maven的版本是兼容的。
3)<scm>标签中配置你的git地址,而且具有此git地址的“push”等权限。
4)<distributionManagement>标签中,配置需要进行代码release的地址,包括“releases”和“snapshots”(可选),<id>的值必须与setting.xml中<server>.<id>对应。此处需要非常注意的是:<server>中配置的用户需要具有pom.xml中两个地址的代码提交权限。否则将会抛出:
Return code is: 401, ReasonPhrase: Unauthorized.
5)为了避免不必要的问题,我们还需要将pom.xml中对应的两个url的访问权限按下图所示进行修改(在nexus平台上):
如果没有修改,极有可能抛出:
Return code is: 400, ReasonPhrase: Bad Request.
3、执行:
对于SCM发布,我们通常关注三个指令(参见maven-release-plugin):
1)mvn release:clean:清理release过程中产生的临时文件,通常当我们在release执行的任何过程中出现错误,或者放弃release时,执行此命令,类似于一个“回滚”的操作。
2)mvn release:rollback:回滚操作,当release操作执行一部分之后,无论是否发生错误,如果你希望回滚到release之前的代码状态,可以执行此操作。(注意,release:prepare阶段执行过程中,会修改pom.xml文件,rollback即表示回滚到prepare之前的状态)
3)mvn release:prepare:release的准备阶段,也是正常流的第一个操作,此阶段将会提示操作者本次release的版本号、下一次开发的SNAPSHOT版本号、生成tag的名称等,我们通常是一路回车,一切操作保持默认即可。
4)mvn release:perform:执行release远程发布,执行成功后,我们应该会在nexus响应的url中查看到我们的jar列表。
5)每次发布,都会在scm指定的git地址上生成一个tag,这个tag号与你的version保持一致,如果你意外出错,重新release相同版本时可能不会成功,因为此tag已经存在,此时你需要执行“git tag -d <你的tag名称>”来删除tag才能继续。(可以通过“git tag”查看tag的列表)