开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第1天,点击查看活动详情
1、准备需要上传的仓库地址
以Github仓库为例: github.com/slightlee/m…
2、在 Sonatype 创建 Issue
2.1 注册 Sonatype 账号
发布 Java 包到 Maven 中央仓库,首先需要在 Sonatype 网站创建一个工单(Issues)。 第一次使用这个网站的时候需要注册自己的帐号(这个帐号和密码需要记住,后面会用到)。
2.2 创建 Issue
注册账号成功后,根据你 Java 包的功能分别写上Summary、Description、Group Id、SCM url以及Project URL等必要信息,可以参见我之前创建的 Issue:OSSRH-86665。
创建完之后需要等待 Sonatype 的工作人员审核处理,按照要求处理就好。我这里没有使用域名按照提示在github上面新创建了个临时仓库用作验证,当 Issue 的 Status 变为RESOLVED后,就可以进行下一步操作了。
3、发表快照和发布
central-ossrhBot Central-OSSRH added a comment - Yesterday Congratulations! Welcome to the Central Repository! io.github.slightlee has been prepared, now user(s) demain can: Publish snapshot and release artifacts to s01.oss.sonatype.org Have a look at this section of our official guide for deployment instructions: central.sonatype.org/publish/pub… Depending on your build configuration, your first component(s) might be released automatically after a successful deployment. If that happens, you will see a comment on this ticket confirming that your artifact has synced to Maven Central. If you do not see this comment within an hour or two, you can follow the steps in this section of our guide: central.sonatype.org/publish/rel…
可参考官方配置: central.sonatype.org/publish/pub… central.sonatype.org/publish/pub…
3.1 使用 GPG 生成公私钥对
3.1.1 安装 gpg
3.1.1.1 windows 安装 Gpg4win
Windows 系统,可以下载 Gpg4win 软件来生成密钥对。 Gpg4win 下载地址 安装后,执行命令 gpg --version 检查是否安装成功。
gpg --version
图形界面
3.1.1.2 mac 安装 gpg
方式1: www.gnupg.org/download/ 方式2: brew 安装
brew install gpg
3.1.2 生成密钥对
3.1.2.1 命令生成
gpg --gen-key
输入 Real name 、Email address 、输入 O 完毕之后弹窗提示输入保护密码
3.1.2.2 图形界面操作
3.1.3 查看公钥
gpg --list-keys
文件地址: C:\Users\ming\AppData\Roaming\gnupg\pubring.kbx
公钥: EA83BDE317D9301EE52D7BA75F7DE5F1222BC349
图形界面方式查看
3.1.4 发布公钥到 PGP 密钥服务器
gpg --keyserver keyserver.ubuntu.com --send-keys 公钥
gpg --keyserver keyserver.ubuntu.com --send-keys EA83BDE317D9301EE52D7BA75F7DE5F1222BC349
3.1.5 查看公钥是否发布成功
gpg --keyserver keyserver.ubuntu.com --recv-keys 公钥
gpg --keyserver keyserver.ubuntu.com --recv-keys EA83BDE317D9301EE52D7BA75F7DE5F1222BC349
3.2 Maven 配置
3.2.1 settings.xml 配置
<settings>
<servers>
<server>
<id>ossrh</id>
<!-- 用户名、密码就是 Sonatype 账号、密码 -->
<username>your-jira-id</username>
<password>your-jira-pwd</password>
</server>
</servers>
</settings>
<settings>
<profiles>
<profile>
<id>ossrh</id>
<properties>
<gpg.executable>gpg</gpg.executable>
<!-- gpg 的密码,注意这里不是指公钥 -->
<gpg.passphrase>the_pass_phrase</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
3.2.2 pom.xml 配置 (SNAPSHOT版)
<?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>io.github.slightlee</groupId>
<artifactId>mybatis-plus-generator-simple</artifactId>
<!-- 快照版 -->
<version>0.0.1-SNAPSHOT</version>
<name>mybatis-plus-generator-simple</name>
<description>simple mybatis-plus-generator</description>
<url>https://github.com/slightlee/mybatis-plus-generator-simple.git</url>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis-plus-generator.version>3.5.3</mybatis-plus-generator.version>
<mybatis-plus.version>3.5.2</mybatis-plus.version>
<velocity.version>2.3</velocity.version>
<junit-jupiter.version>5.9.1</junit-jupiter.version>
<spring.checkstyle.plugin>0.0.35</spring.checkstyle.plugin>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
</properties>
<dependencies>
...
</dependencies>
<!-- 发布仓库-->
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<scm>
<url>https://github.com/slightlee/mybatis-plus-generator-simple.git</url>
<connection>https://github.com/slightlee/mybatis-plus-generator-simple.git</connection>
<developerConnection>https://github.com/slightlee</developerConnection>
</scm>
<!-- 开源协议 -->
<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>
<!-- 开发者信息 -->
<developers>
<developer>
<name>demain_lee</name>
<email>lmm_work@163.com</email>
<organization>demain_lee</organization>
<organizationUrl>https://github.com/slightlee</organizationUrl>
</developer>
</developers>
<!-- bug提交地址 -->
<issueManagement>
<system>Github</system>
<url>https://github.com/slightlee/mybatis-plus-generator-simple/issues</url>
</issueManagement>
<build>
<plugins>
<!-- 指定maven.compiler.plugin 配置版本,解决编译问题 -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${java.version}</release>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<!--
github地址: https://github.com/spring-io/spring-javaformat
代码格式插件,默认使用spring 规则,可运行命令进行项目格式化:./mvnw spring-javaformat:apply 或 mvn spring-javaformat:apply
可在IDEA中安装插件以下插件进行自动格式化: https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin
-->
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring.checkstyle.plugin}</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 生成 Javadoc 和 Source jar 文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</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>2.9.1</version>
<!-- 排除格式警告 -->
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG 插件用于通过以下配置对组件进行签名。 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.2.3 pom.xml 配置 (RELEASE版)
<?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>io.github.slightlee</groupId>
<artifactId>mybatis-plus-generator-simple</artifactId>
<!-- RELEASE版 -->
<version>0.0.1-RELEASE</version>
<name>mybatis-plus-generator-simple</name>
<description>simple mybatis-plus-generator</description>
<url>https://github.com/slightlee/mybatis-plus-generator-simple.git</url>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis-plus-generator.version>3.5.3</mybatis-plus-generator.version>
<mybatis-plus.version>3.5.2</mybatis-plus.version>
<velocity.version>2.3</velocity.version>
<junit-jupiter.version>5.9.1</junit-jupiter.version>
<spring.checkstyle.plugin>0.0.35</spring.checkstyle.plugin>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
</properties>
<dependencies>
...
</dependencies>
<!-- 发布仓库-->
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<scm>
<url>https://github.com/slightlee/mybatis-plus-generator-simple.git</url>
<connection>https://github.com/slightlee/mybatis-plus-generator-simple.git</connection>
<developerConnection>https://github.com/slightlee</developerConnection>
</scm>
<!-- 开源协议 -->
<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>
<!-- 开发者信息 -->
<developers>
<developer>
<name>demain_lee</name>
<email>lmm_work@163.com</email>
<organization>demain_lee</organization>
<organizationUrl>https://github.com/slightlee</organizationUrl>
</developer>
</developers>
<!-- bug提交地址 -->
<issueManagement>
<system>Github</system>
<url>https://github.com/slightlee/mybatis-plus-generator-simple/issues</url>
</issueManagement>
<build>
<plugins>
<!-- 指定maven.compiler.plugin 配置版本,解决编译问题 -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${java.version}</release>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<!--
github地址: https://github.com/spring-io/spring-javaformat
代码格式插件,默认使用spring 规则,可运行命令进行项目格式化:./mvnw spring-javaformat:apply 或 mvn spring-javaformat:apply
可在IDEA中安装插件以下插件进行自动格式化: https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin
-->
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring.checkstyle.plugin}</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 生成 Javadoc 和 Source jar 文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</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>2.9.1</version>
<!-- 排除格式警告 -->
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG 插件用于通过以下配置对组件进行签名。 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<!-- release 版配置-->
<profile>
<id>release</id>
<build>
<plugins>
<!-- 用于部署和发布的 Nexus Staging Maven 插件-->
<plugin>
<!-- https://mvnrepository.com/artifact/org.sonatype.plugins/nexus-staging-maven-plugin -->
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
3.3 打包部署上传
3.3.1 SNAPSHOT版
3.3.1.1 发布
执行命令 mvn clean deploy
mvn clean deploy
3.3.2 查看上传的快照
仓库地址: s01.oss.sonatype.org 账号密码对应 issues.sonatype.org 注册的账号密码 可以看到快照版本
3.3.2 RELEASE版
3.3.2.1 修改项目的版本命令
例如 0.0.1-RELEASE
mvn versions:set -DnewVersion=0.0.1-RELEASE
3.3.2.2 发布
执行命令 mvn clean deploy -P release 注意:pom.xml 新增的 profiles 配置
mvn clean deploy -P release
可以看到
点击左侧的 Staging Repositories 可以看到刚才上传的版本,选中 点击上方的 Close –> Confirm 然后查看状态,当状态变成closed后, 操作 Release -> Confirm 。成功后会自动删除,过一段时间即可同步到 maven 中央仓库。
完成之后会收到 邮件通知,Sonatype 上面也会有新的回复 。
io.github. slightlee的中央同步被激活。在你成功发布后,你的组件将在中央repo1.maven.org/maven2/,通常在30分钟内向公众开放,不过更新到search.maven.org 可能需要4个小时。
4、使用
4.1 中央仓库搜索 jar包
central.sonatype.dev/ 搜索 mybatis-plus-generator-simple search.maven.org/ 搜索 mybatis-plus-generator-simple
maven仓库 搜索 mybatis-plus-generator-simple (截止目前未搜索到,不影响正常使用)
4.2 使用
<dependency>
<groupId>io.github.slightlee</groupId>
<artifactId>mybatis-plus-generator-simple</artifactId>
<version>0.0.1-RELEASE</version>
</dependency>
@Test
public void Test() {
GeneratorInfoConfig generatorInfoConfig = new GeneratorInfoConfig.Builder().author("demain_lee").dbUrl(
"jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT%2B8")
.username("root").password("root")
// .packageName("com.xxx.xxx")
// .outputDir("C:\\Users\\ming\\")
// .mapperDir("C:\\Users\\ming\\mapper")
.tablePrefix("t_").includeTable().excludeTable("t_tag").idType(IdType.ASSIGN_ID)
// .templateEngine(new VelocityTemplateEngine())
// .enableSwagger()
// .enableLombok()
.build();
CodeGeneratorConfig.runCodeGenerator(generatorInfoConfig);
}