Jenkins-03-build app案例_Build a Java app with Maven(文档翻译整理)

本文详细介绍如何在macOS、Linux及Windows环境下,利用Docker容器中的Jenkins和BlueOcean搭建持续集成环境,通过Maven自动构建Java应用程序。涵盖Jenkins安装、解锁、插件安装、用户创建、Pipeline项目创建及Jenkinsfile配置等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用Maven构建Java APP

1、机器配置要求
macOS, Linux, Windows机器:

  • 256GB RAM,推荐512MB以上
  • 10GB硬盘空间,用于Jenkins和Docker images和容器

安装软件:

2、Docker中运行Jenkins
macOS和Linux系统

  1. 打开终端窗口
  2. 运行jenkinsci/blue ocean image作为容器,命令如下:
docker run \
  --rm \
  -u root \
  -p 8080:8080 \
  -v jenkins-data:/var/jenkins_home \ 
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v "$HOME":/home \ 
  jenkinsci/blueocean
  1. 进入到[安装向导。

Windows系统

  1. 打开终端窗口
  2. 运行jenkinsci/blueocean image作为容器,命令如下:
docker run ^
  --rm ^
  -u root ^
  -p 8080:8080 ^
  -v jenkins-data:/var/jenkins_home ^
  -v /var/run/docker.sock:/var/run/docker.sock ^
  -v "%HOMEPATH%":/home ^
  jenkinsci/blueocean
  1. 进入到[安装向导。

4、进入Jenkins/Bule Ocean Docker容器
如果你想通过命令行中docker exec命令,进入Jenkins/Bule Ocean Docker容器,你可以添加一个像“–name jenkins-tutorials”的选项,用来命名容器为“jenkins-tutorial”
可以通过以下命令来实现:

docker exec -it jenkins-tutorials bash

5、安装向导
进入jenkins之前,还需要一些步骤需要做。

解锁Jenkins
首次进入jenkins instance,需要使用自动产生的密码来解锁Jenkins

  1. 浏览http://localhost:8080,等到出现以下界面
    在这里插入图片描述
  2. 从你的终端窗口中,复制自动产生的字母数字密码
    在这里插入图片描述
    3.将密码粘贴到Unlock Jenkins中的Administrator password中,点击继续

用plugins定制你的Jenkins
安装推荐的插件

创建第一个管理员用户

  • 在“Create First Admin User”页中,填入你的详细信息,点击"Save and Finish"
  • 显示“Jenkins is ready”时,点击“Start using Jenkins”

Stopping和restarting Jenkins
你可以终端命令窗口中,通过“Ctrl-C”,来停止Jenkins

重启Jenkins/Blue Ocean Docker container:

  • 运行与上边同样的命令“docker run …”
  • 浏览http://localhost:8080
  • 等待登录窗口出现,然后登录

6、从GitHub上fork和clone样本库(sample repository)

  • 登录GitHub账户

  • Fork simple-java-maven-app到你的本地GitHub账户

  • clone你fork的样本库到你的本地仓库

7、在Jenkins中创建Pipeline project

  • 登录Jenkins, 点击New Item

  • 填入Pipeline project名字(如:simple-java-maven-app)

  • 下拉选择Pipeline

  • 可以为你的Pipeline添加description,以及你所需要的环境变量参数

  • 进入到Pipeline部分,按图片的方式进行填写
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 点击保存

8、将初始化的Pipeline创建为Jenkinsfile

你现在已经准备好创建你的Pipeline,它将会在Jenkins中自动使用Maven来build Java app。你的Pipeline会本创建成Jenkinsfile, Jenkinsfile会被commit到你本地克隆的Git repository(simple-java-maven-app)

首先,创建初始化Pipeline来下载Maven Docker image,运行它作为一个Docker容器(这个就是用来build Java app的)

  • 在本地Git repository(simple-java-maven-app)目录下,创建Jenkinsfile文件(file类型)
  • 复制一下代码到Jenkinsfile
pipeline {
    agent {
        docker {
            image 'maven:3-alpine' 
            args '-v /root/.m2:/root/.m2' 
        }
    }
    stages {
        stage('Build') { 
            steps {
                sh 'mvn -B -DskipTests clean package' 
            }
        }
    }
}

This image parameter (of the agent section’s docker parameter) downloads the maven:3-apline Docker image (if it’s not already available on your machine) and runs this image as a separate container. This means that:
You’ll have separate Jenkins and Maven containers running locally in Docker.The Maven container becomes the agent that Jenkins uses to run your Pipeline project. However, this container is short-lived - its lifespan is only that of the duration of your Pipeline’s execution.

This args parameter creates a reciprocal mapping between the /root/.m2 (i.e. Maven repository) directories in the short-lived Maven Docker container and that of your Docker host’s filesystem. Explaining the details behind this is beyond the scope of this tutorial. However, the main reason for doing this is to ensure that the artifacts necessary to build your Java application (which Maven downloads while your Pipeline is being executed) are retained in the Maven repository beyond the lifespan of the Maven container. This prevents Maven from having to download the same artifacts during successive runs of your Jenkins Pipeline, which you’ll be conducting later on. Be aware that unlike the Docker data volume you created for jenkins-data above, the Docker host’s filesystem is effectively cleared out each time Docker is restarted. This means you’ll lose the downloaded Maven repository artifacts each time Docker restarts.

Defines a stage (directive) called Build that appears on the Jenkins UI.

This sh step (of the steps section) runs the Maven command to cleanly build your Java application (without running any tests).

  • 保存Jenkinsfile,并commit到你本地的Git repository。可以用以下命令:
git add .
git commit -m "Add initial Jenkins"
  • 回到Jenkins,打开Blue Ocean,进入Blue Ocean界面

  • 点击运行“This job has not been run”,然后快速点击短暂显示的右下角链接。如果你无法点击链接,点击Blue Ocean主界面的row进入。
    note: 第一步运行,你可能需要等待几分钟。当你克隆本地的simple-java-maven-app Git repository之后,Jenkins:

    • 将Agent中需要运行的项目排个队列
    • 下载Maven Docker image,在一个Docker容器中运行它
      在这里插入图片描述
    • 在Maven容器中,运行Build stage(在Jenkinsfile中定义)。这期间,Maven会下载很多必要的产品来build你的java App, 它最后将被存放在Jenkins本地Maven repository(在Docker主机的文件系统中)
      在这里插入图片描述
      如果Jenkins成功的build Java App,Blue Ocean将会变绿。
      在这里插入图片描述
  • 关闭界面,回到Blue Ocean主界面。

注:

  • 如果有误,欢迎指正
  • 相关Link
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值