mac发布项目到maven中央仓库

本文详述了如何在mac环境下,利用Maven和Java,将项目注册并发布到Sonatype的OSS仓库,包括账号注册、环境配置、GPG签名生成、pom.xml修改及构建发布流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.环境准备

mac系统

maven

java

2.注册 Sonatype 账号

Sonatype官网:http://www.sonatype.org/
注册地址:https://issues.sonatype.org/secure/Signup!default.jspa
oss地址:https://oss.sonatype.org

登录 Sonaytype Jira 创建一个 issue
创建一个 issue

Project 选择 Community Support - Open Source Project Repository Hosting (OSSRH)
Issue Type 选择 new Project
Summary 输入你的 项目介绍
GroupId 填写你的工程使用的 groupId,如 io.github.xxx,如果是其他,必须是自己的域名,必要时,管理员会要求你用填写域名的邮箱发送验证邮件来证明域名是你的;
其他不用填写,直接点击 Create,创建一个 issue
接下来就是等待回复了;

审批通过之后,会回复你这样的内容

 3.更新本地 Maven 的 setting.xml 文件

<server>
     <id>ossrh</id>
     <username>刚注册的用户名</username>
     <password>密码</password>
</server>    

添加setting.xml添加profile

    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>the_pass_phrase</gpg.passphrase>
      </properties>
    </profile>

4.生成 GPG 签名

下载安装gpg2

生成gpg2签名

$ gpg2 --gen-key

# 接下来会要求输入 username 和 email,请对号入座,确认信息之后会弹出一个 shell 对话框,要求输入签名秘钥passphrase,输入一个自己记得住的秘钥,一定要记下来,以后每次 deploy 都会使用到。确认后完成签名的生成。

pub   rsa2048 2019-04-25 [SC] [有效至:2021-04-24]
      4140A213ED5136AB8130E3E0B66F18CEA315F945
uid                      yingxiaoyong <1327478407@qq.com>
sub   rsa2048 2019-04-25 [E] [有效至:2021-04-24]

其中4140A213ED5136AB8130E3E0B66F18CEA315F945 这个是我们后面要用到的公钥key
# 上传 GPG 公钥到秘钥服务器
$ gpg2 --keyserver hkp://pool.sks-keyservers.net --send-keys 公钥ID
 
# 通过下面的命令验证是否上传成功
$ gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 公钥ID

5.修改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>

    <groupId>io.github.yxylemon</groupId>
    <artifactId>excel-boot</artifactId>
    <version>1.1.RELEASE</version>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>9</version>
    </parent>

    <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://github.com/yxylemon/excel-boot</url>
        <connection>https://github.com/yxylemon/excel-boot.git</connection>
        <developerConnection>https://github.com/yxylemon/excel-boot</developerConnection>
    </scm>
    <developers>
        <developer>
            <name>yingxiaoyong</name>
            <email>1327478407@qq.com</email>
            <url>https://github.com/yxylemon/excel-boot</url>
        </developer>
    </developers>

    <dependencies>
        <!--poi-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!--org.apache.xerces.parsers.SAXParser-->
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.12.0</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--HttpServletResponse-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>
        <!--log-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.8.0-beta2</version>
        </dependency>
        <!--Guava-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>27.0.1-jre</version>
        </dependency>
    </dependencies>

    <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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <target>1.6</target>
                    <source>1.6</source>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
<!--                    <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-source-plugin</artifactId>
                        <version>2.1.2</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.10.1</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <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>
                </plugins>
            </build>
        </profile>

    </profiles>
</project>

其中scm,developer修改成自己的地址,plugins中source,javadoc,gpg是必须的

发布构建到 Sonatype

我 pom.xml 里面定义的 profiles id 为“release”,所以我执行下面的 Command

$ mvn clean deploy -P release

## 过程中会要求输入前面生成 GPG 秘钥的 passphrase,输入即可

如果不出意外,整个过程将会一帆风顺,刷过几屏日志之后,本地 Maven 构架就已经上传到 Sonatype 的 OSS 暂存仓库上了,注意只是StagingRepositories,还没有到中央仓库;

这是就可以登录 https://oss.sonatype.org/ 查看了
点击 左侧菜单栏当中的 Staging Repositiries,在右上角的搜索框中输入自己的 groupId 进行模糊检索;找到自己刚才 deploy 的构建;
Staging Repositiries
勾选构建,点击 Close ,输入一段 Description 之后点击 Confirm 按钮;
之后需要等待一段时间,等 Close Staging 的状态生效之后,就可以再次登录oss找到自己的构建,选中之后,点击 Release 按钮;再次 Confirm 之后,恭喜你,你的构建已经正式发布到 Maven 中央仓库了。

接下来,到中央仓库:https://search.maven.org/ 搜索到你的构建了,如果没有搜索到,稍等片刻一定会有的,OSS 同步到中央仓库需要一点点时间;

如果你已经在中央仓库搜索到自己的构建了,记得回到 sonatype 的 jira issue 里面回复管理员你已经完成了构建,可以 Close this issue 了;

tips:如果想自动close-release 可以把上面的注解的组建nexus-staging-maven-plugin注解打开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值