Jenkins Deploy2Tomcat
-
Install JDK
-
Install Jenkins, you can add
restartafter your jenkins url to restart jenkins.It consist of following default folder:
/usr/lib/jenkins /var/lib/jenkins /etc/sysconfig/jenkins /var/log/jenkins/jenkins.log -
Install Maven and SVN
-
Install Tomcat, make sure tomcat has host-manager and manager app, otherwise war cannot be deployed.
Also edit
tomcat-users.xml, add<user username="deployer" password="deployer" roles="manager-script" />, this user name/password is used to connect to tomcat for Jenkins, this user role should containmanager-script. -
Create a Jenkins project, enter a project name then choose Freestyle project, if project name is
HelloWorld, then Jenkins will create aHelloWorldfolder under/var/lib/jenkins/workspace/
-
In General tab, you can check
Discard old builds, strategy isLog rotation, then inputmax # of builds to keep, this is optional.
-
Config Source Code Management, you can check
GitorSubversion.For
Subversion, inputRepository URLandCredentialswhich is user name and password to check out source code from svn. Then choose therepository depth.If
Repository URLishttp://192.168.2.102:8090/svn/test/JenkinTest, then Jenkins will create a folder namedJenkinTestunder/var/lib/jenkins/workspace/<your project name>/. If it detects svn has changes, then it will check out source code from svn to this folder and build it.
-
Config Build Triggers, that is when to trigger the build automatically.
If you check
Poll SCMandScheduleisH/5 * * * *, it means Jenkins will check svn every 5 minutes, if code has change, then it will run the build process.
-
Add Build Step, if you use Maven to build, you can select
Invoke top-level Maven targets, then input maven goals to build.
-
Add post build action to deploy war to tomcat after install Deploy to container plugin.
deploy war/ear to a containershould be selected.value of
war/ear filesshould be<your svn project folder name>/target/*.waror*/target/*.warif this value is wrong, then Jenkins cannot run this depoly action, there is no error message in console output and/var/log/jenkins/jenkins.log.In this section,
Context Pathis your web app name in tomcat, you can leave it empty.Then select container and input the user name/password to connect it.
Input
Tomcat URL, likehttp://192.168.2.102:8181/
Problems
Jenkins installation successfully base on above steps in my pc. However it failed in another linux pc.
Jenkins is 2.108 in my pc, 2.107.1 in failed pc. I have no idea that why they are different because I installed it base on same instruction of jenkins doc.
both they are centos 7 and have same jdk version 1.8.
Maven is installed local in them.
Cannot run program “mvn” (in directory …):error=2, No such file or directory
Specify maven installation in Jenkins:
Jenkins -> Global Tool Configuration -> Maven, input maven name and maven home, uncheck Install automatically.
Then in Build section of project config, select maven version instead default.
JAVA_HOME is not defined correctly
Go to Manage Jenkins -> Configure System -> Global Properties -> Check Environment variables -> input JAVA_HOME and JAVA_HOME
PATH.
Cannot delete *.pom in jenkins workspace
or change permission manually.
Failed to undeploy
The Tomcat Manager responded “FAIL - Context [/Test] is defined in server.xml and may not be undeployed.
As the error message implies, there is context Test defined in server.xml which cause this problem.
Go to tomcat -> conf -> server.xml, find out like:
<Host name="localhost" appBase="/var/www/webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="/var/www/webapps/Test" path="/Test" reloadable="false" />
delete context definition.
Note: If you change webapp directory, like above, then the manager and host-manager should be moved, in this case, they should be in /var/www/webapps/