利用 GitHub 的 Action 将 jar 包推送到 Maven 中心仓库

配置 SpringBoot 项目

pom.xml

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?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">

    <!-- 基本信息 -->
    <name>Gear Common GIS</name>
    <description>A common GIS utility library for Java applications</description>
    <url>https://github.com/gearinger/gear-common-gis</url>

    <!-- 许可证信息 -->
    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <!-- 开发者信息 -->
    <developers>
        <developer>
            <name>GuoYingdong</name>
            <email>gearingxz@gmail.com</email>
            <organization>Gearinger</organization>
            <organizationUrl>https://github.com/gearinger</organizationUrl>
        </developer>
    </developers>

    <!-- SCM 信息 -->
    <scm>
        <url>https://github.com/gearinger/gear-common-gis</url>
    </scm>

    <modelVersion>4.0.0</modelVersion>

    <groupId>io.github.gearinger</groupId>
    <artifactId>gear-common-gis</artifactId>
    <version>0.0.1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <geotools.version>27.0</geotools.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson-core</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geotiff</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <!--        shapefile-->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>osgeo</id>
            <name>OSGeo Release Repository</name>
            <url>https://repo.osgeo.org/repository/release/</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                    <encoding>${encoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.3.0</version>
                <inherited>false</inherited>
                <configuration>
                    <nohelp>true</nohelp>
                    <charset>${encoding}</charset>
                    <encoding>${encoding}</encoding>
                    <docencoding>${encoding}</docencoding>
                    <failOnError>false</failOnError>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.6</version>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                        <configuration>
                            <gpgArguments>
                                <arg>--pinentry-mode</arg>
                                <arg>loopback</arg>
                            </gpgArguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.8.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>central</publishingServerId>
                    <autoPublish>true</autoPublish>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

注册 Maven 中心仓库的账号

注册

Maven 中心仓库:https://central.sonatype.com/

创建 token

图 0

保存下 username 和 password

创建 namespace

点击 Publishing - namespace - Register New Namespace,输入 io.github.gearinger 后提交,根据提示进行验证。

如需推送不稳定版本,可配置 Enable SNAPSHOTs

创建 GPG 密钥

下载 Gpg4win:https://www.gpg4win.org/get-gpg4win.html

安装后,点击文件 - 新建 OpenPGP 密钥对,勾选使用密码保护密钥,记下输入的 gpg 密码,根据提示完成。

然后,选中密钥,点击文件 - 在服务器上发布,根据提示完成。

命令行导出 gpg 私钥:

1
gpg -a -o private-file.key --export-secret-keys

配置 GitHub Action

创建 Action

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
name: Push Jar To Maven Central
on:
  # 发布版本时触发
  release:
    types: [ released ]
jobs:
  publish-central:
    runs-on: ubuntu-latest
    steps:

      - name: Checkout Git Repo
        uses: actions/checkout@v4

      - name: Set up Apache Maven Central
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '11'
          server-id: central
          server-username: SERVER_USERNAME
          server-password: SERVER_PASSWORD
          gpg-private-key: ${{ secrets.GPG_SECRET }}
          gpg-passphrase: GPG_PASSPHRASE

      - name: Publish to Apache Maven Central
        run: mvn deploy
        env:
          SERVER_USERNAME: ${{ secrets.SERVER_USERNAME }}
          SERVER_PASSWORD: ${{ secrets.SERVER_PASSWORD }}
          GPG_PASSPHRASE: ${{ secrets.GPG_PASSWORD }}

配置变量

进入 Github 对应仓库,点击 Settings - Secrets And Variables - Action - new repository secret

图 1

SERVER_USERNAME:生成的userToken中的username SERVER_PASSWORD:生成的userToken中的password GPG_SECRET:gpg 的私钥,—–BEGIN PGP PRIVATE KEY BLOCK—–开头那一大串 GPG_PASSWORD:创建 gpg 密钥串时的密码

发布版本,推送到 Maven Central

进入 Github 仓库,点击 Releases - Draft a new release,根据提示完成。

查看 Github Action 运行情况

查看 Maven Central 运行情况:https://central.sonatype.com/publishing

有问题可根据 Github Action 和 Maven Central 的部署日志排查。

Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……