使用GitHub Actions发布部署项目到Maven Central


假设你在Sonatype的Jira上创建了一个帐户,您的本地帐户settings.xml已经配置好。
Java Actions工作流通常使用一种setup-java操作,它仅用于下载JDK,但事实证明它可以做得更多:它还知道如何设置运行程序以在Maven Central上发布工件:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8

    - name: Build with Maven
      run: mvn -B package --file pom.xml

    - name: Set up Apache Maven Central
      uses: actions/setup-java@v1
      with: # running setup-java again overwrites the settings.xml
        java-version: 1.8
        server-id: maven # Value of the distributionManagement/repository/id field of the pom.xml
        server-username: MAVEN_USERNAME # env variable for 用于部署中的用户名
        server-password: MAVEN_CENTRAL_TOKEN # env variable 用于部署中的令牌
        gpg-private-key: $ # 要导入的gpg私钥的值
        gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for 用于gpg私钥passphrase

    - name: Publish to Apache Maven Central
      run: mvn deploy
      env:
        MAVEN_USERNAME: maven_username123
        MAVEN_CENTRAL_TOKEN: $
        MAVEN_GPG_PASSPHRASE: $

你的Maven的pom.xml中需要配置使用上述环境变量:

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
      <id>maven</id>
      <username>${env.MAVEN_USERNAME}</username>
      <password>${env.MAVEN_CENTRAL_TOKEN}</password>
    </server>
    <server>
      <id>gpg.passphrase</id>
      <passphrase>${env.MAVEN_GPG_PASSPHRASE}</passphrase>
    </server>
  </servers>
</settings>

当然,你的项目pom.xml还需要:

<build>
    <plugins>
        ...
        <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>
        ...
    </plugins>
</build>

<profiles>
    <profile>
        <id>release</id>
        <build>
            <plugins>
                ...
                <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>
    </profile>
</profiles>

 
注意点:
  • 如果没有MAVEN_GPG_PRIVATE_KEY,您需要使用以下命令导出GPG专用密钥: gpg --armor --export-secret-keys KEY_ID
  • 为避免gpg: signing failed: Inappropriate ioctl for device错误,您需要进行如下配置maven-gpg-plugin :
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-gpg-plugin</artifactId>
        <version>1.6</version>
        <configuration>
            <!-- Prevent gpg from using pinentry programs -->
            <gpgArguments>
                <arg>--pinentry-mode</arg>
                <arg>loopback</arg>
            </gpgArguments>
        </configuration>
        ...
    </plugin>

样本发布工作流程可在GitHub上找到。