https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
作为一个java程序员,对maven中央仓库https://mvnrepository.com/自然是非常熟悉的,毕竟咱们的应用依赖的jar大部分都来自此处,如果您想把自己开发的java库也托管在上面,让大家像使用Jackson、Spring那样轻松简单的使用您的jar,就请随本文一起操作吧;
先看看效果,下图是我发布的java库在中央仓库的搜索结果:
文末还会对曾经踩过的小坑做了总结,希望能帮助读者们提前避开
看起来略有些繁琐,但其实很简单,接下来开始吧
首先请把您的java工程准备好,我用的是一个非常普通的maven工程,名为opencv-linux,github仓库地址https://github.com/zq2599/opencv-client
本次涉及的软件信息如下:
登录您的github,按照要求创建仓库,我这里要创建的是https://github.com/zq2599/OSSRH-74965
老老实实的创建出来就行了:
后面的操作中,在将jar发布到中央仓库时,要用GPG工具对上传的数据进行签名,因此接下来要准备好GPG秘钥
先安装GPG软件,打开网站:https://www.gnupg.org/download/
下载安装文件,请选择适合您的操作系统的,我的选择如下图红框:
安装完成后,在控制台执行gpg2 --gen-key开始创建秘钥
根据提示输入账号、邮箱、密码等:
GnuPG needs to construct a user ID to identify your key. Real name: zq2599 Email address: zq2599@gmail.com You selected this USER-ID: "zq2599 <zq2599@gmail.com>" Change (N)ame, (E)mail, or (O)kay/(Q)uit? O
gpg: key 11027EJIHGFEDCBA marked as ultimately trusted gpg: directory '/Users/will/.gnupg/openpgp-revocs.d' created gpg: revocation certificate stored as '/Users/will/.gnupg/openpgp-revocs.d/561AEE4EA92EE3E4C389941811027E9876543210.rev' public and secret key created and signed. pub rsa3072 2021-11-10 [SC] [expires: 2023-11-10] 561AEE4EA92EE3E4C389941811027E9876543210 uid zq2599 <zq2599@gmail.com> sub rsa3072 2021-11-10 [E] [expires: 2023-11-10]
如上所示,得到了pub key等于561AEE4EA92EE3E4C389941811027E9876543210
执行以下命令,将秘钥同步到云端,注意keyserver,网上可以搜到很多个,个人实际操作中,下面这个是可以成功的:
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 561AEE4EA92EE3E4C389941811027E9876543210
试想把sonatype的账号密码写在项目的pom.xml中,再上传到github让所有人都看到?相信您一定不愿意这样,所以还是放在maven的全局配置中比较安全,毕竟是保存在自己的电脑上
打开maven的配置文件settings.xml,在servers下面增加一个server节点,这是个账号密码的配置,对应的是https://issues.sonatype.org的账号密码:
<server> <id>ossrh</id> <username>zq2599</username> <password>12345678</password> </server>
<profile> <id>gpg</id> <properties> <!-- 由于我的电脑装的gpg2,所以需要指定执行gpg2,否则会报错 --> <gpg.executable>gpg2</gpg.executable> <gpg.passphrase>abcdefgh</gpg.passphrase> </properties> </profile>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.github.zq2599</groupId> <artifactId>opencv-linux</artifactId> <version>0.0.3</version> <name>opencv-linux</name> <description>opencv-linux</description> <!-- 1. url必须要有,不然远程提交时会返回错误 --> <url>https://github.com/zq2599/opencv-client</url> <properties> <java.version>1.8</java.version> </properties> <packaging>jar</packaging> <!-- 2. 开源证书 --> <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> <!-- 3. 源码仓库信息 --> <scm> <connection>scm:git:git@github.com:zq2599/opencv-client.git</connection> <developerConnection>scm:git:git@github.com:zq2599/opencv-client.git</developerConnection> <url>https://github.com/zq2599/opencv-client/tree/main</url> </scm> <!-- 4. 开发人员信息 --> <developers> <developer> <name>zq2599</name> <email>zq2599@gmail.com</email> <organization>https://github.com/zq2599</organization> <timezone>+8</timezone> </developer> </developers> <!-- 5. 上传的仓库地址,以及使用哪个账号密码配置 --> <distributionManagement> <snapshotRepository> <id>ossrh</id> <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> <repository> <id>ossrh</id> <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> </dependencies> <build> <!-- 配置好每个插件的属性 --> <pluginManagement> <plugins> <!-- 6. 上传到sonatype的插件 --> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>1.6.7</version> <extensions>true</extensions> <configuration> <!-- 这里的id必须要和全局配置中的server一致 --> <serverId>ossrh</serverId> <!-- 这个地址,一定要和issue的评论中给出的地址一致! --> <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl> <!-- 如果希望发布后自动执行close和release操作,此处可以调整为true --> <autoReleaseAfterClose>false</autoReleaseAfterClose> </configuration> </plugin> <!-- 7. 上传源码的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.1.0</version> <inherited>true</inherited> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <excludeResources>true</excludeResources> <useDefaultExcludes>true</useDefaultExcludes> </configuration> </plugin> <!-- 8. 生成doc文档的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.0</version> <inherited>true</inherited> <executions> <execution> <id>bundle-sources</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <maxmemory>1024</maxmemory> <encoding>UTF-8</encoding> <show>protected</show> <notree>true</notree> <!-- Avoid running into Java 8's very restrictive doclint issues --> <failOnError>false</failOnError> <doclint>none</doclint> </configuration> </plugin> <!-- 9. 编译构建maven工程的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </pluginManagement> <!-- 10. 确定要使用哪些插件 --> <plugins> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> </plugins> </build> <profiles> <profile> <id>release</id> <build> <plugins> <!-- 11. 生成签名,确定使用那个gpg秘钥 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.5</version> <executions> <execution> <!-- 必须和配置中的gpg校验id一致 --> <id>gpg</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
mvn clean javadoc:jar deploy -P release
... [INFO] Installing /Users/zhaoqin/github/blog_demos/opencv-linux/target/opencv-linux-0.0.3-sources.jar.asc to /Users/zhaoqin/github/blog_demos/opencv-linux/target/nexus-staging/staging/543da2cd9af848/io/github/zq2599/opencv-linux/0.0.3/opencv-linux-0.0.3-sources.jar.asc [INFO] Performing remote staging... [INFO] [INFO] * Remote staging into staging profile ID "543da2cd9abc12" [INFO] * Created staging repository with ID "iogithubzq2599-1008". [INFO] * Staging repository at https://s01.oss.sonatype.org:443/service/local/staging/deployByRepositoryId/iogithubzq2599-1008 [INFO] * Uploading locally staged artifacts to profile io.github.zq2599 Uploading to ossrh: ... https://s01.oss.sonatype.org:443/service/local/staging/deployByRepositoryId/iogithubzq2599-1008/io/github/zq2599/opencv-linux/0.0.3/opencv-linux-0.0.3-sources.jar.asc (659 B at 1.2 kB/s) [INFO] * Upload of locally staged artifacts finished. [INFO] * Closing staging repository with ID "iogithubzq2599-1008". Waiting for operation to complete... ... [INFO] Remote staged 1 repositories, finished with success. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 27.199 s [INFO] Finished at: 2021-11-12T08:08:37+08:00
<dependency> <groupId>io.github.zq2599</groupId> <artifactId>opencv-linux</artifactId> <version>0.0.3</version> </dependency>
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos