前言
- 因为自己开源了一个jar包,就想着如何把它发布到公网上。但是把它发布到maven中央仓库太麻烦,所以就想到自己搭建个私服,并且同时想在公网上访问到它。经过一番调研后,最终选择了github作为maven中央仓库。同时自己也参考了其他的博主发布的一些教程,但是并没有很详细,于是自己总结下流程。详细你看完后一定会懂的。ps: 结尾有彩蛋!!!
一、修改maven配置文件
- 找到本地maven的配置文件
setting.xml
,找到servers标签添加如下内容:<server> <!-- 记住此id, 后续发布jar包时要使用 --> <id>github</id> <username>github的登录用户名</username> <password>github的登录密码</password> </server>
二、创建github仓库
- 使用github作为maven仓库,也需要github的一个仓库来存储maven打出来要发布的jar包信息,这样才能在github中访问到
- github创建仓库的教程这里就不阐述了。而我创建的仓库名叫
maven-repository
三、配置github的profile中的name
- 配置这个的原因是防止在发布的过程中抛出如下错误
[ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project rfcore: Error creating commit: Invalid request. [ERROR] For 'properties/name', nil is not a string. [ERROR] For 'properties/name', nil is not a string. (422) [ERROR] -> [Help 1]
四、添加发布jar包pom配置
- 在需要发布jar包的项目(在这里我要发布项目的
artifactId
叫spring-boot-zookeeper-client-starter
)中的pom文件添加如下内容:<properties> <!-- 此处配置的名称要和maven配置文件对应的serverId一致 --> <github.global.server>github</github.global.server> </properties> <build> <!-- 此tag下面的所有plugins都是关于上传jar包的依赖 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <!-- 配置本地打包后的本地仓库存储地址,后续上传jar包会从此仓库中去取 --> <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/maven-repository</altDeploymentRepository> </configuration> </plugin> <plugin> <groupId>com.github.github</groupId> <artifactId>site-maven-plugin</artifactId> <version>0.12</version> <configuration> <message>Maven artifacts for ${project.artifactId}-${project.version}</message> <noJekyll>true</noJekyll> <!-- 指定从哪里去取打好的包,并上传至github --> <outputDirectory>${project.build.directory}/maven-repository</outputDirectory> <!-- 指定要上传的分支, refs/heads 这个不变,后面的分支名可选,可以采取一个jar包使用一个分支的策略。 若多个jar包同时发布在同一个分支的话,会覆盖。。。。 --> <branch>refs/heads/master</branch> <!-- 包含outputDirectory标签内填的文件夹中的所有内容 --> <includes> <include>**/*</include> </includes> <!-- github远程存储outputDirectory标签内填的文件夹中的内容 --> <repositoryName>maven-repository</repositoryName> <!-- github的用户名,注意不是登录的用户名,此项需要登录后,进入https://github.com/settings/profile页面配置Name属性, 否则会报 [ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project rfcore: Error creating commit: Invalid request. [ERROR] For 'properties/name', nil is not a string. [ERROR] For 'properties/name', nil is not a string. (422) [ERROR] -> [Help 1] 的错误 --> <repositoryOwner>AvengerEug</repositoryOwner> </configuration> <executions> <execution> <goals> <goal>site</goal> </goals> <phase>deploy</phase> </execution> </executions> </plugin> </plugins> </build>
- 在项目根目录下(pom.xml文件所在的目录)执行如下命令
mvn clean deploy
- 执行完此命令最终可以在target目录下查看到下述文件:(ps: maven-repository是在上述的pom文件中配置的,后续会将这个文件夹内的所有东西都上传至github)
然后直接在github中访问项目
https://github.com/AvengerEug/maven-repository, 可以看到,上述图片中的所有文件都上传到了github中
五、在项目中引入github中的jar包
- 添加发布jar包的依赖
<!-- 刚刚发布的jar包依赖, 根据自己的实际发布项目来配置 --> <dependency> <groupId>com.eugene.sumarry</groupId> <artifactId>spring-boot-zookeeper-client-starter</artifactId> <version>0.0.1</version> </dependency>
- 添加远程仓库地址:
<repositories> <repository> <id>github</id> <name>avengerEug</name> <!-- 1. https://raw.github.com => 固定写法, 若编译的时候一直连接不上(Connection reset),可将https改成http并反复重试,如果还是连接不上的话,大家都懂的,国外的网站嘛,多连接(编译)几次就可以了 2. AvengerEug => github登录的用户名 3. maven-repository => 存储发布的jar仓库 4. master => 从哪个分支中拿jar包 --> <url>https://raw.github.com/AvengerEug/maven-repository/master</url> </repository> </repositories>
- 注意事项:
1. github中的仓库基本上是一个分支对应一个发布的jar包,若需要拉取其他分支的jar包则需要新增reposotiry的配置。 2. 同一个仓库要发布不同的jar包,则可以使用不同的分支存储(上面在配置pom.xml中有讲到),然后配置的repository中的分支名也要记得修改
六、总结
- 最后利用此篇博客推荐下自己开源的一个jar包。链接: https://github.com/AvengerEug/spring-boot-zookeeper-client-starter ,若觉得不错,恳请各位给个Star
- I am a slow walker, but I never walk backwards.