[原]Jenkins(七)---jenkins項目編譯測試發布由maven構建的web項目


/**

* lihaibo

* 文章內容都是根據自己工作情況實踐得出。

* 版權聲明:本博客歡迎轉發,但請保留原作者信息!

http://www.cnblogs.com/horizonli/p/5332645.html

*/

一、使用maven編譯

(maven編譯  與 測試 test 和打包 package 和 部署 install 類似,不再贅述 )

在項目的配置頁面中有個maven配置:里面只有一個clean   就是清除以前的構建信息:

之前我使用了clean   package來編譯打包:結果如下圖:

 

--------------------------------------------------------

當執行完只有clean的時候,之前構建的信息就被刪除了:如下圖

------------------------------------------------------

附注解:

/opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target


/opt/jenkins_tomcat6     表示jenkins工作目錄

jobs/quick4j_demo        表示jenkins管理quick4j_demo項目的目錄

workspace                表示maven等插件的工作目錄

--------------------------------------------------------------------------------------------------

jenkins項目中使用maven擴展

這里的Goals  就是用maven 編譯要用的命令如下圖中所示:

jenkins使用maven編譯以invoke top-level Maven targets中的Goals為准:

 
---------------------------------------------------------------------------------------------

二、部署到遠程主機的tomcat中

     重點:兩台機器要ssh遠程root無密碼登錄。

     再回到Jenkins(二)---jenkins之Git+maven+jdk+tomcat中的全局圖:

server A --->   jenkins主機ip:192.168.100.119

server B --->  遠程部署主機IP:192.168.100.118

  

下面是編譯過程日志

紫色字體jenkins使用git從gitlab倉庫http://192.168.100.200/clq/quick4j.git拉取代碼

黑色字體:jenkins使用maven編譯測試發布web工程到本地 /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target/quick4j.war

藍色字體:jenkins使用ssh將本地打包好的war包傳送到遠程服務器的/opt/jenkins中

紅色字體:jenkins遠程執行遠程服務器上的腳本,檢查tomcat是否開啟,如狀態為開啟,則關閉后,刪除tomcat中原來的web項目包,再從/opt/jenkins中

              拷貝剛打包好的web項目war包到tomcat中,啟動tomcat。

Started by user admin
Building in workspace /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace           #jenkins本機的工作目錄
 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository  > /usr/bin/git config remote.origin.url http://192.168.100.200/clq/quick4j.git # timeout=10
Fetching upstream changes from http://192.168.100.200/clq/quick4j.git
 > /usr/bin/git --version # timeout=10
using .gitcredentials to set credentials
 > /usr/bin/git config --local credential.username clq # timeout=10
 > /usr/bin/git config --local credential.helper store --file=/opt/tomcat6/apache-tomcat-6.0.45/temp/git4880643529884290174.credentials # timeout=10
 > /usr/bin/git -c core.askpass=true fetch --tags --progress http://192.168.100.200/clq/quick4j.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git config --local --remove-section credential # timeout=10
 > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > /usr/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision b123b099e8e72d7e467ab780ae28726a1c866797 (refs/remotes/origin/master)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f b123b099e8e72d7e467ab780ae28726a1c866797
 > /usr/bin/git rev-list b123b099e8e72d7e467ab780ae28726a1c866797 # timeout=10
[workspace] $ /opt/apache-maven-3.3.9/bin/mvn -Dmaven.repo.local=/opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/.repository clean
[INFO] Scanning for projects...
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] Building quick4j App 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ quick4j ---
[INFO] Deleting /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.299 s
[INFO] Finished at: 2016-03-29T13:18:06+08:00
[INFO] Final Memory: 7M/238M
[INFO] ------------------------------------------------------------------------
[workspace] $ /bin/sh -xe /opt/tomcat6/apache-tomcat-6.0.45/temp/hudson1223364716445353332.sh
Parsing POMs
[workspace] $ /opt/JDK/bin/java -cp /opt/jenkins_tomcat6/plugins/maven-plugin/WEB-INF/lib/maven31-agent-1.5.jar:/opt/apache-maven-3.3.9
/boot/plexus-classworlds-2.5.2.jar:/opt/apache-maven-3.3.9/conf/logging jenkins.maven3.agent.Maven31Main
/opt/apache-maven-3.3.9 /opt/tomcat6/apache-tomcat-6.0.45/webapps/jenkins/WEB-INF/lib/remoting-2.51.jar
/opt/jenkins_tomcat6/plugins/maven-plugin/WEB-INF/lib/maven31-interceptor-1.5.jar
/opt/jenkins_tomcat6/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.5.jar 36966 <===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/pom.xml
-Dmaven.repo.local=/opt/jenkins_tomcat6/maven-repositories/1 -s /opt/apache-maven-3.3.9/conf/settings.xml
-gs /opt/apache-maven-3.3.9/conf/settings.xml clean package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building quick4j App 1.0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ quick4j --- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ quick4j --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 9 resources [INFO] Copying 3 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ quick4j --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 62 source files to /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ quick4j --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 3 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ quick4j --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 5 source files to /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ quick4j --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ quick4j --- [INFO] Packaging webapp [INFO] Assembling webapp [quick4j] in [/opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target/quick4j] [INFO] Processing war project [INFO] Copying webapp resources [/opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/src/main/webapp] [INFO] Webapp assembled in [383 msecs] [INFO] Building war: /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target/quick4j.war #發布構建好的web工程到本機工作目錄下 [INFO] WEB-INF/web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.611 s [INFO] Finished at: 2016-03-29T13:18:15+08:00 [INFO] Final Memory: 28M/
438M [INFO] ------------------------------------------------------------------------ [JENKINS] Archiving /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/pom.xml to com.eliteams/quick4j/1.0.0/quick4j-1.0.0.pom [JENKINS] Archiving /opt/jenkins_tomcat6/jobs/quick4j_demo/workspace/target/quick4j.war to com.eliteams/quick4j/1.0.0/quick4j-1.0.0.war SSH: Connecting from host [Local-Machine-1] SSH: Connecting with configuration [192.168.100.118-ssh] ... SSH: Creating session: username [root], hostname [192.168.100.118], port [22] SSH: Connecting session ... channel stopped SSH: Connected SSH: Opening SFTP channel ... SSH: SFTP channel open SSH: Connecting SFTP channel ... SSH: Connected SSH: cd [/opt/jenkins] SSH: OK SSH: cd [/opt/jenkins] SSH: OK SSH: put [quick4j.war] SSH: OK SSH: Opening exec channel ... SSH: EXEC: channel open SSH: EXEC: STDOUT/STDERR from command [sh /opt/auto_deploy.sh] ... SSH: EXEC: connected ********************************** [2016-03-29]13:18:23 ************************************* updating server environment start updating server environment end check tomcat status... tomcat is running....port is 9090 shutdown tomcat..... >>>>>>>shutdown tomcat begin<<<<<<<< Using CATALINA_BASE: /opt/tomcat6/apache-tomcat-6.0.45 Using CATALINA_HOME: /opt/tomcat6/apache-tomcat-6.0.45 Using CATALINA_TMPDIR: /opt/tomcat6/apache-tomcat-6.0.45/temp Using JRE_HOME: /app/java/jdk1.8.0_11/jre Using CLASSPATH: /opt/tomcat6/apache-tomcat-6.0.45/bin/bootstrap.jar >>>>>>>shutdown tomcat end <<<<<<<< -------------- begin transfer war package to tomcat webapps ------------------- Find /opt/jenkins exist war package quick4j.war deleteing old package quick4j.war in /opt/tomcat6/apache-tomcat-6.0.45/webapps/ start transfer quick4j.war to /opt/tomcat6/apache-tomcat-6.0.45/webapps/ -------------- transfer war package to tomcat webapps end ------------------- >>>>>>> rebooting tomcat begin <<<<<<<< Using CATALINA_BASE: /opt/tomcat6/apache-tomcat-6.0.45 Using CATALINA_HOME: /opt/tomcat6/apache-tomcat-6.0.45 Using CATALINA_TMPDIR: /opt/tomcat6/apache-tomcat-6.0.45/temp Using JRE_HOME: /app/java/jdk1.8.0_11/jre Using CLASSPATH: /opt/tomcat6/apache-tomcat-6.0.45/bin/bootstrap.jar >>>>>>> rebooting tomcat end <<<<<<<< the log you can read in canalina.out ************************ deploy war package into container Successlly ********************************** SSH: EXEC: completed after 11,406 ms SSH: Disconnecting configuration [192.168.100.118-ssh] ... SSH: Transferred 1 file(s) Finished: SUCCESS

--------------------------------------------------------------------------------------------------------------------------------------------------------

三、瀏覽器驗證是否正確發布:

jenkins主機ip:192.168.100.119

遠程部署主機IP:192.168.100.118

發布成功

 

 --------------------目錄電梯---------------------

[原]Jenkins(一)---我理解的jenkins是這樣的

[原]Jenkins(二)---jenkins之Git+maven+jdk+tomcat

[原]Jenkins(三)---Jenkins初始配置和插件配置

[原]Jenkins(四)---Jenkins添加密鑰對

[原]jenkins(五)---jenkins添加項目

[原]jenkins(六)---jenkins遠程部署腳本

[原]Jenkins(七)---jenkins項目編譯測試發布由maven構建的web項目

[原]Jenkins(八)---jenkins構建項目報錯時發送錯誤報告郵件

[原]Jenkins(九)---jenkins分別發布多個項目到多個遠程主機

[原]Jenkins(十)---jenkins注冊管理員admin並賦所有權限給admin

[原]Jenkins(十一)---jenkins使用管理員admin創建用戶和分配權限

[原]Jenkins(十二)---jenkins管理員用戶無法登陸解決辦法Access Denied

[原]Jenkins(十三)---jenkins用戶權限管理

[原]Jenkins(十四)---jenkins示例:admin管理所有項目,新建用戶只能看部分項目

[原]Jenkins(十五)---jenkins插件之deploy


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM