2.Jenkins & Maven

Jenkins & Maven

2.Jenkins & Maven_第1张图片

Apache Maven是Spring框架最常用的自动构建工具。

Jenkins Plugin

Jenkins 通过安装插件plugin来拓展功能。

安装

  • Manage Jenkins > Manage Plugins > Available Tab
  • 在filter框内输入想要安装的插件,点击install

验证安装

  • Manage Jenkins > Manage Plugins > Installed Tab
  • 在filter框内输入插件名称

Maven Plugin

如果之前没有安装过Maven Plugin:

  • Manage Jenkins > Manage Plugins > Available Tab
  • 在filter框内输入Maven,找到Maven Integration,点击install
  • 安装完成后进入Installed Tab查看

Git Plugin

Git Plugin是Jenkins初次启动时,推荐安装的插件之一。如果之前使用了默认安装,此时应该已经加载有Git Plugin

  • Manage Jenkins > Manage Plugins > Installed Tab
  • 在filter框内输入git,查看Git plugin & Git client
  • 如果没有Git plugin & Git client,根据上一节进行安装。

安装git命令行

sudo apt install -y git

对于国内的用户,如果安装失败,建议先修改Ubuntu的apt-get源为国内镜像源

​​sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
​​sudo vim /etc/apt/sources.list​​

添加阿里云镜像源

deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

重新安装Git

sudo apt-get update
sudo apt install -y git

使用Maven构建Java应用程序

New Item > Freestyle job (name: maven-job)

1. SCM (Source Code Management)
  • 选择Git
  • 输入
    • Repository URL:
      https://github.com/jenkins-docs/simple-java-maven-app.git
    • Credentials: None
    • Branch: */master
  • 保存,点击Build
  • 成功
Started by user ubuntu
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/maven-job
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning repository https://github.com/jenkins-docs/simple-java-maven-app.git
 > git init /var/lib/jenkins/workspace/maven-job # timeout=10
Fetching upstream changes from https://github.com/jenkins-docs/simple-java-maven-app.git
 > git --version # timeout=10
 > git --version # 'git version 2.34.1'
 > git fetch --tags --force --progress -- https://github.com/jenkins-docs/simple-java-maven-app.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/jenkins-docs/simple-java-maven-app.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision d429203aa304cba0a1beb7fda8219d916c1e2fce (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f d429203aa304cba0a1beb7fda8219d916c1e2fce # timeout=10
Commit message: "Merge pull request #264 from MarkEWaite/use-latest-compiler-plugin"
First time build. Skipping changelog.
Finished: SUCCESS
workspace

输出中出现了workspace地址Building in workspace /var/lib/jenkins/workspace/maven-job,指向了/var/lib/jenkins/workspace/maven-job

切换虚拟机路径,可以看到Jenkins将整个repo clone到了本地。本地内容与git repo中的内容一致。

打包JAR文件

Manage Jenkins > Global Tool Configuration > Maven

  • 点击
    • Add Maven
    • Name: jenkins-maven
    • Version: 3.8.6
  • 保存

Dashboard > maven-job > configure > Build Steps

  • 下拉选择 Invoke top-level Maven targets
    • Maven Version: jenkins-maven
    • Goals: -B -DskipTests clean package
      (查看Jenkinsfile,我们会在后面的章节详细解释Jenkinsfile)
  • 保存
  • Build

查看输出,Jenkins 依次执行了打包和构建。
查看Building jar: /var/lib/jenkins/workspace/maven-job/target/my-app-1.0-SNAPSHOT.jar
执行java -jar my-app-1.0-SNAPSHOT.jar

测试

Dashboard > maven-job > configure > Build Steps
增加test步骤

  • 下拉选择 Invoke top-level Maven targets
    • Maven Version: jenkins-maven
    • Goals: test
  • 保存
  • Build
    此处的test来自于https://github.com/jenkins-docs/simple-java-maven-app/blob/master/src/test/java/com/mycompany/app/AppTest.java
模拟部署

Dashboard > maven-job > configure > Build Steps
增加test步骤

  • 下拉选择 Execute shell
echo "************************"
echo "Deploy jar"
echo "************************"

java -jar /var/lib/jenkins/workspace/maven-job/target/my-app-1.0-SNAPSHOT.jar
  • 保存
  • Build
生成报告

Jenkins完成测试后,会自动生成报告

Surefire report directory: /var/lib/jenkins/workspace/maven-job/target/surefire-reports

Dashboard > maven-job > configure > Post-build Actions

  • Add post-build actions下拉选择Publish JUnit test result report
    • Test report XMLs:target/surefire-reports/*.xml
  • 保存
  • 连续Build多次,刷新页面
保存成功的jar

Dashboard > maven-job > configure > Post-build Actions

  • Add post-build actions下拉选择Archive the artifacts
    • Files to archive: target/*.jar
    • 勾选Archive artifacts only if build is successful
  • 保存
  • Build

你可能感兴趣的:(jenkins,maven,elasticsearch)