一、前言
- 在现实Java微服务开发中,经常需要抽取一些通用的接口或者公共的方法类,并打包成jar提供给其它服务使用。对开源架构进行二次封装,并通过springBoot的Starter,作为公司基础架构。通常这些jar都会被推送到公司的私服nexus中。
- 但是作为个人开发者,我们没有那么都资源取搭建自己的私服nexus,但是我们又有公共或者基础架构的jar。所以我们需要把jar推送至maven中央仓库,并提供自己项目依赖。本文从0到1实现自定义jar上传到Maven中央仓库。
二、申请账号
2.1 注册账号
在sonatype的jira(issues.sonatype.org/)上注册账号
2.2 新建项目
注意:
项目:Community Support-Open
问题类型:New Project
概要:(根据自己项目填写)
groupId:如果有自己域名的就自己域名com.xxx;如果用的gitee那么填写com.gitee.xxx(github类似);
例如:
groupId: com.gitee.xmhzzz
projectUrl:gitee.com/xmhzzz/micr…
scmurl: gitee.com/xmhzzz/micr…
2.3 验证个人仓库和修改错误信息
提交创建问题后,大概1-2小时会有回复。在注释可以查看到管理员回复。
我因为第一次填写groupId 写的是不存在的域名,有点问题需要修改了重新提交。
管理员回复没问题后。
在gitee上创建新建公开仓库并且名称是管理员告诉你的仓库,用来验证是个你自己的个人账号。
并回复:
i have add the public repo named OSSRH-94265,you can verify the gitee account.
管理员开通后,会回复:
2.4 验证是否开通
通过刚才注册的账号登录s01.oss.sonatype.org/
如果成功说明开通成功
三、生成密钥
3.1 下载安装Gpg4Win
官网:www.gpg4win.org/
在电脑安装Gpg4Win
3.2 生成密匙
3.3 发布密钥
四、配置maven
4.1配置settings
配置servers
<servers>
<server>
<id>ossrh</id>
<username>issues.sonatype.org账号</username>
<password>issues.sonatype.org密码</password>
</server>
</servers>
配置profiles
<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>Gpg4Win位置D:\software\gpg\bin\gpg.exe</gpg.executable>
<gpg.passphrase>生成密钥时的密码</gpg.passphrase>
<gpg.homedir>秘钥在磁盘上的位置C:\Users\12463\AppData\Roaming\gnupg</gpg.homedir>
</properties>
</profile>
</profiles>
五、准备项目
5.1准备发布的SpringBoot项目
5.2配置项目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>com.gitee.xmhzzz</groupId>
<artifactId>micro-component</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<modules>
<!-- <module>component-dependencies</module>-->
<module>component-spring</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--开源协议-->
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<!--项目地址-->
<url>https://gitee.com/xmhzzz/micro-component.git</url>
<!--开发者信息-->
<developers>
<developer>
<id>xmhzzz</id>
<name>xmhzzz</name>
<email>994124252@qq.com</email>
<roles>
<role>Developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
<!--项目在github或其它托管平台的地址-->
<scm>
<connection>https://gitee.com/xmhzzz/micro-component.git</connection>
<developerConnection>scm:git:ssh://git@gitee.com:xmhzzz/micro-component.git</developerConnection>
<url>https://gitee.com/xmhzzz/micro-component/tree/master/</url>
</scm>
<dependencyManagement>
<dependencies>
<!-- springboot与springcloud版本 start -->
<dependency>
<groupId>com.gitee.xmhzzz</groupId>
<artifactId>component-dependencies</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<testCompilerArgument>-parameters</testCompilerArgument>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<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>
</project>