Docker整合Jenkins打包maven项目

一. 全局工具配置

MAVEN和JDK配置

  1. MAVEN我选的默认设置和全局配置

  2. JDK的JAVA_HOME用的是/opt/java/openjdk
    注:此jenkins运行在Docker内部,如果设置容器外部的Java环境变量在构建任务的时候会报找不到JAVA_HOME路径错误

  3. Maven安装Name选择下拉框和Linux maven版本号一直的名字。

Docker整合Jenkins打包maven项目_第1张图片
Docker整合Jenkins打包maven项目_第2张图片

二. 系统配置相关变量( Configure System [Jenkins])

  1. 配置全局属性,配置环境变量的键值对。
    注: Java的键值对可以进入容器输入echo $JAVA_HOME查看


    CLASS_PATH

    .: J A V A H O M E / l i b / d t . j a r : JAVA_HOME/lib/dt.jar: JAVAHOME/lib/dt.jar:JAVA_HOME/lib/tools.jar

    JAVA_HOME

    /opt/java/openjdk

    M2_HOME

    /usr/local/maven/apache-maven-3.3.9

    PATH

    P A T H : PATH: PATH:M2_HOME/bin
    Docker整合Jenkins打包maven项目_第3张图片
    Docker整合Jenkins打包maven项目_第4张图片

三. 工作空间,主目录相关问题

  1. 如果控制台输出报不能在workspace工作空间执行mvn,或找不到目录。可以在jenkins_home下的config.xml修改工作空间位置workspaceDir标签下。
    在这里插入图片描述

四. Job工作任务相关配置

  1. Build配置
    Root POM:
    pom.xml
    Goals and options:
    clean package -U -DskipTests=true

  2. Post Steps
    调用顶层Maven目标
    Maven版本选择刚刚在全局和系统配置的版本
    目标
    clean
    package
    Docker整合Jenkins打包maven项目_第5张图片

  3. 执行(立即构建)
    最后输出
    Started by user root
    Running as SYSTEM
    Building in workspace /usr/local/jenkins/workspace/maven-test
    The recommended git tool is: NONE
    using credential e7fab。。。
    .
    .
    .
    .
    .
    [INFO] — maven-jar-plugin:3.2.0:jar (default-jar) @ flink-server —
    [INFO] Building jar: /usr/local/jenkins/workspace/maven-test/modules/flink-server/target/flink-server-0.0.1-SNAPSHOT.jar
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building commons 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] — maven-clean-plugin:3.1.0:clean (default-clean) @ commons —
    [INFO] Deleting /usr/local/jenkins/workspace/maven-test/commons/target
    [INFO]
    [INFO] — maven-resources-plugin:3.2.0:resources (default-resources) @ commons —
    [INFO] Using ‘UTF-8’ encoding to copy filtered resources.
    [INFO] Using ‘UTF-8’ encoding to copy filtered properties files.
    [INFO] skip non existing resourceDirectory /usr/local/jenkins/workspace/maven-test/commons/src/main/resources
    [INFO] skip non existing resourceDirectory /usr/local/jenkins/workspace/maven-test/commons/src/main/resources
    [INFO]
    [INFO] — maven-compiler-plugin:3.8.1:compile (default-compile) @ commons —
    [INFO] No sources to compile
    [INFO]
    [INFO] — maven-resources-plugin:3.2.0:testResources (default-testResources) @ commons —
    [INFO] Not copying test resources
    [INFO]
    [INFO] — maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ commons —
    [INFO] Not compiling test sources
    [INFO]
    [INFO] — maven-surefire-plugin:2.22.2:test (default-test) @ commons —
    [INFO] Tests are skipped.
    [INFO]
    [INFO] — maven-jar-plugin:3.2.0:jar (default-jar) @ commons —
    [WARNING] JAR will be empty - no content was marked for inclusion!
    [INFO] Building jar: /usr/local/jenkins/workspace/maven-test/commons/target/commons-0.0.1-SNAPSHOT.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary:
    [INFO]
    [INFO] big-Data … SUCCESS [ 0.439 s]
    [INFO] modules … SUCCESS [ 0.002 s]
    [INFO] flink-server … SUCCESS [ 5.727 s]
    [INFO] commons … SUCCESS [ 0.396 s]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 9.554 s
    [INFO] Finished at: 2021-12-24T10:22:53+00:00
    [INFO] Final Memory: 30M/143M
    [INFO] ------------------------------------------------------------------------
    Finished: SUCCESS

可以在工作目录查看到各jar包
Building in workspace /usr/local/jenkins/workspace/maven-test
工作目录被我修改过之前有报错找不到目录
mvn没有对应pom.xml
执行命令
find / -name maven-test
/var/lib/docker/overlay2/e723d6386a89ca2a18c74de68ccbb86ec91fe8252b6fdb93c41e9e7470c39ecd/diff/usr/local/jenkins/workspace/maven-test
/var/lib/docker/overlay2/e723d6386a89ca2a18c74de68ccbb86ec91fe8252b6fdb93c41e9e7470c39ecd/merged/usr/local/jenkins/workspace/maven-test
/usr/local/jenkins/jobs/maven-test

[root@iZuf6bvvjcih5uryul8zleZ workspace]# cd /usr/local/jenkins/jobs/maven-test
[root@maven-test]#
[root@maven-test]# ls
builds config.xml modules nextBuildNumber

[root@maven-test]# cd modules/
[root@modules]# ls
‘com.bigDataKaTeX parse error: Double superscript at position 12: big-Data' '̲com.bigDatacommons’ ‘com.bigDataKaTeX parse error: Double superscript at position 16: flink-server' '̲com.bigDatamodules’
[root@modules]# cd com.bigData$flink-server/
[[email protected]KaTeX parse error: Expected 'EOF', got '#' at position 14: flink-server]#̲ ks -bash: ks:…flink-server]# ls
builds config.xml nextBuildNumber
[[email protected]$flink-server]# cd builds/
[root@builds]# ls
13 14 15 permalinks
[root@builds]# cd 15/
[root@15]# ls
data build.xml log
[root@15]# cd data/
[root@data ]# ls
com.bigData
[root@data]# cd com.bigData/
[[email protected]]# ls
flink-server
[[email protected]]# cd flink-server/
[root@flink-server]# ls
0.0.1-SNAPSHOT
[root@flink-server]# cd 0.0.1-SNAPSHOT/
[[email protected]]# ls
flink-server-0.0.1-SNAPSHOT.jar flink-server-0.0.1-SNAPSHOT.pom
[[email protected]]#

Docker整合Jenkins打包maven项目_第6张图片
在这里插入图片描述
在这里插入图片描述
Docker整合Jenkins打包maven项目_第7张图片

最后可以看到各模块生成的Jar包了。但是包结构还是很奇怪。
控制台输出的工作目录其实是Docker容器的位置,容器外实际是在另外一个位置,通过find / -name可以看出

find / -name maven-test
/var/lib/docker/overlay2/e723d6386a89ca2a18c74de68ccbb86ec91fe8252b6fdb93c41e9e7470c39ecd/diff/usr/local/jenkins/workspace/maven-test
/var/lib/docker/overlay2/e723d6386a89ca2a18c74de68ccbb86ec91fe8252b6fdb93c41e9e7470c39ecd/merged/usr/local/jenkins/workspace/maven-test
/usr/local/jenkins/jobs/maven-test

应该还需配置下工作空间的挂载目录,可以尝试运行中的容器编辑配置json配置,后续会继续调整,jenkins和maven pom配置的还是有些问题。

你可能感兴趣的:(Jenkins,jenkins,maven,docker,java)