一。开源日志框架介绍
最近在项目中一直需要记录日志或者打印日志,在每个项目都需需要额外通过AOP方式来拦截,控制日志,结合springboot
将日志的记录写成了一个组件,开源到github上。
github地址:https://github.com/jiaozi789/cherry-tool-log
二。上传sonatype仓库步骤
Sonatype OSSRH介绍:
Sonatype OSSRH使用Nexus 为开源项目提供仓库管理服务,该仓库就是所谓maven的中央仓库,OSSRH允许我们向Maven中央仓库提交二进制文件。
- 提交(deploy)开发版本的二进制文件(snapshorts)
- 阶段性的发布版本
- 发布一个release,然后同步他们到中央仓库。
注册Sonatype
1:注册一个JIRA账号:https://issues.sonatype.org/secure/Signup!default.jspa
2:创建一个新工程的单:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134
只有当这个jira单的状态我resolved时,才可以提交jar包
新建工单动态图:
其中注意表单中一定要选中有groupid的那个,因为其中有两个project图标和内容很像,如果不知道选哪个,登录后直接输入路径:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134
Summary:表示概览,简要描述一下项目,比如:Open Source Framework for Logging
Description:表示详细描述。
Group Id:表示你项目的maven的group,注意如果是 cn.abc,系统询问 abc.cn是否是你拥有的域名,你可以使用
你自己的gihub账号作为group 比如我的:io.github.jiaozi789.tool,注意这里填写的一定要和将来上传的jar
的group一致否则无法上传,一定不要写错,反正系统上改是很麻烦的。
Project URL:表示你项目位置,上传你的源代码到你的github地址比如:https://github.com/jiaozi789/cherry-tool-log
SCM url:源代码位置 一样的gihub 比如:https://github.com/jiaozi789/cherry-tool-log.git
Username(s):允许哪些用户发布你的maven,不填或者填你自己的用户名
点击create创建完成,点击issue找到自己的
当然刚发布 issue中的Status是 unresolved 等待差不多一天时间,变成绿色的Resolved就可以看到下面有个comment,就可以
上传jar包了
配置项目上传jar
》》 gpg生成密钥对
sonatype要求所有上传的jar包源代码doc都需要使用 gpg加密,我在window开发,安装软件包gpg4win
https://www.gpg4win.org/download.html
该软件会安装客户端软件辅助来生成公私密钥,可以使用gpg命令
打开桌面快捷方式Kleopatra,选择菜单文件-新建
选择个人OpenPGP密钥对,输入名字和个人邮箱(这个可以随意)
输入密钥对的密码,确认密码(这个密码将来发布要用的)
可以再软件列表中查看,可以使用命令行查看(cmd)
C:/Users/Administrator/AppData/Roaming/gnupg/pubring.kbx
--------------------------------------------------------
pub rsa2048 2019-01-15 [SC] [expires: 2021-01-15]
695D60D04322E6892665CD22183EE19ED3372914
uid [ultimate] lixin1112003 <lixin1112003@126.com>
sub rsa2048 2019-01-15 [E] [expires: 2021-01-15]
可以使用该工具安装的另外一个客户端工具 GPA 查看每个密钥对的唯一标识符 keyid
执行命令查看(其实keyid就是公钥拇印的最后8位数字):
C:\Users\Administrator>gpg --list-keys D3372914
pub rsa2048 2019-01-15 [SC] [expires: 2021-01-15]
695D60D04322E6892665CD22183EE19ED3372914
uid [ultimate] lixin1112003 <lixin1112003@126.com>
sub rsa2048 2019-01-15 [E] [expires: 2021-01-15]
关于其他的命令(上传key,使用公钥加密,私钥解密,私钥签名,公钥验证等),自行百度
》》 gpg密钥对上传到公共服务器
因为仓库在发布release版本的jar包时需要通过公钥去验证你上传的jar包的签名,我们可以讲公钥上传到公共的位置
以下是上传过程:
C:\Users\Administrator>gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys D3372914
gpg: sending key 183EE19ED3372914 to hkp://keyserver.ubuntu.com:11371
我之前就是弄错了keyid 结果没上传成功 使用maven安装时 报错:
[ERROR] Rule failure while trying to close staging repository with ID "iogithubjiaozi789-1000".
[ERROR]
[ERROR] Nexus Staging Rules Failure Report
[ERROR] ==================================
[ERROR]
[ERROR] Repository "iogithubjiaozi789-1000" failures
[ERROR] Rule "signature-staging" failures
[ERROR] * No public key: Key with id: (183ee19ed3372914) was not able to be located on <a href=http://keys.g
nupg.net:11371/>http://keys.gnupg.net:11371/</a>. Upload your public key and try the operation again.
[ERROR] * No public key: Key with id: (183ee19ed3372914) was not able to be located on <a href=http://pool.s
ks-keyservers.net:11371/>http://pool.sks-keyservers.net:11371/</a>. Upload your public key and try the ope
ration again.
[ERROR] * No public key: Key with id: (183ee19ed3372914) was not able to be located on <a href=http://keyser
ver.ubuntu.com:11371/>http://keyserver.ubuntu.com:11371/</a>. Upload your public key and try the operation
again.
[ERROR]
[ERROR]
[ERROR] Cleaning up local stage directory after a Rule failure during close of staging repositories: [iogithubjiaoz
i789-1000]
[ERROR] * Deleting context 909919ad19ae8a.properties
[ERROR] Cleaning up remote stage repositories after a Rule failure during close of staging repositories: [iogithubj
iaozi789-1000]
[ERROR] * Dropping failed staging repository with ID "iogithubjiaozi789-1000" (Rule failure during close of stagin
g repositories: [iogithu
》》 配置你在sonatype的用户名和密码
打开maven软件包conf/setting.xml,新增服务器
<servers>
<server>
<id>sonatype-nexus-snapshots</id><!--名字表示用于快照库-->
<username>你的用户名</username>
<password>你的密码</password>
</server>
<server>
<id>sonatype-nexus-staging</id><!--名字表示用于发布库-->
<username>你的用户名</username>
<password>你的密码</password>
</server>
</servers>
添加一个profile,用于表示获取你的私钥用于签名的命令 就是gpg
<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.passphrase>你之前新建密钥对是确认过的密码</gpg.passphrase>
</properties>
</profile>
</profiles>
》》 配置打包测试版本
修改项目的pom.xml
注意测试版本版本号(必须-SNAPSHOT结尾):
<version>0.0.1-SNAPSHOT</version>
配置授权协议(使用github或者其它地方的开源项目时候,需要注意开源项目的授权协议。开源不等于免费使用,如果公司使用开源项目时候违反其开源协议,有可能给公司或者个人带来版权纠纷。使用时候需要慎重阅读开源代码提供者的授权条件。)
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
设置代码托管位置:
<scm>
<url>https://github.com/jiaozi789/cherry-tool-log.git</url>
</scm>
设置开发者的信息:
<developers>
<developer>
<name>jiaozi</name>
<email>973465719@qq.com</email>
<organization>jiaozi</organization>
<organizationUrl>https://github.com/jiaozi789</organizationUrl>
</developer>
</developers>
设置发布的位置:
注意id和setting.xml的server的id一致
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
设置默认build的插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--打包源代码-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!--生成javadoc-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--获取本地私钥签名-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
上面的配置就可以直接deploySNASHOT的版本的,maven中执行命令
mvn clean deploy
注意你项目的group必须和你申请工单的group 一致 否则会被拒绝
》》 配置打包正式版本
修改项目的版本
<version>0.0.1-RELEASE</version>
正式版本发布和测试版本配置一致 需要额外添加一个依赖 ,指定一个profile在运行时 通过激活profile
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>sonatype-nexus-staging</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
执行命令:
mvn clean deploy -P release
如果上传出错
* Invalid POM: /io/github/gvtcy/gvt_sdk_apollo/1.0-RELEASE/gvt_sdk_apollo-1.0-RELEASE.pom: Project name missing, Project description missing, Project URL missing
一般需要在自己的pom.xml添加项目名称和描述,如:
<name>gvt_sdk_apollo</name>
<description>第三方接口api</description>
一般上传过程会需要输入密钥的密码,然后就是等待大概1分钟左右上传jar包成功,可以到到sonatype上搜索
接下来在之前那个工单上添加一个comment通知sonatype同步到中央仓库
I have released a release, coordinates:
<groupId>io.github.jiaozi789.tool</groupId>
<artifactId>cherry-tool-log</artifactId>
<version>0.0.1-RELEASE</version>
Please synchronize the coordinates to the central warehouse,thanks
等待一天后查看issue工单
中央仓库搜索发现release版本已经能够查看到了