Jenkins中maven的作用--构建项目
Posted 芹溪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins中maven的作用--构建项目相关的知识,希望对你有一定的参考价值。
本文主要根据Jenkins上的日志来继续说明构建项目的过程,上文我们已经讲到构建一个测试环境或单独终端的过程,详情可以了解上篇文章
一、背景介绍
首先看下SVN代码的仓库的结构:
代码仓库里有一个文件夹,可以随便取名,文件夹中可以放置很多单独的项目,每个项目下有branch,trunk,demand三个文件。在branch下面可以有pc端,微信端,
APP端,后台等终端,从下面的结构中,可以看出每个终端的代码中有资源文件夹src(类的定义,配置文件,说明文档,jar包等)和pom文件。
二、构建trunk项目
当发布正式环境时需先先构建trunk,然后发布正式项目,因此此时需要注意,如果有项目功能没有写完,不能直接构建trunk,此时构建trunk会将仓库下所有项目修改或更新的部分全部更新到Jenkins工作目录中。
1、Jenkins中的SVN地址:
构建正式环境:需要构建trunk,将代码仓库中所有变动的代码和Jenkins工作目录中的代码对比,然后更新Jenkins工作目录的代码
3.4版本trunk地址 https://101.21.101.111:443/svn/代码仓库/branches/qianrongyiV3.4(项目下有web,APP,WeChat等终端) 3.4版本正式项目地址: https://101.21.101.111:443/svn/代码仓库/branches/qianrongyiV3.4/qian-web(pom文件)(单独web终端)
构建预发布环境:不需要构建trunk,只需要单独构建项目即可,将单独项目变动部分更新。 3.4版本预发布环境: https://101.21.101.111:443/svn/代码仓库/branches/qianrongyiV3.4/qian-app
2、构建3.4版本trunk的过程:
1 Started by user 3798233333@qq.com 2 Building in workspace /bat/jenkins/workspace/3.4-pro-trunk ##在Jenkins工作目录中更新3.4-pro-trunk,如果没有这个文件夹,则创建。 3 Updating https://101.21.101.111:443/svn/代码仓库/branches/qianrongyiV3.4 at revision \'2018-02-08T17:49:41.934 +0800\' 4 下面内容表示更新Jenkins工作目录中项目的内容(根据SVN仓库中的文件与Jenkins工作目录中的文件对比),U表示更新;A表示添加;D表示删除。 5 U(update)qianee-common/src/main/java/com/test/qianee/constants/Constants.java(全路径为:/bat/jenkins/workspace/3.4-pro-trunk/qianee-common/src/main/java/com/test/qianee/constants/Constants.java;下面路径相同) 6 A(add) qianee-service/src/main/java/com/test/qianee/service/StatisticPlatformIncomeServiceImpl.java 7 U qianee-service/src/main/java/com/test/qianee/service/CapitalmanagerService.java 8 A qianee-service/src/main/java/com/test/qianee/service/ContentAdvertisementsLinksImple.java 9 D(delete) qianee-service/src/main/java/com/test/qianee/service/impl/ContentAdvertisementsLinksImple.java 10 D qianee-service/src/main/java/com/test/qianee/service/impl/StatisticPlatformIncomeServiceImpl.java 11 12 At revision 23920 13 14 Parsing POMs ##开始分析pom文件 15 Established TCP socket on 59933 16 [3.4-pro-trunk] $ java -cp /bat/jenkins/plugins/maven-plugin/WEB-INF/lib/maven32-agent-1.7.jar:/bat/maven/boot/plexus-classworlds-2.5.2.jar:/bat/maven/conf/logging jenkins.maven3.agent.Maven32Main /bat/maven /bat/webapps/jenkins/WEB-INF/lib/remoting-2.59.jar /bat/jenkins/plugins/maven-plugin/WEB-INF/lib/maven32-interceptor-1.7.jar /bat/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.7.jar 58833 ##指明了执行这个class文件所需要的所有类的包路径-即系统类加载器的路径 17 <===[JENKINS REMOTING CAPACITY]===>channel started 18 Executing Maven: -B -f /bat/jenkins/workspace/3.4-pro-trunk/pom.xml clean install 19 [INFO] Scanning for projects... 20 [INFO] ------------------------------------------------------------------------ 21 [INFO] Reactor Build Order: ##反应堆(Reactor)是一个包含了所有需要构建模块的抽象概念;模块会被包含到反应堆;反应堆中所有模块的构建顺序 22 [INFO] 23 [INFO] qianee 24 [INFO] qianee-wechat-service 25 [INFO] qianee-talent-wechat-service 26 27 项目二: 28 29 [INFO] ------------------------------------------------------------------------ 30 [INFO] Building qianee-wechat-service 3.4-SNAPSHOT 31 [INFO] ------------------------------------------------------------------------ 32 [INFO] ##加载maven清理插件,清除Jenkins工作目录下项目的target已编译文件 33 [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ qianee-wechat-service --- 34 [INFO] Deleting /bat/jenkins/workspace/3.4-pro-trunk/qianee-wechat-service/target 35 [INFO] ##为了使项目结构更为清晰,Maven区别对待Java代码文件和资源文件,maven-compiler-plugin用来编译Java代码,maven-resources-plugin则用来处理资源文件。 36 37 默认的主资源文件目录是src/main/resources,很多用户会需要添加额外的资源文件目录,这个时候就可以通过配置maven-resources-plugin来实现。 38 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ qianee-wechat-service --- 39 [INFO] Using \'UTF-8\' encoding to copy filtered resources. 40 [INFO] Copying 0 resource 41 [INFO] 42 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ qianee-wechat-service --- 43 [INFO] Changes detected - recompiling the module! 44 [INFO] Compiling 29 source files to /bat/jenkins/workspace/3.4-pro-trunk/qianee-wechat-service/target/classes 45 [INFO] 46 [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ qianee-wechat-service --- 47 [INFO] Using \'UTF-8\' encoding to copy filtered resources. 48 [INFO] Copying 0 resource 49 [INFO] 50 [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ qianee-wechat-service --- 51 [INFO] Nothing to compile - all classes are up to date 52 [INFO] 53 [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ qianee-wechat-service --- 54 [JENKINS] Recording test results 55 [INFO] 56 [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ qianee-wechat-service --- 57 [INFO] Building jar: /bat/jenkins/workspace/3.4-pro-trunk/qianee-wechat-service/target/qianee-wechat-service.jar 58 [INFO] 59 [INFO] --- maven-install-plugin:2.4:install (default-install) @ qianee-wechat-service --- 60 [INFO] Installing /bat/jenkins/workspace/3.4-pro-trunk/qianee-wechat-service/target/qianee-wechat-service.jar to /root/.m2/repository/com/test/qianee/qianee-wechat-service/3.4-SNAPSHOT/qianee-wechat-service-3.4-SNAPSHOT.jar 61 [INFO] Installing /bat/jenkins/workspace/3.4-pro-trunk/qianee-wechat-service/pom.xml to /root/.m2/repository/com/test/qianee/qianee-wechat-service/3.4-SNAPSHOT/qianee-wechat-service-3.4-SNAPSHOT.pom 62 63 64 ------------------------------------------------------- 65 T E S T S 66 ------------------------------------------------------- 67 Running com.test.qianee.app.WelcomeApiControllerTest ##运行app登录控制类测试 68 2018-02-08 17-52-12 [main] [org.springframework.test.context.web.WebTestContextBootstrapper] [INFO] - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@36ecb8fa, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6f644141, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1ef84b89, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3f271f41] 69 2018-02-08 17-52-12 [main] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] [INFO] - Loading XML bean definitions from class path resource [TestContext.xml] 70 ........(省略数行)...... 71 ==============loaded properties files================= 72 file [/bat/jenkins/workspace/3.4-pro-trunk/qianee-app/target/classes/application-sms.properties] 73 .......(省略数行)......... 74 Results : ##几个类的测试结果 75 76 Tests in error: 77 test(com.test.qianee.app.UserApiControllerTest): Result{status=2, msg=\'该手机号尚未注册,请重新输入或注册\', bat=null, msgMap=null} 78 。。。。。。(省略数行)。。。。。 79 80 Tests run: 15, Failures: 0, Errors: 5, Skipped: 0 81 82 [ERROR] There are test failures. 83 84 Please refer to /bat/jenkins/workspace/3.4-pro-trunk/qianee-app/target/surefire-reports for the individual test results. 85 [JENKINS] Recording test results 86 [INFO] ##APP项目打包 87 [INFO] --- maven-war-plugin:2.2:war (default-war) @ qianee-app --- 88 [INFO] Packaging webapp 89 [INFO] Assembling webapp [qianee-app] in [/bat/jenkins/workspace/3.4-pro-trunk/qianee-app/target/qianee-app] 90 [INFO] Processing war project 91 [INFO] Copying webapp resources [/bat/jenkins/workspace/3.4-pro-trunk/qianee-app/src/main/webapp] 92 [INFO] Webapp assembled in [505 msecs] 93 [INFO] Building war: /bat/jenkins/workspace/3.4-pro-trunk/qianee-app/target/qianee-app.war 94 [INFO] WEB-INF/web.xml already added, skipping 95 [INFO] 96 [INFO] --- maven-install-plugin:2.4:install (default-install) @ qianee-app --- 97 [INFO] Installing /bat/jenkins/workspace/3.4-pro-trunk/qianee-app/target/qianee-app.war to /root/.m2/repository/com/test/qianee/qianee-app/3.4-SNAPSHOT/qianee-app-3.4-SNAPSHOT.war 98 [INFO] Installing /bat/jenkins/workspace/3.4-pro-trunk/qianee-app/pom.xml to /root/.m2/repository/com/test/qianee/qianee-app/3.4-SNAPSHOT/qianee-app-3.4-SNAPSHOT.pom 99 [INFO] 100 。。。。。。(省略数行)。。。。。 、 101 [INFO] Reactor(反应器) Summary: 102 [INFO] 103 [INFO] qianee-web ......................................... SUCCESS [ 16.531 s] 104 [INFO] qianee-wechat-service .............................. SUCCESS [ 2.053 s] 105 [INFO] qianee-talent-wechat-service ....................... SUCCESS [ 1.889 s] 106 107 [INFO] ------------------------------------------------------------------------ 108 [INFO] BUILD SUCCESS 109 [INFO] ------------------------------------------------------------------------ 110 [INFO] Total time: 03:30 min 111 [INFO] Finished at: 2018-02-08T17:53:26+08:00 112 [INFO] Final Memory: 76M/557M 113 [INFO] ---------------------------项目存档--------------------------------------------- 114 项目一存档:(将Jenkins工作目录中的war包和pom.xml文件存档备份到用户家目录下) 115 [JENKINS] Archiving(存档,归档) /bat/jenkins/workspace/3.4-pro-trunk/qianee-web/pom.xml to com.test.qianee/qianee-web/3.4-SNAPSHOT/qianee-web-3.4-SNAPSHOT.pom 116 [JENKINS] Archiving /bat/jenkins/workspace/3.4-pro-trunk/qianee-web/target/qianee-web.war to com.test.qianee/qianee-web/3.4-SNAPSHOT/qianee-web-3.4-SNAPSHOT.war 117 项目二存档: 118 [JENKINS] Archiving /bat/jenkins/workspace/3.4-pro-trunk/qianee-talent-wechat-service/pom.xml to com.test.qianee/qianee-talent-wechat-service/3.4-SNAPSHOT/qianee-talent-wechat-service-3.4-SNAPSHOT.pom 119 [JENKINS] Archiving /bat/jenkins/workspace/3.4-pro-trunk/qianee-talent-wechat-service/target/qianee-talent-wechat-service.jar to com.test.qianee/qianee-talent-wechat-service/3.4-SNAPSHOT/qianee-talent-wechat-service-3.4-SNAPSHOT.jar 120 项目三存档: 121 [JENKINS] Archiving /bat/jenkins/workspace/3.4-pro-trunk/qianee-wechat-service/pom.xml to com.test.qianee/qianee-wechat-service/3.4-SNAPSHOT/qianee-wechat-service-3.4-SNAPSHOT.pom 122 [JENKINS] Archiving /bat/jenkins/workspace/3.4-pro-trunk/qianee-wechat-service/target/qianee-wechat-service.jar to com.test.qianee/qianee-wechat-service/3.4-SNAPSHOT/qianee-wechat-service-3.4-SNAPSHOT.jar 123 。。。。。。(省略数行)。。。。。 124 channel stopped 125 Finished: UNSTABLE
3、Jenkins上trunk配置:
部分1、Maven project name 栏设置
主要填写Maven project name(工程名)、Descript(工程描述信息)
部分2、Source Code Management 栏设置
输入源码的获取地址,由于我的插件中演示的是svn,那么如下输入:
Repository depth: Jenkins在执行checkout、update代码时,所取出的代码的范围。默认我们选择infinity就可以。
Check-out Strategy:svn第一次更新代码和以后同步代码的步骤,和策略。默认选项一般就可以。
部分3、Build Triggers 栏设置
默认选项为:
Build whenever a SNAPSHOT dependency is buil 意思就是svn更新触发构建
这里建议选择:
Poll SCM 相当于新建一个计划任务,定时检查版本库,发现有新的提交就触发构建。其配置的意思和linux的crontab差不多,如10分钟构建一次
部分4、Build 栏设置
如果对mvn构建参数有需求,或者需要设置pom.xml的文件位置,可以在这里设置。
默认: pom.xml的文件就在工程目录下。
部分5、完成
点 save 完成对一个一个工程的设置
4、构建trunk的结果:
以上是关于Jenkins中maven的作用--构建项目的主要内容,如果未能解决你的问题,请参考以下文章
Jenkins——Jenkins构建Maven项目(三种风格)+Jenkins项目构建细节
Jenkins——Jenkins 构建Maven项目(三种风格的项目构建自由风格Maven风格Pipeline流水线风格)