發佈java項目到Maven中央倉庫的實踐過程記錄

    作爲一名資深的java工程師(並不是),我們長期的工作使我們積累了非常多的輪子,有些輪子使我們可以進行騷操作,有些輪子幫助我們快速啓動項目,單獨的一個類,一部分代碼片段我們可以在固定的文檔裏做記錄,但是如果是作爲項目的組件或者工具庫,顯然我們需要一個雲端的倉庫並且能在使用時方便的耦合到我們的項目中,於是我們選擇發佈自己的構件到 Maven 中央倉庫中(oss.sonatype.org/,問渠那得清如許,絕知此事要躬行,開始動手環節:

具體發佈步驟如下:

一、創建項目的github倉庫,我使用的github,記錄下地址

二、註冊sonatype賬號,創建新的issue

1.註冊地址:https://issues.sonatype.org ,務必記住賬號密碼!

2.創建一個新的issue

如圖,在sonatype官網的上方,點擊“create”創建一個新的issue

3.填寫相關信息,此處需要填寫項目github倉庫以確認你有項目的所有權

①其中上方的“Project”和“Issue Type”不需要修改,如下方所示

②“Group Id”中填寫的域名你要有所有權

③創建issue後,按照commonts中官方的提示進行相關操作

三、創建gpg祕鑰對

win10下載gpghttps://www.gpg4win.org/

安裝完成後依次執行以下命令:

  • gpg --version 檢查是否安裝成功
  • gpg --gen-key 生成密鑰對
  • gpg --list-keys 查看公鑰

  • gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 你的公鑰 將公鑰發佈到 PGP 密鑰服務器

  • gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 你的公鑰 查詢公鑰是否發佈成功

 

四、修改你使用的maven的settings文件

我使用的settings文件內容如下:

<?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>E:\maven-repository</localRepository>	
	<pluginGroups>
	</pluginGroups>

	<proxies>	
	</proxies>
	<servers>
		<server>
		  <id>ossrh</id> <!--自定義的id,在項目的pom文件配置中會用到-->
		  <username>jorian93</username><!--snatype的賬號-->
		  <password>sXgc42.@1125</password><!--snatype的密碼-->
		</server>
		<server>
			<id>sonatype-nexus-snapshots</id>
			<username>jorian93</username><!--snatype的賬號-->
			<password>sXgc42.@1125</password><!--snatype的密碼-->
		</server>
		<server>
			<id>sonatype-nexus-staging</id>
			<username>jorian93</username><!--snatype的賬號-->
			<password>sXgc42.@1125</password><!--snatype的密碼-->
		</server>
	</servers>
	
	<mirrors>		
	</mirrors>
	
	<profiles>		
	</profiles>

	<activeProfiles>		
	</activeProfiles>
</settings>

五、設置項目的pom文件,我的pom文件如下:

<?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>cn.jorianye.common</groupId>
    <artifactId>tools</artifactId>
    <version>1.0.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <name>tools</name>
    <description>Demo project for Spring Boot</description>
    <url>https://github.com/Jorian93/common</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <swagger.version>2.8.0</swagger.version>
        <google.guava>23.0</google.guava>
        <fastjson.version>1.2.47</fastjson.version>
        <druid.version>1.1.9</druid.version>
        <poi.version>3.17</poi.version>
        <jwt.version>0.9.0</jwt.version>
    </properties>
    <!--licenses信息-->
    <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信息,此處和你在sonatype中的issue填寫的一樣-->
    <scm>
        <url>https://github.com/Jorian93/common</url>
        <connection>scm:git:https://github.com/Jorian93/common.git</connection>
        <developerConnection>scm:git:https://github.com/Jorian93/common.git</developerConnection>
    </scm>

    <!--發佈者信息-->
    <developers>
        <developer>
            <name>jorianye</name>
            <email>[email protected]</email>
            <organization>https://github.com/Jorian93</organization>
            <organizationUrl>https://github.com/Jorian93</organizationUrl>
        </developer>
    </developers>
<!--你項目的依賴-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!--mail-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!--swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!--Shiro-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--LOMBOK-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>
        <!--Mybatis-Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!--mpplus 3.x後需單獨添加代碼生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!--mp-plus-分頁插件-->

        <!--velocity 代碼生成器需要模板引擎-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>

        <!-- 模板引擎 -->
        <!--<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.20</version>
        </dependency>-->
        <!--JWT-->
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--guava-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${google.guava}</version>
        </dependency>


    </dependencies>

    <profiles>
        <profile>
            <id>release</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>
                    <!-- java-doc ,不可省略-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <configuration>
                            <encoding>utf-8</encoding>
                            <charset>utf-8</charset>
                            <docencoding>utf-8</docencoding>
                            <additionalOptions>
                                <additionalOption>-Xdoclint:none</additionalOption>
                            </additionalOptions>
                        </configuration>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- 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>
                </plugins>
            </build>
            <distributionManagement>
                <snapshotRepository>
                    <id>ossrh</id><!--此處是你在settings中設置的server id-->
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>ossrh</id><!--此處是你在settings中設置的server id-->
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
        </profile>
    </profiles>

</project>

六、執行mvn deploy,發佈項目到中央倉庫

七、登陸 https://oss.sonatype.org 後,查看stagingRepositories

①剛剛提交的項目在系統中會顯示是open狀態

②勾選剛剛提交的項目,點擊close,刷新頁面,使項目進入closed狀態

③勾選剛剛提交的項目,點擊release,刷新頁面,使項目進入released狀態

八、回到issue,回覆官方,表示已經將項目進入到released狀態

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