将项目发布到 Maven 中央仓库sonatype

133 阅读1分钟

我本人参考保姆级教程:从 0 到 1 将项目发布到 Maven 中央仓库【2024年5月】_maven 发布到仓库-CSDN博客对这个大佬的文章稍微修改,提供完整的pom文件

完全可以参考这位大佬的文档。但是有几个配置,大佬可能未给出。我下面提供一下我的配置

  • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.gitee.cchilei</groupId>
    <artifactId>uni-pay-hub</artifactId>
    <version>1.0.1</version>
    <name>uni-pay-hub</name>
    <packaging>jar</packaging>
    <url>https://gitee.com/cchilei/uni-pay-hub</url>
    <description>
        “支付中心”工具包
        接入了支付宝(alipay-sdk-java-4.35.79.ALL)、
        微信支付(alipay-sdk-java-4.5.0)
    </description>

    <developers>
        <developer>
            <name>cchilei</name>
            <email>cchilei@163.com</email>
            <organization>Sonatype</organization>
            <organizationUrl>http://www.sonatype.com</organizationUrl>
        </developer>
    </developers>

    <licenses>
        <license>
            <name>Apache License 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <scm>
        <tag>main</tag>
        <url>https://gitee.com/cchilei/uni-pay-hub</url>
        <connection>https://gitee.com/cchilei/uni-pay-hub.git</connection>
        <developerConnection>https://gitee.com/cchilei</developerConnection>
    </scm>

    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <profiles>
        <profile>
            <id>sonatype-cchilei</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <!--gpg插件-->
                    <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>

                    <plugin>
                        <groupId>org.sonatype.central</groupId>
                        <artifactId>central-publishing-maven-plugin</artifactId>
                        <version>0.4.0</version>
                        <extensions>true</extensions>
                        <configuration>
                            <!-- 这里的serverId是之前在settings.xml中配置的 -->
                            <publishingServerId>sonatype-cchilei</publishingServerId>
                            <tokenAuth>true</tokenAuth>
                            <autoPublish>false</autoPublish>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.7</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>sonatype-cchilei</serverId>
                            <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                    <!--生成源码插件-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.2.1</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!--生成API文档插件-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                </plugins>
            </build>
            <distributionManagement>
                <snapshotRepository>
                    <!--注意,此id必须与setting.xml中指定的一致-->
                    <id>sonatype-cchilei</id>
                    <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
                </snapshotRepository>
                <repository>
                    <id>sonatype-cchilei</id>
                    <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
        </profile>
    </profiles>

</project>

  • settings.xml
<?xml version="1.0" encoding="UTF-8"?>


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <localRepository>D:\develop\apache-maven-3.6.3-bin\repository</localRepository>


  <pluginGroups>
  
  </pluginGroups>


  <proxies>

  </proxies>


  <servers>

	<server>
		<id>sonatype-cchilei</id>
		<username>username</username>
		<password>password</password>
	</server>

  </servers>

  <mirrors>
  
 
  </mirrors>

  <profiles>
   
	 <profile>
		<id>sonatype-cchilei</id>
		<properties>
		  <gpg.passphrase>521+ChiLei</gpg.passphrase>
		</properties>
	  </profile>

    
  </profiles>

  
  <activeProfiles>
	<activeProfile>sonatype-cchilei</activeProfile>
  </activeProfiles>

</settings>