如何在maven仓库发布自己的包

1. 首先拥有自己的账号

Maven Central

2. 创建空间

登录账号之后,在右上角选择View Namespaces

点击Add Namespace

根据自己的仓库填写namespace

Choosing your Coordinates - Documentation

3. 验证空间

根据提示在自己仓库创建临时仓库,然后点击验证即可

4. 生成token用户

选择View Account 

点击 Generate User Token

这里生成的token user需要配置到maven的setting.xml

5. gpg

https://gnupg.org/download/index.html#sec-1-2

下载gpg4win并安装,通过命令验证 gpg --version

生成密钥对 gpg --gen-key

分发到密钥服务器 gpg --keyserver keyserver.ubuntu.com --send-keys A378393D3A69057FBEC7635DE7C61E7CB6BA4DE1

验证是否分发成功 gpg --keyserver keyserver.ubuntu.com --recv-keys A378393D3A69057FBEC7635DE7C61E7CB6BA4DE1

6. 配置maven setting.xml

<profile>
		  <id>central</id>
		  <activation>
			<activeByDefault>true</activeByDefault>
		  </activation>
		  <properties>
			<!-- gpg.exe 文件的位置\gpg -->
			<gpg.executable>F:\worktools\gpg\az\GnuPG\bin\gpg</gpg.executable>
			<!-- 创建密钥对时配置的密码 -->
			<gpg.passphrase>abc123456</gpg.passphrase>
		  </properties>
		</profile>

7. 修改项目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.gitee.test</groupId>
    <artifactId>proj-name</artifactId>
    <name>proj-name</name>
    <packaging>jar</packaging>
    <version>0.0.1</version>
    <description>描述</description>
    <url>https://gitee.com/test/proj-name.git</url>

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>test</name>
            <email>邮箱</email>
            <url>https://gitee.com/test/proj-name.git</url>
            <timezone>+8</timezone>
        </developer>
    </developers>

    <scm>
        <!-- 项目URL -->
        <url>https://gitee.com/test/proj-name</url>
        <!-- 项目URL.git -->
        <connection>scm:git:https://gitee.com/test/proj-name.git</connection>
        <!-- 项目URL.git -->
        <developerConnection>scm:git:https://gitee.com/test/proj-name.git</developerConnection>
    </scm>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 服务id 也就是setting.xml中的servers.server.id -->
        <serverId>central</serverId>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
    </dependencies>

    <build>
        <plugins>
            <!-- 编译插件,设置源码以及编译的jdk版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <!-- Source -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Javadoc -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <additionalparam>-Xdoclint:none</additionalparam>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Javadoc -->
            <!-- Gpg Signature -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 新方式的配置,将组件部署到OSSRH并将其发布到Central Repository-->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>central</publishingServerId>
                    <tokenAuth>true</tokenAuth>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <snapshotRepository>
            <id>${serverId}</id>
            <url>https://central.sonatype.com/</url>
        </snapshotRepository>
    </distributionManagement>
</project>

8. 执行发布 

mvn deploy -e

这里只是相当于把包上传上去了,还需要到Maven Central去手动发布

9. publish

进入页面之后点击publish,大概需要半小时

10. 验证

Maven Central搜索试试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

n_rts

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值