发布包到maven公共仓库图文教程(2) --- gpg签名和pom.xml的配置等

在操作这篇文章之前你需要做一些账号注册和提交申请前置操作, 前置操作我已经写了另一篇博客, 请移步 链接在此

这篇接着讲如何使用gpg和和配置发布信息。 因为内容有点多, 所以最重要的打包和发布环节在第三篇博客中讲解,敬请期待。

各位读者,能否给我这个小博主一个关注点赞,谢谢。

整个教程分三篇博客讲解

申请账号

链接在此

提交申请

链接在此

GPG签名使用

发布jar包需要使用gpg对你发布的包进行签名。 这段主要讲如何安装使用gpg。

GnuPG,简称 GPG,来自 http://www.gnupg.org,是 GPG 标准的一个免费实现。不管是 Linux 还是 Windows 平台,都可以使用。GPGneng 可以为文件生成签名、管理密匙以及验证签名。

下载安装 GPG

访问 http://www.gnupg.org/download,下载适合自己操作系统平台的安装程序。
mac系统推荐使用brew下载安装。
window系统可以下载gpg4win-2.3.3.exe。下载下来后正常安装即可。

生成秘钥

在这里插入图片描述
安装完成后, 桌面上会出现如图所示的图标,点开它。
在这里插入图片描述

如果是首次点开,图中红框部分会出现两个选项. 分别是生成新秘钥和导入秘钥。 我们选择新建秘钥

在这里插入图片描述

输入姓名和电子邮件,名称和邮件的组合不要和别人重复, 点击下一步。

在这里插入图片描述

会出现让你输入密码, 这个密码你可以自己设置, 设置好后要记住,后面会用到。 点击OK

在这里插入图片描述

出现下一步的三个选项, 我们先完成最重要的将公钥上传到
公共目录服务器

在这里插入图片描述

出现图中弹窗,表示上传成功了。 成功后的页面如下

在这里插入图片描述

图中就是显示的你生成的证书信息

项目的pom文件的配置

接下来我会分段讲解pom文件中的各个部分注意事项。并会在最后贴出完整的pom文件

基本信息

   <modelVersion>4.0.0</modelVersion>
    <groupId>xyz.xiezc</groupId>
    <artifactId>yao</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>yao</name>
    <description>一个依赖注入的框架, 整合netty 提供类似springMvc的能力。 整合mybatis提供类似mybatis-spring的能力。</description>
    <url>https://github.com/blanexie/Yao</url>

其中需要注意的是url填写你的项目github地址。 description简单描述下你的项目。

licenses 证书信息

    <licenses>
        <license>
            <name>Mulan Permissive Software License,Version 1</name>
            <url>https://license.coscl.org.cn/MulanPSL/</url>
        </license>
    </licenses>

这里需要贴出你的开源证书的名称和证书的url。 我使用是木兰宽松许可。 所以贴出的是上面的地址,

  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>

Apache 协议 2.0
其他协议类似

scm软件配置管理插件

    <scm>
        <connection>scm:git:https://github.com/blanexie/Yao.git</connection>
        <developerConnection>scm:git:https://github.com/blanexie/Yao.git</developerConnection>
        <url>git:https://github.com/blanexie/Yao.git</url>
    </scm>

这里配置你的github的信息。 参考我的配置就行

scm 具体有何作用我也搞不懂, 希望知道的读者能在评论中告知我,谢谢。

配置开发者信息

    <developers>
        <developer>
            <name>blanexie</name>
            <email>[email protected]</email>
        </developer>
    </developers>

这里就是配置开发者信息了, 如果有多个开发者,可以添加多个developer节点。

distributionManagement配置仓库信息

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

这里就是配置上你之前的在发布申请的管理员会在评论中告知你的两个地址。这里需要注意两个地址不要弄混了。 同时需要记住你配置的两个id, 后面会使用到这两个id。

build打包配置

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <target>${java.version}</target>
                    <source>${java.version}</source>
                    <encoding>${java.encoding}</encoding>
                </configuration>
            </plugin>
            <!-- Javadoc -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <encoding>${java.encoding}</encoding>
                    <charset>${java.encoding}</charset>
                    <docencoding>${java.encoding}</docencoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <additionalJOption>-Xdoclint:none</additionalJOption>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • maven-source-plugin 插件必须配置,这个插件是将你的源码一起打包, maven仓库必须需要源码。 所以这个不能少。 具体可以参考我配置
  • maven-javadoc-plugin 插件必须, 这是安装你的代码中方法的注释生成javadoc文档的插件, 也是maven仓库必须的插件, 注意配置这个插件additionalJOption的值为true。 additionalJOption是告知maven忽略不规范的java注释。
  • maven-compiler-plugin 这个插件是配置编译的jdk版本信息等

profiles多环境配置

<profiles>
        <profile>
            <id>release</id>
            <activation>
                <jdk>11</jdk>
            </activation>
            <properties>
                <additionalparam>-Xdoclint:none</additionalparam>
            </properties>

            <build>
                <plugins>
                    <!-- Source -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>snapshot</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>release</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Gpg Signature -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>release</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.8</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>release</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

这中间也配置了build.。其中build中的插件有

  • maven-gpg-plugin 签名插件,这个插件会调用你本地电脑的gpg程序来签名代码。 到时候签名会有很多问题。后面部分会详细说明签名的问题。 这个也是maven仓库必须的插件。参考我的配置即可
  • nexus-staging-maven-plugin 这是帮助我们上传jar包到仓库的插件, 按照我的配置即可
  • maven-source-plugin 这个也是打包源码的插件,为何这里也有一个我也不知道。 希望知道的读者在评论告知。谢谢。

为何要这里定义一个release的profiles环境呢。 这个环境中maven-gpg-pluginnexus-staging-maven-plugin 插件都是只有发布包到maven仓库中才需要的,但是我们平时开发中, 并不是每次都是上传到公共仓库的,所以可以把上传需要的配置提取出来单独放在profile中是很好的处理方法, 平时就使用其他环境来打包。

上面已经分开把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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xyz.xiezc</groupId>
    <artifactId>yao</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>yao</name>
    <description>一个依赖注入的框架, 整合netty 提供类似springMvc的能力。 整合mybatis提供类似mybatis-spring的能力。</description>
    <url>https://github.com/blanexie/Yao</url>

    <properties>
        <app.name>yao</app.name>
        <maven.test.failure.ignore>false</maven.test.failure.ignore>
        <maven.test.skip>false</maven.test.skip>
        <java.version>11</java.version>
        <java.encoding>UTF-8</java.encoding>
        <project.build.sourceEncoding>${java.encoding}</project.build.sourceEncoding>
        <maven.build.timestamp.format>yyyy-MM-dd_HH_mm</maven.build.timestamp.format>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.compilerVersion>${java.version}</maven.compiler.compilerVersion>
    </properties>

    <modules>
        <module>xioc</module>
        <module>example</module>
        <module>xweb</module>
        <module>xorm</module>
    </modules>


    <dependencies>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>xyz.xiezc</groupId>
                <artifactId>xweb</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>xyz.xiezc</groupId>
                <artifactId>xioc</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>xyz.xiezc</groupId>
                <artifactId>xorm</artifactId>
                <version>${project.version}</version>
            </dependency>

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.3.0</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.4</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.20</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
            <dependency>
                <groupId>org.ow2.asm</groupId>
                <artifactId>asm</artifactId>
                <version>8.0.1</version>
            </dependency>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.49.Final</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <target>${java.version}</target>
                    <source>${java.version}</source>
                    <encoding>${java.encoding}</encoding>
                </configuration>
            </plugin>
            <!-- Javadoc -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <encoding>${java.encoding}</encoding>
                    <charset>${java.encoding}</charset>
                    <docencoding>${java.encoding}</docencoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <additionalJOption>-Xdoclint:none</additionalJOption>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <licenses>
        <license>
            <name>Mulan Permissive Software License,Version 1</name>
            <url>https://license.coscl.org.cn/MulanPSL/</url>
        </license>
    </licenses>

    <scm>
        <connection>scm:git:https://github.com/blanexie/Yao.git</connection>
        <developerConnection>scm:git:https://github.com/blanexie/Yao.git</developerConnection>
        <url>git:https://github.com/blanexie/Yao.git</url>
    </scm>

    <developers>
        <developer>
            <name>blanexie</name>
            <email>[email protected]</email>
        </developer>
    </developers>

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


    <profiles>
        <profile>
            <id>release</id>
            <activation>
                <jdk>11</jdk>
            </activation>
            <properties>
                <additionalparam>-Xdoclint:none</additionalparam>
            </properties>

            <build>
                <plugins>
                    <!-- Source -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>snapshot</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>release</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Gpg Signature -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>release</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.8</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>release</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

maven的setting.xml文件配置

我使用的setting.xml配置文件在我用户目录的.m2目录下。注意要改到正确的maven使用的setting.xml文件才有效

 <servers>
       <server> 
        <id>snapshot</id> 
        <username>xiezc</username> 
        <password>***********(马赛克)</password> 
    </server>
      <server> 
        <id>release</id> 
        <username>xiezc</username> 
        <password>***********(马赛克)</password> 
    </server>
  </servers>

在你的setting.xml文件中增加两个server节点。其中id节点就是你上面的pom文件的distributionManagement 节点的两个仓库的id。 注意这两个id一定要上面的pom文件中distributionManagement 节点配置的id一样
这两个节点的username就是你之前注册的账号的用户名, 密码是要你之前记住的注册时候的密码,如果忘记可以通过注册邮箱找回。 上一篇博客中我已经强调了要记住注册密码的。

maven打包项目

走完上面的流程,基本的前置工作已经完成了,下面就是激动人心的打包项目环节了。

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