当前位置: 代码网 > it编程>编程语言>Java > 持续集成Jenkins (四)Jenkins+git+maven项目构建、自动化部署

持续集成Jenkins (四)Jenkins+git+maven项目构建、自动化部署

2024年08月02日 Java 我要评论
1.2 Jenkins 配置插件 Git 在仪表盘选择Manage Jenkins>>Plugin Manager>>进入如下页面,可以选择可选的插件,安装完成后的插件在installed里面可以看到,我这里已经安装了在后面的构建过程可以看到,使用的就是 /usr/bin/git 去拉取远程 git 仓库。这里选择Git,然后如图配置:输入仓库地址,填入验证信息。其他按需更改。登录gitee账号选择仓库地址获取: 2.4 配置Credentials当前使用 username+password类型,并填写访
  1. git配置

1.1 前言:需要安装 git 客户端.
yum install git
1.2 jenkins 配置插件  git 

在仪表盘选择manage jenkins>>plugin manager>>进入如下页面,可以选择可选的插件,安装完成后的插件在installed里面可以看到,我这里已经安装了

1.3 jenkins 选择 git 路径

在后面的构建过程可以看到,使用的就是 /usr/bin/git 去拉取远程 git 仓库。

2. jenkins 配置git

2.1 新建任务
2.2 创建任务
2.3 配置source code management

这里选择git,然后如图配置:输入仓库地址,填入验证信息。其他按需更改。

登录gitee账号选择仓库地址获取: 

2.4 配置credentials

当前使用 username+password类型,并填写访问 git 的用户名、密码。

2.5 配置构建步骤

从 pom.xml 进行构建 maven 项目,并配置编译打包的命令。打包命令,不要加mvn,默认前缀会加;一般为空即可,如果有需要再添加内容;可添加maven命令。

clean package -dmaven.test.skip=true(清除以前的包,重新打包,并跳过测试) 

3. 开始构建

点击立即构建

查看console output查看信息,如下图所示:可以看见由git从仓库拉取代码,并在本地执行的一个过程。

构建成功:

4. 构建过程中的问题

4.1 maven依赖问题
/m2/repository/org/springframework/boot/spring-boot-starter-parent/2.2.3.release/spring-boot-starter-parent-2.2.3.release.pom.part.lock (没有那个文件或目录)
error: failed to parse poms
org.apache.maven.project.projectbuildingexception: some problems were encountered while processing the poms:
[warning] 'dependencies.dependency.(groupid:artifactid:type:classifier)' must be unique: org.projectlombok:lombok:jar -> duplicate declaration of version (?) @ line 71, column 21
[fatal] non-resolvable parent pom for com.testnode:redis_tools:1.0-snapshot: could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.3.release from/to alimaven (http://maven.aliyun.com/nexus/content/groups/public/): /m2/repository/org/springframework/boot/spring-boot-starter-parent/2.2.3.release/spring-boot-starter-parent-2.2.3.release.pom.part.lock (没有那个文件或目录) and 'parent.relativepath' points at no local pom @ line 12, column 13

    at org.apache.maven.project.defaultprojectbuilder.build(defaultprojectbuilder.java:397)
    at hudson.maven.mavenembedder.buildprojects(mavenembedder.java:370)
    at hudson.maven.mavenembedder.readprojects(mavenembedder.java:340)
    at hudson.maven.mavenmodulesetbuild$pomparser.invoke(mavenmodulesetbuild.java:1330)
    at hudson.maven.mavenmodulesetbuild$pomparser.invoke(mavenmodulesetbuild.java:1124)
    at hudson.filepath.act(filepath.java:1200)
    at hudson.filepath.act(filepath.java:1183)
    at hudson.maven.mavenmodulesetbuild$mavenmodulesetbuildexecution.parsepoms(mavenmodulesetbuild.java:985)
    at hudson.maven.mavenmodulesetbuild$mavenmodulesetbuildexecution.dorun(mavenmodulesetbuild.java:689)
    at hudson.model.abstractbuild$abstractbuildexecution.run(abstractbuild.java:522)
    at hudson.model.run.execute(run.java:1896)
    at hudson.maven.mavenmodulesetbuild.run(mavenmodulesetbuild.java:543)
    at hudson.model.resourcecontroller.execute(resourcecontroller.java:101)
    at hudson.model.executor.run(executor.java:442)
finished: failure

构建过程发生上面的报错,原因是maven拉取依赖失败。需要修改 setting.xml 中的配置,我使用的配置内容如下:

<?xml version="1.0" encoding="utf-8"?>

<settings xmlns="http://maven.apache.org/settings/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
          xsi:schemalocation="http://maven.apache.org/settings/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <localrepository>/usr/local/maven_repository</localrepository>

  <plugingroups>  
  </plugingroups>

  <proxies>
  </proxies>

  <servers>
  </servers>

  <mirrors>
    <mirror>
     <id>alimaven</id>
     <mirrorof>central</mirrorof>
     <name>aliyun maven</name>
     <url>https://maven.aliyun.com/repository/central</url>
    </mirror>

    <mirror> 
    <id>alimaven</id> 
    <name>aliyun maven</name> 
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
     <mirrorof>central</mirrorof> 
     </mirror> 
  </mirrors>
  <profiles>   
  </profiles>
</settings>
4.2 权限问题

由于我jenkins使用的不是 root 用户,而是 jenkins 用户,所以在build生成 war 后,没有权限将 war 生成到 maven 目录下,需要进行授权:

chown -r jenkins /usr/local/maven_repository

5. 自动化部署(未完……)

5.1 插件安装

(1)安装ssh插件

如果没有安装ssh插件的话,是不支持配置远程ssh的,这里可以去插件里面看下,是否安装了。 (2) 新增全局配置

环境配置, 管理jenkins-->configure system 找到 publish over ssh,点击新增,新增ssh server,输入hostname,username,password,点击test configuration,提示:success 代表ssh连通远端服务器,保存即可。

注:如果没有勾选 use password authentication, or use a different key这个选项,点击测试连接,会报错:jenkins.plugins.publish_over.bappublisherexception: failed to connect and initialize ssh connection

创建jenkins访问私钥:ssh

6. 单独编译子模块

jenkins构建maven多模块项目时,单独编译子模块,并且不触发构建其它模块。

配置:

  1. root pom指向父pom.xml

  1. goals and options指定构建模块的参数:mvn -pl service01-web -am clean package,单独构建jsoft-web项目以及它所依赖的其它项目。

  1. 编译多个子模块,使用多个 -pl 参数,为: -pl service01 -pl service02 。

通过上面的操作之后确实能单独构建了,但可能会同时触发jenkins上的其它模块的项目,可以通过屏蔽下游项目来限制

选中即可实现不自动触发下游的项目。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com