從零開始把項目發佈到maven倉庫中心

sonatype準備操作

1. group id 如果你有com域名的所有權可以直接使用,如果不符合可以使用com.github.{賬號名來定義}
2. 如果有問題管理員會在下方留言通知你如下文(github所有權驗證)

然後等會有類似的通知發出,並按通知進行操作。

When choosing a groupId that reflects your project hosting, in this case, something like io.github.ants-double or com.github.ants-double would be correct. Also, please create a public repo called OSSRH-52139 to verify github account ownership.

然後你就創建對應的公有倉庫,等創建成功後在些回覆一下,看到如下評論前期準備工作就正式結束了

com.github.ants-double has been prepared, now user(s) antsdouble,Ants-double can:

Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
Release staged artifacts into repository 'Releases'
please comment on this ticket when you promoted your first release, thanks

GPG的準備工作

  • windows安裝gpg https://www.gpg4win.org/get-gpg4win.html,這裏使用git命令行工具

  • 生成祕鑰

    gpg --gen-key 按照提示輸入姓名/郵箱,然後按O即可生成
  • 查看祕鑰

    gpg --list-keys
  • 同步公鑰

    gpg --keyserver http://keys.gnupg.net:11371/ --send-keys [xxxxxxxxx] 其中的[xxxxxxxxx],可用gpg --list-keys顯示出來。

Maven準備工作

  • 更新setting.xml文件添加倉庫

    <server>
      <id>sonatype-nexus-snapshots</id>
      <username>Sonatype賬號</username>
      <password>Sonatype密碼</password>
    </server>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>Sonatype賬號</username>
      <password>Sonatype密碼</password>
    </server>
    
  • 修改pom.xml文件 增加許可協議、SCM信息、開發者信息 (最後附完整的pom.xml文件)

    <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>
            <tag>master</tag>
            <url>[email protected]:Ants-double/mianhua.git</url>
            <connection>scm:git:[email protected]:Ants-double/mianhua.git</connection>
            <developerConnection>scm:git:[email protected]:Ants-double/mianhua.git</developerConnection>
        </scm>
        <developers>
            <developer>
                <name>ants-double</name>
                <email>[email protected]</email>
                <organization>ants-double</organization>
            </developer>
        </developers>
  • 添加相關插件和倉庫信息

    <build>
                    <plugins>
                        <!-- 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>
                                <encoding>${chartset.UTF8}</encoding>
                                <aggregate>true</aggregate>
                                <charset>${chartset.UTF8}</charset>
                                <docencoding>${chartset.UTF8}</docencoding>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>attach-javadocs</id>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>jar</goal>
                                    </goals>
                                    <configuration>
                                        <additionalparam>-Xdoclint:none</additionalparam>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                        <!-- GPG -->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-gpg-plugin</artifactId>
                            <version>1.5</version>
                            <executions>
                                <execution>
                                    <phase>verify</phase>
                                    <goals>
                                        <goal>sign</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <!--Compiler-->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.0</version>
                            <configuration>
                                <source>1.8</source>
                                <target>1.8</target>
                                <fork>true</fork>
                                <verbose>true</verbose>
                                <encoding>UTF-8</encoding>
                                <showWarnings>false</showWarnings>
                            </configuration>
                        </plugin>
                        <!--Release-->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-release-plugin</artifactId>
                            <version>2.5.1</version>
                        </plugin>
                    </plugins>
                </build>
                <distributionManagement>
                    <snapshotRepository>
                        <id>sonatype-nexus-snapshots</id>
                        <name>Sonatype Nexus Snapshots</name>
                        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                    </snapshotRepository>
                    <repository>
                        <id>sonatype-nexus-staging</id>
                        <name>Nexus Release Repository</name>
                        <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                    </repository>
                </distributionManagement>

打包發佈

  • 使用 命令,一鍵打包。

    `mvn clean deploy -P <profile-id>` 
  • 打包過程中會要求輸入gpg密鑰的Passphase信息,輸入即可

deploy

  • 然後到https://oss.sonatype.org/#stagingRepositories 查看,看到如下信息

    activity

  • 然後去賬號下面去回覆評論,大概意思是我弄完了,你處理一下吧

    oss

  • 最後在 nexus repository manager中staging repositories選中最新發布的項目,然後執行

    open -> closed
    closed->release
    執行完可以在activity中查看狀態以角錯誤的原因
  • 最後按官網上說的過一會,maven Center可以更久一點就能查到了。

    https://search.maven.org/
    
    https://mvnrepository.com/
  • 附圖

    mainhua

完整的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>com.github.ants-double</groupId>
    <artifactId>mainhua</artifactId>
    <version>1.0-RELEASE</version>

    <name>mainhua</name>
    <url>https://github.com/Ants-double/mianhua</url>
    <description>常用工具集成</description>

    <properties>
        <!-- Environment Settings -->
        <java.version>1.8</java.version>

        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <chartset.UTF8>UTF-8</chartset.UTF8>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!--        json-->
        <project.fastjson.version>1.2.56</project.fastjson.version>
        <lombok.version>1.18.8</lombok.version>
        <project.test.version>4.12</project.test.version>

    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${project.fastjson.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${project.test.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
    <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>
        <tag>master</tag>
        <url>[email protected]:Ants-double/mianhua.git</url>
        <connection>scm:git:[email protected]:Ants-double/mianhua.git</connection>
        <developerConnection>scm:git:[email protected]:Ants-double/mianhua.git</developerConnection>
    </scm>
    <developers>
        <developer>
            <name>ants-double</name>
            <email>[email protected]</email>
            <organization>ants-double</organization>
        </developer>
    </developers>
    <profiles>
        <profile>
            <id>Releases</id>
            <build>
                <plugins>
                    <!-- 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>
                            <encoding>${chartset.UTF8}</encoding>
                            <aggregate>true</aggregate>
                            <charset>${chartset.UTF8}</charset>
                            <docencoding>${chartset.UTF8}</docencoding>
                        </configuration>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <additionalparam>-Xdoclint:none</additionalparam>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- GPG -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.5</version>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--Compiler-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.0</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                            <fork>true</fork>
                            <verbose>true</verbose>
                            <encoding>UTF-8</encoding>
                            <showWarnings>false</showWarnings>
                        </configuration>
                    </plugin>
                    <!--Release-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-release-plugin</artifactId>
                        <version>2.5.1</version>
                    </plugin>
                </plugins>
            </build>
            <distributionManagement>
                <snapshotRepository>
                    <id>sonatype-nexus-snapshots</id>
                    <name>Sonatype Nexus Snapshots</name>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>sonatype-nexus-staging</id>
                    <name>Nexus Release Repository</name>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>

        </profile>
    </profiles>
</project>

出現的問題

  1. java1.8javadoc嚴格驗證

    解決是添加
     <configuration>
                                        <additionalparam>-Xdoclint:none</additionalparam>
                                    </configuration>
  2. 項目id不存在,解決是添加命名的項目id

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章