1、安裝Maven並配置環境變量
下載maven(windows下載zip包,linux下載tar.gz包),然后配置環境變量
在項目中使用maven,可以從java中央倉庫中獲取到項目所依賴的jar包,才能完成項目的部署。
1.1 Linux配置環境變量內容
export MAVEN_HOME=/usr/local/apache-maven-3.5.3 -- 選擇自己正確的版本號export PATH=${PATH}:${MAVEN_HOME}/bin
配置完成后,輸入source profile使環境變量生效
在終端輸入mvn -v查看maven是否安裝成功
1.2 Windows配置環境變量內容
添加環境變量
MAVEN_HOME=C:\Program Files\Maven\apache-maven-3.5.3在path后追加環境變量:%MAVEN_HOME%\bin
輸出mvn -v檢查是否安裝成功
2、配置Jenkins中的jdk和maven環境變量
2.1 在Jenkins中點擊系統管理->全局工具配置分別配置jdk和maven
2.1.1 配置jdk
2.1.2 配置maven
-
安裝插件Maven Integration plugin
-
在全局配置工具中配置maven
3 安裝搭建svn
安裝文檔地址:
https://jingyan.baidu.com/article/870c6fc36a31d0b03fe4be3f.html
4 構建Jenkins集成項目
-
構建一個maven項目(需要先安裝Maven Integration plugin插件)
-
配置源碼管理(svn)
-
在Pre Steps下找到Build進行配置
5. 項目執行日志輸出及解釋
5.1 日志關鍵點展示
5.1.1 項目構建完成控制台輸出

5.1.2 生成的jar包的路徑

5.1.3 如果控制台報錯找不到對應的jar包的解決辦法
說明在maven的中央倉庫無法下載到這個jar包,那么就需要找開發要對應的jar包然后放到控制台報錯指示路徑即可
5.2 日志輸出完整展示
由用戶 admin 啟動
構建中 在工作空間 C:\Program Files (x86)\Jenkins\workspace\Maven_SVN_Jenkins02 中
Updating https://172_16_0_13/svn/MySVN at revision '2018-05-09T19:13:45.498 +0800' --quietUsing sole credentials admin/****** (SVN) in realm ‘<https://172_16_0_13:443> VisualSVN Server’ At revision 1No changes for https://172_16_0_13/svn/MySVN since the previous build No emails were triggered. Parsing POMs Established TCP socket on 52378[JunitTest02] $ "C:\Program Files\Java\jdk1.8.0_144/bin/java" -cp "C:\Program Files (x86)\Jenkins\plugins\maven-plugin\WEB-INF\lib\maven35-agent-1.12-alpha-1.jar;C:\Program Files\Maven\apache-maven-3.5.3\boot\plexus-classworlds-2.5.2.jar;C:\Program Files\Maven\apache-maven-3.5.3/conf/logging" jenkins.maven3.agent.Maven35Main "C:\Program Files\Maven\apache-maven-3.5.3" "C:\Program Files (x86)\Jenkins\war\WEB-INF\lib\remoting-3.20.jar" "C:\Program Files (x86)\Jenkins\plugins\maven-plugin\WEB-INF\lib\maven35-interceptor-1.12-alpha-1.jar" "C:\Program Files (x86)\Jenkins\plugins\maven-plugin\WEB-INF\lib\maven3-interceptor-commons-1.12-alpha-1.jar" 52378<===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f C:\Program Files (x86)\Jenkins\workspace\Maven_SVN_Jenkins02\JunitTest02\pom.xml install [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.maven:JunitTest02 >------------------------[INFO] Building JunitTest02 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]---------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ JunitTest02 ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ JunitTest02 ---[INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ JunitTest02 ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ JunitTest02 ---[INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ JunitTest02 ---[INFO] Surefire report directory: C:\Program Files (x86)\Jenkins\workspace\Maven_SVN_Jenkins02\JunitTest02\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running com.junit.exam01.DecoExamTest This is test A This is test B Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.094 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0[JENKINS] Recording test results [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ JunitTest02 ---[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/ [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ JunitTest02 ---[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/ [INFO] Installing C:\Program Files (x86)\Jenkins\workspace\Maven_SVN_Jenkins02\JunitTest02\target\JunitTest02-0.0.1-SNAPSHOT.jar to C:\Windows\system32\config\systemprofile\.m2\repository\com\maven\JunitTest02\0.0.1-SNAPSHOT\JunitTest02-0.0.1-SNAPSHOT.jar [INFO] Installing C:\Program Files (x86)\Jenkins\workspace\Maven_SVN_Jenkins02\JunitTest02\pom.xml to C:\Windows\system32\config\systemprofile\.m2\repository\com\maven\JunitTest02\0.0.1-SNAPSHOT\JunitTest02-0.0.1-SNAPSHOT.pom [WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.MavenArtifactArchiver$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/ [WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.MavenFingerprinter$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/ [INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------[INFO] Total time: 13.063 s [INFO] Finished at: 2018-05-09T19:14:07+08:00[INFO] ------------------------------------------------------------------------Waiting for Jenkins to finish collecting data [JENKINS] Archiving C:\Program Files (x86)\Jenkins\workspace\Maven_SVN_Jenkins02\JunitTest02\pom.xml to com.maven/JunitTest02/0.0.1-SNAPSHOT/JunitTest02-0.0.1-SNAPSHOT.pom [JENKINS] Archiving C:\Program Files (x86)\Jenkins\workspace\Maven_SVN_Jenkins02\JunitTest02\target\JunitTest02-0.0.1-SNAPSHOT.jar to com.maven/JunitTest02/0.0.1-SNAPSHOT/JunitTest02-0.0.1-SNAPSHOT.jar channel stopped Email was triggered for: Always Sending email for trigger: Always Sending email to: Gupan0923@126.com Finished: SUCCESS
6. 打war包配置及出錯指導
6.1 修改pom.xml文件
使用maven默認打包時打的jar包,如果要打war包需要修改pom.xml文件,修改方式如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.maven</groupId> <artifactId>JunitTest02</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 以下是添加的代碼--> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> <!-- 添加代碼結束--> <!-- 將這里修改為war打包的時候就可以打war包 --> <packaging>war</packaging></project>
6.2 jenkins打包報錯解決指導
6.2.1 錯誤展示
修改后,jenkins打包時,發現報錯如下:
6.2.2 出錯原因
maven的web項目默認的webroot是在src\main\webapp\WEB-INF。如果在此目錄下找不到web.xml就拋出以上的異常。
6.2.3 問題解決
在src\main\webapp\WEB-INF下建立web.xml文件