小白学习使用github作为maven仓库(一):cloud-component-security-starter

169 阅读4分钟

前言

因为自己开源了一个starter,就想着如何把它发布到公网上。但是把它发布到maven中央仓库太麻烦,所以就想到自己搭建个私服,并且同时想在公网上访问到它。经过一番调研后,最终选择了github作为maven中央仓库。同时自己也参考了其他的博主发布的一些教程,但是并没有很详细,于是自己总结下流程。相信你看完后一定会懂的。

本文只是教大家发布jar到自己的github仓库,且该种方式发布的包只能供发布者自己使用,不能被其他人使用。

一、修改maven配置文件

找到本地maven的配置文件setting.xml,找到servers标签添加如下内容:

<server>
  <!-- 记住此id, 后续发布jar包时要使用 -->
  <id>github</id>
  <username>github的登录用户名</username>
  <password>token</password>
</server>

上面的token需要登陆后自己去生成。步骤如下:

  1. 点击用户头像 image.png
  2. 点击setting image.png
  3. 点击Developer Settings image.png
  4. 展开Personal access tokens image.png
  5. 点击Tokens (classic) image.png
  6. 展开Generate new token并点击Generate new token (classic) image.png

image.png 这个 token 切记增加user:email权限。勾选下面的复选按钮 image.png 7. 生成token image.png

二、创建github仓库

使用github作为maven仓库,也需要github的一个仓库来存储maven打出来要发布的jar包信息,这样才能在github中访问到。我创建的仓库名叫maven-repository

备注:这里不需要单独创建organization,直接在默认组织下创建repository

三、配置githubprofile中的name\color{red}三、配置github的profile中的name

image.png 配置这个的原因是防止在发布的过程中抛出如下错误

[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包的项目(在这里我要发布项目的artifactIdcloud-component-security-starter)中的pom文件添加如下内容:
<properties>
   <java.version>8</java.version>
   <!-- 此处配置的名称要和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/dependency</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>hehepeng</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文件中配置的,后续会将这个文件夹内的所有东西都上传至githubimage.png 然后直接在github中访问项目, 可以看到,上述图片中的所有文件都上传到了github中

五、在项目中引入github中的jar包

  • 添加发布jar包的依赖
<!-- 刚刚发布的jar包依赖, 根据自己的实际发布项目来配置 -->
<dependency>
    <groupId>com.gateway.security</groupId>
    <artifactId>cloud-component-security-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

注意事项:

  1. github中的仓库基本上是一个分支对应一个发布的jar包,若需要拉取其他分支的jar包则需要新增reposotiry的配置。
  2. 同一个仓库要发布不同的jar包,则可以使用不同的分支存储(上面在配置pom.xml中有讲到),然后配置的repository中的分支名也要记得修改

问题\color{red}问题

若遇见如下问题,检查上面配置的token和username对不对

[ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project cloud-component-security-starter: Error creating blob: Not Found (404) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException