將自己的項目作為jar包發布到maven中央倉庫


maven版本是3.5.0,jdk是1.8(注意,不是說項目是1.8就行,必須是環境變量里的也是,不能超過1.8,否則一大堆問題,執行mvn前用java -version看下版本)

一:先在sonatype那里創建一個open source的issue后被審核通過,這個步驟網上的教程基本上一樣可以百度;

二:安裝gpg,並且先生成相關的key,然后上傳到服務器;可看:https://blog.csdn.net/qq_38225558/article/details/94381467(注意,用powershell不要用cmd)

注意,這里上傳時用的是key,而非用public key的id(用更短的那個 gpg key: xxxx)

三:配置setting.xml,主要是增加一個

<servers>
  <!-- 上傳jar包到maven中央倉庫配置start -->
  <server>
      <id>ossrh</id>
      <username>Sonatype賬號</username>
      <password>Sonatype密碼</password>
  </server>
  <!-- 上傳jar包到maven中央倉庫配置end -->
</servers>

四:配置要提交到sonatype倉庫的項目的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.gitee.silentdoer</groupId>
<artifactId>CommentManager</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
<description>The test uploads the jar to the maven central repository</description>
<url>https://gitee.com/silentdoer/CommentManager</url>

<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</parent>

<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>
<connection>scm:git:git://gitee.com/silentdoer/CommentManager.git</connection>
<developerConnection>scm:git:git@gitee.com:silentdoer/CommentManager.git</developerConnection>
<url>https://gitee.com/silentdoer/CommentManager</url>
</scm>

<developers>
<developer>
<name>silentdoer</name>
<email>1010993610@qq.com</email>
<url>https://gitee.com/silentdoer/CommentManager</url>
</developer>
</developers>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<!--<gpg.keyname>53FB91B916248F4D</gpg.keyname>
<gpg.passphrase>secret</gpg.passphrase>-->
<!--<maven.deploy.skip>true</maven.deploy.skip>-->
</properties>

<!-- 【注】snapshotRepository 與 repository 中的 id 一定要與 setting.xml 中 server 的 id 保持一致! -->
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>

<profiles>

<profile>
<id>ossrh</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>

 五:在項目目錄里執行(注意用powershell執行,之前用git的terminal執行死活成功不了)mvn clean deploy -P ossrh -Darguments="gpg.passphrase=password";(這里可能會提示要輸入密碼,后面的-Darguments=...可以不要,我這邊貌似沒什么用,還是要每次都輸入密碼)

注意上面的ossrh是profile的id;(經過測試其實用mvn clean deploy -P ossrh即可)

六:同步到Maven的中央倉庫:

到:https://oss.sonatype.org/#stagingRepositories;【或者直接https://oss.sonatype.org,然后點擊Staging Repositories】找到Staging Reopsitories,然后刷新后拉到最下面,最后一個就是自己提交的【也可以搜索】(注意每次提交新版本version要改下)

勾選它,點擊左上角的close按鈕。當項目狀態變為closed之后,點擊release按鈕,輸入必要的description信息后,就發布成功了。大約幾小時后你就可以在中央倉庫上搜索到你的項目了【比如:https://repo.maven.apache.org/maven2/】!(據說最長需要2小時)(也可以用Drop將上傳的項目刪除然后重新上傳)

注意,這里close是可能失敗的,系統會檢測項目的合法性(主要是看有沒有doc,soure,和asc這些文件);

還有就是可以不執行close-release操作,添加一個插件即可:

<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>nexus-staging-maven-plugin</artifactId>
    <version>1.6.8</version>
    <extensions>true</extensions>
    <configuration>
            <serverId>ossrh</serverId>
            <nexusUrl>https://oss.sonatype.org</nexusUrl>
            <autoReleaseAfterClose>true</autoReleaseAfterClose>
    </configuration>
</plugin>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM