发布GitHub项目到maven仓库

61 阅读2分钟

一 注册sonatype

issues.sonatype.org/secure/Dash…

二 新建项目

1. 点击创建,新建一个项目

截屏2023-10-16 13.50.49.png

截屏2023-10-16 13.53.51.png 注意,问题类型时New Project

概要:项目概要
描述:项目描述
GroupId:用于后面Maven引入包时的groupId,如果是com.xxxx,那么需要对应的域名txt解析,开源项目用io.github.xxx即可,比较方便
ProjectURL:项目GitHub地址,不带.git后缀
SCM url:项目GitHub地址,带.git后缀
Username(s):有权限上传这个库的sonatype用户名,默认不用填(默认就是自己)
Already Synced to Central:是否已经同步:默认 No即可

2. 审核

大概几分钟,会收到邮件及评论,可能会让证明有所填GitHub项目的权限,让我们建议各临时项目,按照邮件里的回复新建一个,然后把把issues 状态重新改为 开放,等待审核人员审核,又过了几分钟,会收到邮件,看看回复,没啥问题的话基本都会通过。

3. 发布项目
  • gpg配置,安装gpg
brew update
brew upgrade
brew install gnupg
  • 生成私密钥
gpg --version
gpg --generate-key
# 按提示输入邮箱 、username以及保护密码(passphrase)
  • 发送公钥到服务器
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys xxxxxxC9B775C55C8xxxxxxxxx
# 查看是否发送成功
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys xxxxxxC9B775C55C8xxxxxxxxx
  • 配置settings.xml \
 <servers>
    <server>
           <id>sonatype</id>
           <username>sonatype用户名</username>
           <password>sonatype密码</password>
    </server>
 </servers>
 ...
 <profiles>
    <profile>
      <id>sonatype</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>gpg保护密码</gpg.passphrase>
      </properties>
    </profile>
</profiles>
  • 配置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>

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>3.1.4</version>
   </parent>

   <groupId>io.github.longxiaoyun.is</groupId>
   <artifactId>robotstxt</artifactId>
   <version>1.0.0</version>
   <packaging>jar</packaging>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <java.version>17</java.version>
       <lombok.version>1.18.30</lombok.version>
       <log4jdbc.log4j2.version>1.16</log4jdbc.log4j2.version>
       <rest.assured.version>2.3.3</rest.assured.version>
       <junit.version>4.13.2</junit.version>
       <commons-lang3.version>3.13.0</commons-lang3.version>
       <fastjson2.version>2.0.41</fastjson2.version>
   </properties>

   <licenses>
       <license>
           <name>MIT License</name>
           <url>https://opensource.org/licenses/MIT</url>
           <distribution>repo</distribution>
       </license>
   </licenses>

   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter</artifactId>
           <exclusions>
               <exclusion>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-starter-logging</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <version>${lombok.version}</version>
           <scope>provided</scope>
       </dependency>


       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>${junit.version}</version>
           <scope>test</scope>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-lang3</artifactId>
           <version>${commons-lang3.version}</version>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-log4j2</artifactId>
       </dependency>

       <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
       <dependency>
           <groupId>com.alibaba.fastjson2</groupId>
           <artifactId>fastjson2</artifactId>
           <version>${fastjson2.version}</version>
       </dependency>
   </dependencies>

   <distributionManagement>
       <snapshotRepository>
           <id>sonatype</id>
           <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
       </snapshotRepository>
       <repository>
           <id>sonatype</id>
           <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
       </repository>
   </distributionManagement>



   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
               <configuration>
                   <excludes>
                       <exclude>
                           <groupId>org.projectlombok</groupId>
                           <artifactId>lombok</artifactId>
                       </exclude>
                   </excludes>
               </configuration>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-gpg-plugin</artifactId>
               <version>3.1.0</version>
               <executions>
                   <execution>
                       <id>auth</id>
                       <phase>verify</phase>
                       <goals>
                           <goal>sign</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
           <plugin>
               <groupId>org.sonatype.plugins</groupId>
               <artifactId>nexus-staging-maven-plugin</artifactId>
               <version>1.6.13</version>
               <extensions>true</extensions>
               <configuration>
                   <serverId>sonatype</serverId>
                   <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                   <autoReleaseAfterClose>true</autoReleaseAfterClose>
               </configuration>
           </plugin>
           <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-source-plugin -->
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-source-plugin</artifactId>
               <version>3.3.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.6.0</version>
               <configuration>
                   <additionalJOptions>
                       <additionalJOption>-Xdoclint:none</additionalJOption>
                   </additionalJOptions>
               </configuration>
               <executions>
                   <execution>
                       <id>attach-javadocs</id>
                       <goals>
                           <goal>jar</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>

           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-deploy-plugin</artifactId>
               <version>3.1.1</version>
           </plugin>

       </plugins>
   </build>



</project>
  • 发布
mvn clean deploy

发布快照版本和release版本,注意修改项目对应的版本号

截屏2023-10-16 14.17.32.png

三 问题记录&解决

1. gpg: 签名时失败: Inappropriate ioctl for device\

当执行 mvn clean deploy 时,出现 gpg: 签名时失败: Inappropriate ioctl for device 错误
原因:无法弹出密码输入框 解决:当前终端执行 export GPG_TTY=$(tty)

2. 403 Forbidden\

执行 mvn clean deploy 时,出现

 Could not transfer artifact ... from/to sonatype (https://s01.oss.sonatype.org/content/repositories/snapshots): Authorization failed for https://s01.oss.sonatype.org/content/repositories/snapshots/... 403 Forbidden -> [Help 1]

解决:

  • 排查groupId 是否正确,要和申请的一致
  • 或者settings文件的路径是不是正确