发布jar包到公有maven库详解

简介: Step 1: 安装 JDK, Maven, 建 Github 账号等等.Step 2: 如果还没有,先建 GitHub 账号.Step 3: 建立新的 Github 库.Step 4: 为你的GitHub账号 新增 SSH 密钥Step 5: 向GitHub推送代码. Step 6: 注册 Sonatype Jira 账号Step 7: 为新托管项目建一个 Jira issue.搞个简单请求点这里.

初始步骤要求


Step 1: 安装 JDK, Maven, 建 Github 账号等等.

Step 2: 如果还没有,先建 GitHub 账号.

Step 3: 建立新的 Github 库.

Step 4: 为你的GitHub账号 新增 SSH 密钥

Step 5: 向GitHub推送代码.

Step 6: 注册 Sonatype Jira 账号

Step 7: 为新托管项目建一个 Jira issue.搞个简单请求点这里.

DZN)~YQ%42`GBS8VI71CQ4Y.png

Step 8:为你的操作系统 安装 GNU PG. 确认如下:

C:\Users\Nadeem>gpg --version
gpg (GnuPG) 2.1.15
libgcrypt 1.7.3
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: C:/Users/Nadeem/AppData/Roaming/gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2


Step 8: 生成键对

C:\Users\Nadeem>gpg --full -gen -key
gpg (GnuPG) 2.1.15; Copyright (C) 2016 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
gpg: keybox 'C:/Users/Nadeem/AppData/Roaming/gnupg/pubring.kbx' created
Please select what kind of key you want:
 (1) RSA and RSA (default)
 (2) DSA and Elgamal
 (3) DSA (sign only)
 (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
 <n>  = key expires in n days
 <n>w = key expires in n weeks
 <n>m = key expires in n months
 <n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y
GnuPG needs to construct a user ID to identify your key.
Real name: Nadeem Mohammad
Email address: coolmind182006@gmail.com
Comment:
You selected this USER-ID:
"Nadeem Mohammad <coolmind182006@gmail.com>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy.
gpg: C:/Users/Nadeem/AppData/Roaming/gnupg/trustdb.gpg: trustdb created
gpg: key 27835B3BD2A2061F marked as ultimately trusted
gpg: directory 'C:/Users/Nadeem/AppData/Roaming/gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as 'C:/Users/Nadeem/AppData/Roaming/gnupg/openpgp-revocs.d\5694AA563793429557F1727835B3BD2A223A.rev'
public and secret key created and signed.
pub   rsa2048 2016-08-29 [SC]
5694AA563793429557F1727835B3BD2A223A
uid                      Nadeem Mohammad <coolmind182006@gmail.com>
sub   rsa2048 2016-08-29 [E]
C:\Users\Nadeem>


Step 9: 输入密码.

@`}L{D_ATKF0LMW$T4$~CFN.png


发布步骤

Step 1:增加发布管理 distributed management 部分到 pom.xml.

增加发布插件 deploy plugin.

<plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <executions>
        <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
                <goal>deploy</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这是如何增加发布管理项的(distribution management)到 POM:

<distributionManagement>
    <snapshotRepository>
        <id>ossrh</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </snapshotRepository>
    <repository>
        <id>ossrh</id>
        <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    </repository>
</distributionManagement>


Step 2:增加 ossrh 服务器到你的maven配置文件 ~/$M2_REPO/settings.xml.

<settings>
    <servers>
        <server>
            <id>ossrh</id>
            <username>your-jira-id</username>
            <password>your-jira-pwd</password>
        </server>
    </servers>
</settings>
注: ID 与 settings.xml 中的 servers/server ID 要相对应, snapshot库与 POM 文件里的库也要对应.


Step 3:在pom里增加代码管理部分SCM.

<scm>
    <connection>scm:git:git://github.com/dexecutor/dependent-tasks-executor.git</connection>
    <developerConnection>scm:git:git@github.com:yujiaao/spring-mvc-source-analysis.git</developerConnection>
    <url>https://github.com/dexecutor/dependent-tasks-executor</url>
    <tag>HEAD</tag>
</scm>


Step 4: 增加 Maven release 插件.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
        <localCheckout>true</localCheckout>
        <pushChanges>false</pushChanges>
        <mavenExecutorId>forked-path</mavenExecutorId>
        <arguments>-Dgpg.passphrase=${gpg.passphrase}</arguments>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.scm</groupId>
            <artifactId>maven-scm-provider-gitexe</artifactId>
            <version>1.9.5</version>
        </dependency>
    </dependencies>
</plugin>

把 GPG 密码放到Maven settings.xml对应的profile里.

<settings>
    <profiles>
        <profile>
            <id>ossrh</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <gpg.passphrase>[your_gpg_passphrase]</gpg.passphrase>
            </properties>
        </profile>
    </profiles>
</settings>

增加 Nexus staging Maven 插件.

<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>nexus-staging-maven-plugin</artifactId>
    <version>1.6.7</version>
    <extensions>true</extensions>
    <configuration>
        <serverId>ossrh</serverId>
        <nexusUrl>https://oss.sonatype.org/</nexusUrl>
        <autoReleaseAfterClose>true</autoReleaseAfterClose>
    </configuration>
</plugin>


Step 5: 增加源码和 javadoc 插件.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.10.4</version>
    <configuration>
        <encoding>UTF-8</encoding>
    </configuration>
    <execuations>
        <execution>
            <id>attach-javadoc</id>
            <goals>
        </execution>
    </executions>
</plugin>


Step 6: 配置发布版本的项目签名.

<profiles>
    <!-- GPG Signature on release -->
    <profile>
        <id>release-sign-artifacts</id>
        <activation>
            <property>
                <name>performRelease</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-gpg-plugin</artifactId>
                    <version>1.6</version>
                    <executions>
                        <execution>
                            <id>sign-artifacts</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>


Step 7: 发布 GPG 密钥对到 GPG 服务器:

gpg –keyserver [KEY_SERVER] –send-key [KEY_ID]
The KEY_ID in the above case is 5694AA563793429557F1727835B3BD2A223A.
Some of the key servers are: 
pool.sks-keyservers.net
gnupg.net:11371
keys.pgp.net
surfnet.nl
mit.edu


Step 8: 发布正式版本!

mvn clean
mvn release:prepare
mvn release:perform


Step 9: 推送标签 tag 和代码到远程库.

git push–tags
git push origin maste


Step 10: 验证sonatype库.

AC$6F4W7DZ@~6HUZ30_1VOY.png


Step 11: 更新 Sonatype 的 Jira 问题项(ticket).

@L0VR_]DT(_46IO51YCTYTR.png

详细配置可查看这个pom.xml文件例子。


搞错了怎么办

Step 1: 取消本次发布:

git reset –hard HEAD~1 (You may have to do it a second time, depending upon when the error occurred.)
git reset –hard HEAD~1

Step 2: 删除标签tag.

git tag -d tagName
git push origin :refs/tags/tagName
相关文章
|
4月前
|
Java Maven
2022最新版超详细的Maven下载配置教程、IDEA中集成maven(包含图解过程)、以及导入项目时jar包下载不成功的问题解决
这篇文章是一份关于Maven的安装和配置指南,包括下载、环境变量设置、配置文件修改、IDEA集成Maven以及解决jar包下载问题的方法。
2022最新版超详细的Maven下载配置教程、IDEA中集成maven(包含图解过程)、以及导入项目时jar包下载不成功的问题解决
|
4月前
|
Java Maven 容器
java依赖冲突解决问题之Maven在编译打包过程中对依赖的jar包如何解决
java依赖冲突解决问题之Maven在编译打包过程中对依赖的jar包如何解决
|
1月前
|
Java 应用服务中间件 Maven
Maven的三种项目打包方式——pom,jar,war的区别
Maven 提供了多种打包方式,分别适用于不同类型的项目。pom 用于父项目或聚合项目,便于项目的结构和依赖管理;jar 用于Java类库或可执行的Java应用程序;war 则专用于Java Web应用程序的部署。理解这些打包方式的用途和特点,可以帮助开发者更好地配置和管理Maven项目,确保构建和部署过程的顺利进行。无论是单模块项目还是多模块项目,选择合适的打包方式对于项目的成功至关重要。
89 3
|
4月前
|
Java Maven 容器
Maven使用IDEA自带工具打包,同时将lib下的jar包打入,双击jar包可直接运行
使用IntelliJ IDEA的Artifacts功能,可以将项目依赖的第三方jar包打包进jar文件中,实现双击jar包即可直接运行。
Maven使用IDEA自带工具打包,同时将lib下的jar包打入,双击jar包可直接运行
|
4月前
|
SQL 前端开发 Java
在IDEA中使用Maven将SpringBoot项目打成jar包、同时运行打成的jar包(前后端项目分离)
这篇文章介绍了如何在IntelliJ IDEA中使用Maven将Spring Boot项目打包成可运行的jar包,并提供了运行jar包的方法。同时,还讨论了如何解决jar包冲突问题,并提供了在IDEA中同时启动Vue前端项目和Spring Boot后端项目的步骤。
在IDEA中使用Maven将SpringBoot项目打成jar包、同时运行打成的jar包(前后端项目分离)
|
4月前
|
Java Maven Windows
Maven 引用jar包冲突 Intellij 查找排除JAR包的依赖关系(Maven Helper)
Maven 引用jar包冲突 Intellij 查找排除JAR包的依赖关系(Maven Helper)
61 0
|
4月前
|
Java Maven
SpringBoot 引用仓库中没有 第三方包 - 将jar 包安装本地 maven
SpringBoot 引用仓库中没有 第三方包 - 将jar 包安装本地 maven
44 0
|
24天前
|
XML Java 测试技术
从零开始学 Maven:简化 Java 项目的构建与管理
Maven 是一个由 Apache 软件基金会开发的项目管理和构建自动化工具。它主要用在 Java 项目中,但也可以用于其他类型的项目。
36 1
从零开始学 Maven:简化 Java 项目的构建与管理
|
1月前
|
Java Maven
maven项目的pom.xml文件常用标签使用介绍
第四届人文,智慧教育与服务管理国际学术会议(HWESM 2025) 2025 4th International Conference on Humanities, Wisdom Education and Service Management
127 8
|
2月前
|
Java 关系型数据库 MySQL
Maven——创建 Spring Boot项目
Maven 是一个项目管理工具,通过配置 `pom.xml` 文件自动获取所需的 jar 包,简化了项目的构建和管理过程。其核心功能包括项目构建和依赖管理,支持创建、编译、测试、打包和发布项目。Maven 仓库分为本地仓库和远程仓库,远程仓库包括中央仓库、私服和其他公共库。此外,文档还介绍了如何创建第一个 SpringBoot 项目并实现简单的 HTTP 请求响应。
169 1
Maven——创建 Spring Boot项目
下一篇
DataWorks