虚拟化容器,大数据,DBA,中间件,监控。

Jenkins 常用的项目构建工具集成 01 Maven

21 12月
作者:admin|分类:系统运维

常用的项目构建工具集成


image.png

前端的打包工具,一般使用的都是npm

Java项目构建工具


 初始化一个springboot项目  https://start.spring.io/

image.pngmaven去打包的时候,下面都会有一个xml文件,叫pom.xml这个文件,这个文件就定义了项目的依赖项,通过各种结构化的数据来定义。

这样就生成了maven测试项目的一个过程 

Maven


官网:http://maven.apache.org/download.cgi Maven是一个项目的构建依赖管理工具。通常项目的根目录会存在一个pom.xml文件(该文件用于定义项目的依赖包信息和构建配置)

经常要做的就是如何在流水线里面进行打包

Maven安装配置


maven也是依赖JDK的,所以要先将java_home配置好

[root@jenkins-master ~]# java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.282-b08, mixed mode)

[root@jenkins-master ~]# tail -n6 /etc/profile
export JAVA_HOME=/usr/local/jdk8
export PATH=$JAVA_HOME/bin:$PATH
export GROOVY_HOME=/usr/local/groovy-3.0.7
export PATH=$GROOVY_HOME/bin:$PATH
export M2_HOME=/usr/local/apache-maven-3.8.1
export PATH=$M2_HOME/bin:$PATH
### 下载

wget  https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
tar zxf apache-maven-3.8.1-bin.tar.gz -C /usr/local/
cd /usr/local/apache-maven-3.8.1/
pwd  /usr/local/apache-maven-3.8.1


### 配置环境变量
vi /etc/profile
export M2_HOME=/usr/local/apache-maven-3.8.1
export PATH=$M2_HOME/bin:$PATH
source /etc/profile


### 验证

mvn -v
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /usr/local/apache-maven-3.8.1
Java version: 1.8.0_282, vendor: AdoptOpenJDK, runtime: /usr/local/jdk8u282-b08/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "4.18.0-80.el8.x86_64", arch: "amd64", family: "unix"

gitlab里面创建一个项目 

本地有代码了就可以传上去

来到这个目录下面将代码上传

[root@jenkins-master ~]# cd devops-maven-service-master
[root@jenkins-master devops-maven-service-master]# ls
Jenkinsfile  mvnw  mvnw.cmd  pom.xml  src

[root@jenkins-master devops-maven-service-master]# git init
Initialized empty Git repository in /root/devops-maven-service-master/.git/
[root@jenkins-master devops-maven-service-master]# git remote add origin http://139.198.170.122:81/root/devops-maven-service.git
[root@jenkins-master devops-maven-service-master]# git add .
[root@jenkins-master devops-maven-service-master]# git commit -m "Initial commit"
[master (root-commit) 0b12b52] Initial commit
 11 files changed, 816 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 .mvn/wrapper/MavenWrapperDownloader.java
 create mode 100644 .mvn/wrapper/maven-wrapper.jar
 create mode 100644 .mvn/wrapper/maven-wrapper.properties
 create mode 100644 Jenkinsfile
 create mode 100755 mvnw
 create mode 100644 mvnw.cmd
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/example/demo/DemoApplication.java
 create mode 100644 src/main/resources/application.properties
 create mode 100644 src/test/java/com/example/demo/DemoApplicationTests.java
[root@jenkins-master devops-maven-service-master]# git push -u origin master
Username for 'http://139.198.170.122:81': root
Password for 'http://root@139.198.170.122:81': 
Counting objects: 27, done.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (27/27), 53.44 KiB | 0 bytes/s, done.
Total 27 (delta 0), reused 0 (delta 0)
To http://139.198.170.122:81/root/devops-maven-service.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

resource文件夹里面存放着配置文件

Maven 常用命令


  • mvn clean  清理构建目录
  • mvn clean  package 打包 (这里会有单元测试的过程)
  • mvn clean  install 打包部署
  • mvn clean  test 单元测试
  • mvn clean package -f ../pom.xml     -f指定pom位置\
  • mvn clean package -DskipTests / -Dmaven.test.skip=true     跳过单测
  • mvn deploy 发布包到制品库

打包的时候会下载很多依赖,阿里云maven源https://developer.aliyun.com/article/597934 

下面是修改插件源

[root@jenkins-master ~]# vim /usr/local/apache-maven-3.8.1/conf/settings.xml

<mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>  
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>      
    </mirror>

maven下载的包都会缓存在目录当中 

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->


[root@jenkins-master repository]# ls
aopalliance  backport-util-concurrent  classworlds  commons-codec  commons-logging  jakarta  junit  org
asm          ch                        com          commons-io     io               javax    net

 这里多了一个target目录,最关键的就是这个应用包demo-0.0.1-SNAPSHOT.jar

[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:23 min
[INFO] Finished at: 2021-06-01T15:43:54+08:00
[INFO] ------------------------------------------------------------------------
[root@jenkins-master devops-maven-service-master]# ls target/
classes                  demo-0.0.1-SNAPSHOT.jar.original  generated-test-sources  maven-status      test-classes
demo-0.0.1-SNAPSHOT.jar  generated-sources                 maven-archiver          surefire-reports

这个包就可以直接启动

[root@jenkins-master target]# java -jar demo-0.0.1-SNAPSHOT.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.4)

2021-06-01 15:49:32.004  INFO 26266 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_282 on jenkins-master with PID 26266 (/root/devops-maven-service-master/target/demo-0.0.1-SNAPSHOT.jar started by root in /root/devops-maven-service-master/target)
2021-06-01 15:49:32.014  INFO 26266 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2021-06-01 15:49:34.612  INFO 26266 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-06-01 15:49:34.651  INFO 26266 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-06-01 15:49:34.651  INFO 26266 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-06-01 15:49:34.773  INFO 26266 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-06-01 15:49:34.774  INFO 26266 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2615 ms
2021-06-01 15:49:35.960  INFO 26266 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-06-01 15:49:36.592  INFO 26266 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-01 15:49:36.658  INFO 26266 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 5.735 seconds (JVM running for 6.659)
2021-06-01 15:49:36.794  INFO 26266 --- [nio-8080-exec-1] o.apache.tomcat.util.http.parser.Cookie  : A cookie header was received [:sess=O8wndXnEhbZ6q3QOLzGcahd__76DVKul; kubesphere:sess.sig=Ne-55wTGJElmSDxUeGWe9KTXYlI; known_sign_in=Q3gvQ3FQL1l3ZEsxY0gxMXhLUEZmRWFOVmxJVVFKeE9TaDZvSkcyMWJiYkVnZ2JSQjN3UllCSTRjbTZSeG5jTmx3cC81ZXNtQnhiRTlrRmdwTXY3R3p4dFpqRVZjSTNlRUdNenFqMmw4UzRGNW5JMFRMWStYam1aL0dta05WWC8tLTNkeEdmNDNHdk5FYXl6TWlVc2kyS3c9PQ%3D%3D--04f1ffe0e68036b23c7888b13160cb5abaf925d4; _gitlab_session=25974412b939b115dbef9f436ff07db0] that contained an invalid cookie. That cookie will be ignored.
 Note: further occurrences of this error will be logged at DEBUG level.
2021-06-01 15:49:36.817  INFO 26266 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-01 15:49:36.818  INFO 26266 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-06-01 15:49:36.819  INFO 26266 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

Pipeline流水线构建


def buildTools = ["maven": "/usr/local/apache-maven-3.8.1"]

pipeline{
   
   agent {label "build"}

   stages{
     
     stage("GetCode"){
        steps{
            script{
                println("下载分支代码----->${env.branchName}")
                checkout([$class: 'GitSCM', branches: [[name: "${env.branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: '823b86fe-2c1b-466e-b0c3-66ae6991449d', url: "${gitHttpURL}"]]])
            }
        }
     }

     stage("Build"){
         steps{
             script{
                sh "${buildTools["maven"]}/bin/mvn clean package"
             }
         }
     }

   }

}

 单元测试


maven这里有时候会找不到,使用绝对路径,如下使用 

def buildTools = ["maven": "/usr/local/apache-maven-3.8.1"]

pipeline{
   
   agent {label "build"}

   stages{
     
     stage("GetCode"){
        steps{
            script{
                println("下载分支代码----->${env.branchName}")
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '823b86fe-2c1b-466e-b0c3-66ae6991449d', url: "${gitHttpURL}"]]])
            }
        }
     }

     stage("Build"){
         steps{
             script{
                sh "${buildTools["maven"]}/bin/mvn clean package"
             }
         }
     }

     stage("UnitTest"){
         steps{
             script{
               sh "${buildTools["maven"]}/bin/mvn test"
             }
         }
     }

   }

}

单元测试有个插件junit的报告,下面是测试报告的目录

[root@jenkins-master devops-maven-service-master]# ls target/surefire-reports/
com.example.demo.DemoApplicationTests.txt  TEST-com.example.demo.DemoApplicationTests.xml

下面的路径是相对于workspace来说的 

如果单元测试失败就收集不到了,所以呢这里需要添加一个post

def buildTools = ["maven": "/usr/local/apache-maven-3.8.1"]

pipeline{
   
   agent {label "build"}

   stages{
     
     stage("GetCode"){
        steps{
            script{
                println("下载分支代码----->${env.branchName}")
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '823b86fe-2c1b-466e-b0c3-66ae6991449d', url: "${gitHttpURL}"]]])
            }
        }
     }

     stage("Build"){
         steps{
             script{
                sh "${buildTools["maven"]}/bin/mvn clean package"
             }
         }
     }

     stage("UnitTest"){
         steps{
             script{
               sh "${buildTools["maven"]}/bin/mvn test"
             }
         }
         post{
             success{
                script{
                     junit 'target/surefire-reports/*.xml'
                }
             }
         }
     }
  }

}

在企业里面会用nexus去搭建一个私服,那么就可以使用本地私服了,就不需要去拉取外网的包了。最后项目打包时候需要的参数找开发人员去要就行了。 

浏览560 评论0
返回
目录
返回
首页
微服务需要考虑的问题 Jenkins pipeline 09 parallel 阶段并行