美文网首页
利用 Jenkins 把工程的 module 打包 librar

利用 Jenkins 把工程的 module 打包 librar

作者: java后端领域 | 来源:发表于2018-05-08 19:27 被阅读0次

利用 Jenkins 把工程的 module 打包 library jar 发布到 maven 仓库

当我们利用微服务的架构开发时,经常需要把自己服务的 api 提供一个 jar 依赖给调用方,这时需要把这个 jar 发布到 maven 仓库,让调用方引用。
手工处理方式:准备把 release 版本 deploy 到 maven 仓库时,需要先看看 maven 仓库最新的 release 版本,然后在本地+1后,进行 deploy 到 maven 仓库。缺点就是人工做版本控制,不高效而且版本连续性不可控,利用 Jenkins 来管理可以解决。
利用 Jenkins 进行发布后,会将代码中 SNAPHOST 版本升级发布最新的 release 版本,然后修改代码的版本为 release +1 SNAPSHOT 版本
注意:如果利用 Jenkins 来发布 library 的话,那么就要禁用开发者本地 deploy release 的 library

  1. 在工程的最顶层 pom.xml 加入以下配置

     <scm>
        <connection>scm:git:ssh://「git 地址」/「工程路径」.git</connection>
        <developerConnection>scm:git:ssh://「git 地址」/「工程路径」.git
        </developerConnection>
        <tag>HEAD</tag>
      </scm>
      <distributionManagement>
        <repository>
          <id>deploy</id>
          <name>em lib release repo</name>
          <url>http://「maven 私服地址,例如.xxx.maven.com」/artifactory/libs-release-local</url>
        </repository>
        <snapshotRepository>
          <id>deploy</id>
          <name>em lib snapshot repo</name>
          <url>http://「maven 私服地址,例如.xxx.maven.com」/artifactory/libs-snapshot-local</url>
        </snapshotRepository>
      </distributionManagement>
    
  1. Jenkins 构建一个任务:构建一个 maven 项目

    • 「General」 模块:自己配置

    • 「源码管理」模块:自己配置

    • 「构建触发器」模块:勾上这个选项即可:

      Build whenever a SNAPSHOT dependency is built

    • 「Build」模块:

      • Root POM: 填写 pom 路径,也就是第一步的 pom.xml,因为在工程顶层目录,所以直接 pom.xml

        pom.xml

      • Goals and options:

        org.apache.maven.plugins:maven-release-plugin:2.5.3:clean org.apache.maven.plugins:maven-release-plugin:2.5.3:prepare org.apache.maven.plugins:maven-release-plugin:2.5.3:perform -Dusername=deployer -Darguments="-DskipTests"

    • 「Post Steps」 模块:Run regardless of build result

  2. 常见错误

  • 问题1: deploy 时报错 401

Return code is: 401, ReasonPhrase: Unauthorized

解决方案:maven setting.xml 文件中的 server标签里面的id要跟pom.xml的 repository 标签里面的id要一致,例如:

maven setting.xml

<servers>
    <server>
      <id>releases</id>
      <username>deployment</username>
      <password>deployment</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>deployment</username>
      <password>deployment</password>
    </server>
 </servers>

pom.xml

<distributionManagement>
    <repository>
      <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
      <id>releases</id>
      <name>em lib release repo</name>
      <url>http://xxxx</url>
    </repository>
    <snapshotRepository>
      <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
      <id>snapshots</id>
      <name>em lib snapshot repo</name>
      <url>http://xxxx</url>
    </snapshotRepository>
  </distributionManagement>

相关文章

网友评论

      本文标题:利用 Jenkins 把工程的 module 打包 librar

      本文链接:https://www.haomeiwen.com/subject/gkwwrftx.html