maven:创建阿里云私有maven仓库塈maven 命令行发布Release版本

最近在网上查找才发现阿里云提供了免费私有 maven 仓库,这可解决我的大问题了。
我们的大部分项目都是开源提供给用户的,这些项目我们都是发布到maven中央仓库,但也有部分内部使用的jar包,我们不希望发布到maven中央仓库,但也需要有发布版本管理,自己搭安装Nexus搭建Maven私有仓库总是有些麻烦,维护工作也不少。现在有阿里云提供的免费私有 maven 仓库正好能解决我们的问题。

打开下面的页面,使用支付宝或钉钉APP扫码认证就登录进来了,
https://packages.aliyun.com/maven

只需要按照提示绑定邮箱,简单的填写一个公司名称就可以了,也不需要做更多的验证。
在这里插入图片描述
点击生产库-release非生产库-snapshot找到Maven方式如下图就可以看到详细的设置说明:
在这里插入图片描述

settings.xml

如上图在$HOME/.m2/文件目录下settings.xml
找到<servers></servers>段添加如下用户登录信息。

  <server>
    <id>rdc-releases</id>
    <username>************************</username>
    <password>************</password>
  </server>
  <server>
    <id>rdc-snapshots</id>
    <username>************************</username>
    <password>************</password>
  </server>
  <!-- 插件仓库(release)访问用户信息-->
  <server>
    <id>rdc-plugin-releases</id>
    <username>************************</username>
    <password>************</password>
  </server>
  <!-- 插件仓库(snapshot)访问用户信息-->
  <server>
    <id>rdc-plugin-snapshots</id>
    <username>************************</username>
    <password>************</password>
  </server>

找到<mirrors></mirrors>段添加如下镜像信息,以便能更快速的下载jar包

	  <mirror>
	    <id>mirror</id>
	    <mirrorOf>central,jcenter,!rdc-releases,!rdc-snapshots</mirrorOf>
	    <name>mirror</name>
	    <url>https://maven.aliyun.com/nexus/content/groups/public</url>
	  </mirror>

找到<profiles></profiles>段添加如下profile,以增加私有仓库,这样你的其他引用私有仓库的库的项目才能正确找到私有仓库的jar包

    <profile>      
        <id>rdc_repo</id>      
        <activation>      
            <activeByDefault>true</activeByDefault>      
        </activation>      
		<repositories>
		  <repository>
		    <id>rdc-releases</id>
		    <url>https://packages.aliyun.com/maven/repository/2097834-release-WG4jQB/</url>
		    <releases>
		      <enabled>true</enabled>
		    </releases>
		    <snapshots>
		      <enabled>false</enabled>
		    </snapshots>
		  </repository>
		  <repository>
		    <id>rdc-snapshots</id>
		    <url>https://packages.aliyun.com/maven/repository/2097834-snapshot-p3z4YQ/</url>
		    <releases>
		      <enabled>false</enabled>
		      </releases>
		    <snapshots>
		      <enabled>true</enabled>
		    </snapshots>
		  </repository>
		</repositories>
		<!-- 插件仓库位置 -->
		<pluginRepositories>
		  <pluginRepository>
		    <id>rdc-plugin-releases</id>
		    <name>aliyun plugins release repository</name>
		    <url>https://packages.aliyun.com/maven/repository/2097834-release-WG4jQB/</url>
		    <releases>
		      <enabled>true</enabled>
		    </releases>
		    <snapshots>
		      <enabled>false</enabled>
		    </snapshots>
		  </pluginRepository>
		  <pluginRepository>
		    <id>rdc-plugin-snapshots</id>
		    <name>aliyun plugins snapshot repository</name>
		    <url>https://packages.aliyun.com/maven/repository/2097834-snapshot-p3z4YQ/</url>
		    <releases>
		      <enabled>false</enabled>
		    </releases>
		    <snapshots>
		      <enabled>true</enabled>
		    </snapshots>
		  </pluginRepository>
		</pluginRepositories>
    </profile>

settings.xml部分的修改就算完成了,下一步就是要修改自己项目的pom.xml以实现指定上传仓库位置及项目发布

pom.xml

如果你的项目都是要发布到私有仓库的,按照官方提供的方法下图一样setttings.xml中定义制品上传配置肯定是更方便的。
在这里插入图片描述
因为我们的项目有的需要上传maven中央仓库,个别需要发布到私有仓库,并不是全部都需要向私有仓库发布,所以对于我们来说在需要发布到私有仓库的项目的pom.xml中定义制品上传配置才是更合适的。
设置步骤如下:
如上图找到自己的私有仓库的Release版本上传仓库位置Snapshot(快照)版本上传仓库位置,按如下格式在自己的pom.xml中增加<distributionManagement></distributionManagement>段,定义自己的私有仓库的上传位置。

如果你的maven项目有子项目,只需要修改所有子项目共同引用的根项目的pom.xml就可以(通常就是项目根文件夹下的pom.xml),下同

	<!-- 指定发布的仓库位置 -->
	<distributionManagement>
		<repository>
			<id>rdc-releases</id>
			<url>https://packages.aliyun.com/maven/repository/xxxxxxx-release-xxxxxx</url>
		</repository>
		<snapshotRepository>
			<id>rdc-snapshots</id>
			<url>https://packages.aliyun.com/maven/repository/xxxxxxx-snapshot-xxxxxx</url>
		</snapshotRepository>
	</distributionManagement>

到目前为止,你已经可以执行mvn deploy来发布产品了。

但你的制品文件具体推送到哪个库,取决于你的项目pom.xml文件中<version></version>字段的版本后缀是否为-SNAPSHOT,如果后缀为-SNAPSHOT则推送到快照版本库,否则推送到Release版本库。

release

对于经常要发布Release版本的我来说,这也挺麻烦的,有的时候一个项目中有几个子项目,所有的子项目中的pom.xml中的<version></version>字段都要手工修改,忙中出错在所难免。
其实呢,maven已经很贴心的提供了release插件帮助开发者很方便的发布版本

首先在你的pom.xml中添加如下字段定义

	<!--版本管理:定义你的项目的(git仓库位置)-->
	<scm>
		<connection>scm:git:https://gitee.com/xxxx/xxxx.git</connection>
		<developerConnection>scm:git:https://gitee.com/xxxx/xxxx.git</developerConnection>
		<url>https://gitee.com/xxxx/xxxx</url>
	</scm>
	<build>
		<pluginManagement>
			<plugins>
				<!-- release插件 -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-release-plugin</artifactId>
					<version>2.1</version>
					<configuration>
						<mavenExecutorId>forked-path</mavenExecutorId>
						<useReleaseProfile>false</useReleaseProfile>
						<arguments>-Paliyun-release</arguments>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
	<profiles>
		<!-- 定义发布到仓库的 source 和 javadoc -->
		<profile>
			<id>aliyun-release</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-source-plugin</artifactId>
						<version>2.1.2</version>
						<executions>
							<execution>
								<id>attach-sources</id>
								<goals>
									<goal>jar-no-fork</goal>
								</goals>
							</execution>
						</executions>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-javadoc-plugin</artifactId>
						<executions>
							<execution>
								<id>attach-javadocs</id>
								<goals>
									<goal>jar</goal>
								</goals>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>

有了上面的定义后,发布版本就变得很简单版本

先执行:

mvn release:prepare

完成发布准备工作,这里对当前项目状态有如下要求:

  • 要求你的本地git仓库必须是干净的,没有修改未提交的文件
  • 要求项目所有的依赖库的版本都必须是release版本(不能有-SNAPSHOT)
  • 要求项目的当前版本为快照版本(-SNAPSHOT)

否则会报错,无法进行下一步。在这个过程中,maven会自动修改项目的pom.xml中的版本号为Release版本(删除-SNAPSHOT)并添加git 标签 (${project_name}-${version}),推送到远程git仓库(push)

是不是省了好多事儿?

准备工作成功执行后然后再执行:

mvn release:perform

完成版本上传

猜你喜欢

转载自blog.csdn.net/10km/article/details/116000942