发布到maven center仓库
官方说明:
为了让Maven用户利用项目生成的构件,必须将它们部署到maven center repository。 许多开源项目希望允许使用Maven构建的项目用户透明地访问项目的构件。 为了实现这一点,项目应该将其构件部署到*存储库。
简单来说就是将我们自己写好的文件上传到maven*仓库,其他人想用就可以通过依赖的方式去引入。
需求:
-
releases: Only releases can be uploaded to the Central Repository, that means files that won’t change and that only depend on other files already released and available in the repository,(只能是正式包,并且上传的文件只能依赖于*仓库里面含有的依赖)
-
javadoc and sources for IDE lookup(提供Java文档和源代码,遵循Maven存储库格式),
格式命名:artifactId-version-classifier.packaging
例如:
example-application-1.4.7-sources.jar
example-application-1.4.7-javadoc.jar
-
PGP signature(一种签名,具体啥的还得百度下才知道),
所有部署的文件都需要使用GPG/PGP进行签名,每个文件都必须包含一个包含签名.asc文件。
例如:
example-application-1.4.7-sources.jar.asc
example-application-1.4.7-javadoc.jar.asc
-
minimum POM information: There are some requirements for the minimal information in the POMs that are in the Central Repository, see here(为了保证能用,对POM文件的一些要求),
Sufficient Metadata
作为部署的一部分,必须使用 Apache Maven来定义和构建项目对象模型
Correct Coordinates
按照groupId,artifactId,version(必须为正式包)来确定该Jar包在maven*仓库的位置
例如:
com.example.applications
example-application
1.4.7
Project Name, Description and URL
为了提高代码的可读性,必须提供名字,描述和URL
这里是项目名称哦 一般用 p r o j e c t . g r o u p I d : {project.groupId}: project.groupId:{project.artifactId}
项目的简单描述哦
http://www.example.com/example-application
License Information
必须要有许可证,就像你去别人家住,没有别人的许可,肯定也是住不了的哈
例如:
The Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.txt
Developer Information
必须提供一个开发人员信息,出问题了可以立马联系到
例如:
Manfred Moser
manfred@sonatype.com
Sonatype
http://www.sonatype.com
SCM Information
必须接入版本控制系统,git,svn…
例如接入Git
scm:git:git://github.com/simpligility/ossrh-demo.git scm:git:ssh://github.com:simpligility/ossrh-demo.git http://github.com/simpligility/ossrh-demo/tree/masterFully configured example projects including metadata as well as dependencies and Maven build configuration are, for example, available at
- https://github.com/simpligility/ossrh-demo/blob/master/pom.xml
- https://bitbucket.org/simpligility/ossrh-pipeline-demo/src
-
coordinates: Picking the appropriate coordinates for your project is important. See the guidelines here, particularly on groupId and domain ownership.(坐标,参考上面第4点的Correct Coordinates)
Publishing your artifacts to the Central Repository
Approved Repository Hosting
必须使用官方认证的库
- Apache Software Foundation (for all Apache projects)
- see the full list
Other Projects
The easiest way to upload another project is to use the Open Source Software Repository Hosting (OSSRH), which is an approved repository provided by Sonatype for any OSS Project that want to get their artifacts into the Central Repository.
使用托管库上传,maven*仓库每隔一段时间就去托管库更新
一个简单的例子
- pom.xml 中必须包括:name、description、url、licenses、developers、scm 等基本信息
<distributionManagement>
<repository>
<id>repository</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<profiles>
<profile>
<id>disable-javadoc-doclint</id>
<activation>
<jdk>[1.8,)</jdk>
</activation>
<!-- java8版本导致javadoc打包编译失败时候,添加-->
<properties>
<javadoc.opts>-Xdoclint:none</javadoc.opts>
</properties>
</profile>
<profile>
<id>release</id>
<build>
<plugins>
<!-- sonatype 托管库自动发布的插件-->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
<!-- 指定Java8为maven的编译-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- gpg文件打包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 源代码打包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</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>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<!-- java8版本导致javadoc打包编译失败时候,添加-->
<configuration>
<additionalparam>${javadoc.opts}</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<!-- 许可证 -->
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<!-- 版本控制系统 -->
<scm>
<url>https://gitee.com/ikcross/aim-cloud.git</url>
<connection>https://gitee.com/ikcross/aim-cloud.git</connection>
<developerConnection>https://gitee.com/ikcross/aim-cloud</developerConnection>
</scm>
<!-- 开发者信息 -->
<developers>
<developer>
<name>yanyiqiu</name>
<email>yanyiqiu@meizu.com</email>
<url>自己的域名</url>
</developer>
</developers>
- magen的setting.xml配置
需要添加sonatype的身份认证,就是你一开始申请账号时候的用户名和密码。id要和pom.xml中distributionManagement 中的snapshotRepository和repository保持一致。
<server>
<id>id</id>
<username>yourname</username>
<password>yourpass!</password>
</server>
-
执行下面命令
mvn clean install deploy -P release -Dgpg.passphrase=生成PGP秘钥时候你的密码
可以通过gpg秘钥安装来发布到PGP的秘钥服务器
构建完就可以上sonatype这个网站进行发布啦
参考:
https://zhuanlan.zhihu.com/p/41650855