卖不甜枣 发表于 2024-5-17 05:46:14

Java Maven项目推送到 Maven 中心仓库

预备阶段

namespace 域名认证

当需要在 sonatype 认证 com.xxx命名空间时,需要将 @.xxx.com 配置域名剖析。
记录范例:TXT
文本内容:验证的 key。
http://switch-openwrite.oss-cn-hangzhou.aliyuncs.com/16245_3157E018452C43E19EB404B7FD5969AF
http://switch-openwrite.oss-cn-hangzhou.aliyuncs.com/16245_17C6F1BD22DB47898DEA95240C9B93E8
GPG 公私钥天生

GPG 下载地址:https://www.gnupg.org/download/index.html
Mac 可以利用 brew install gpg直接安装
利用方式可参考:

[*]https://central.sonatype.org/publish/requirements/gpg/
[*]https://www.jianshu.com/p/7f19ceacf57c
天生证书

$ gpg --gen-keyhttp://switch-openwrite.oss-cn-hangzhou.aliyuncs.com/16245_42930754E2A542AEBC0DB65F701D0F7F
http://switch-openwrite.oss-cn-hangzhou.aliyuncs.com/16245_9E927C62ACEA4A6AA2D85492E84083E6
查询证书

$ gpg --list-keys
gpg: 正在检查信任度数据库
gpg: marginals needed: 3completes needed: 1trust model: pgp
gpg: 深度:0有效性:1已签名:0信任度:0-,0q,0n,0m,0f,1u
gpg: 下次信任度数据库检查将于 2027-04-06 进行

---------
pub   ed25519 2024-04-06 [有效至:2027-04-06]
      ABC
uid             [ 绝对 ] xxx <xxx@163.com>
sub   cv25519 2024-04-06 [有效至:2027-04-06]上传公钥到公钥管理服务器

$ gpg --keyserver keyserver.ubuntu.com --send-keys ABC
gpg: 正在发送密钥 ABC 到 hkp://keyserver.ubuntu.com假如报错 gpg: keyserver send failed: No route to host可以参考该文章上传公钥:https://vayne.cc/2022/03/13/gpg/
推送阶段

pom.xml 文件配置

配置 url

    <url>https://github.com/xxx/yyy</url>配置 license

    <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>配置 issueManagement

            github      <url>https://github.com/xxx/yyy</url>/issues    配置 SCM

            scm:git:https://github.com/xxx/yyy.git      scm:git:https://github.com/xxx/yyy.git      <url>https://github.com/xxx/yyy</url>    配置开辟者信息

    <developers>
      <developer>
            <name>xxx</name>
            <email>xxx@zzz.com</email>
            <url>https://github.com/xxx</url>
      </developer>
    </developers>通用插件配置

    <build>
      <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                  <updatePomFile>true</updatePomFile>
                  <flattenMode>oss</flattenMode>
                </configuration>
                <executions>
                  <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                  </execution>
                  <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                  </execution>
                </executions>
            </plugin>

            
            <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>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.1.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>

                  
                  <failOnError>false</failOnError>
                  <doclint>none</doclint>
                </configuration>
            </plugin>
      </plugins>
    </build>Maven 上传插件配置

    <profiles>
      <profile>
            <id>release</id>
            <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>
                        <configuration>
                            <gpgArguments>
                              
                              <arg>--pinentry-mode</arg>
                              <arg>loopback</arg>
                            </gpgArguments>
                        </configuration>
                  </plugin>
                  
                  <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>
                            <autoPublish>true</autoPublish>
                            <excludeArtifacts>
                              <excludeArtifact>xxx-yyy</excludeArtifact>
                            </excludeArtifacts>
                        </configuration>
                  </plugin>
                </plugins>
            </build>
      </profile>
    </profiles>settings.xml 文件配置

<servers>
    <server>
      <id>central</id>
      <username>xxx</username>
      <password>yyy</password>
    </server>
</servers>
<profiles>
    <profile>
      <id>gpg</id>
      <activation>
      <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
      <gpg.executable>gpg</gpg.executable>
      <gpg.keyname>xxx@zzz.com</gpg.keyname>
      <gpg.passphrase>passphrase</gpg.passphrase>
      <gpg.useagent>true</gpg.useagent>
      </properties>
    </profile>
</profiles>实验构建并上传

$ mvn clean deploy -Prelease 上传结果

http://switch-openwrite.oss-cn-hangzhou.aliyuncs.com/16245_452B80F021024ADEA020750E184DC8A1
报错参考


[*]Javadocs must be provided but not found in entries:需要提供 Javadoc
[*]License information is missing:需要提供 license 信息
[*]Project URL is not defined:需要界说项目 URL 信息
[*]SCM URL is not defined:需要界说 SCM 信息
[*]version cannot be a SNAPSHOT:Maven 中心仓库不支持推送快照版本
参考文档


[*]https://central.sonatype.org/publish/publish-portal-maven/
[*]https://central.sonatype.org/publish/requirements/gpg/
[*]https://www.gnupg.org/download/index.html
[*]https://www.jianshu.com/p/7f19ceacf57c
分享并记录所学所见

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Java Maven项目推送到 Maven 中心仓库