Application Deployment has always been on of the most tedious tasks in project development life cycle. This involves steps like checking out the code from the Code Repository, building it using Maven/Ant and then deploying it to the remote servers. The process is more time/effort taking in case we have multiple servers like Staging,UAT,production.
Jenkins is an open source tool that helps us to automate and centralize the process of deployment.
In this post i am going to explain how to deploy WAR to Apache Tomcat using SVN as code repository and Maven as build WAR tool.
Step 1 : Configure Jenkins
Jenkins comes bundled as WAR file so you need to deploy it to an Application Server. I used Apache Tomcat version 7.0.32. Below are the steps.
1.1. Start the tomcat
1.2. Deploy the jenkins.war Download location for jenkins.war
Note : Some server restrict the size of war file to be uploaded and you get the common error while deploying it from the Tomcat UI Manager. Below is how i resolved it.
a. Open the file $CATELINA_HOME\webapps\manager\WEB-INF web.xml
b. Look out for the below entry, Size 61316110 worked just fine for me
<!– 50MB max –>
c. Restart Tomcat
1.3. Go to the URL http://locahost:8080/jenkins ( Replace local host with the IP of your server )
1.4. Add Deploy to container Plugin this can be done using the Plugin Manager feature of Jenkins. This plugin will be used for deployment of the war that we will build using Jenkins.
1.5. Provide Maven Location to Jenkins – Jenkins interacts with Maven to build code that requires maven. Follow the below steps.
a. Download Maven Link for download
b. Place it on the server and copy its path
c Go to URL http://localhost:8080/jenkins/configure ( Replace localhost with the IP of your server )
d. Provide name ( Any random name ) and location of your maven.
Step 2 : Configure Tomcat for build deployment
We need to configure the server where we will be deploying our build ( This server can be same or different then the one where Jenkins is running ). This involves creating a user on tomcat and assigning him with manager-script role.
Just add the below lines under the node <tomcat-users></tomcat-users> to tomcat-users.xml file and restart the server. This file is located in the conf folder of tomcat.
<user username=”harpreet” password=”harpreet” roles=”standard,manager,admin,manager-gui,manager-script” />
Step 3 – Create a Job on Jenkins
3.1 Click On “New Job”
3.2 Enter the name for the Job
3.3 Select Build a maven2/3 project
3.4 Look out for section Source Code Management, select Subversion and provide the SVN url of project you will be prompted for your SVN credentials.
3.6 : Add a post build action that will deployed the war file to remote tomcat server. Follow the below steps
a . Click on button “Add post build action”
b. Select the option ” Deploy war/ear to a container”
C . Enter war file name and path
Important : For every job Jenkins create a folder named as workspace in the jobs folder, this path needs to relative to workspace. Upon successful build war is copied in this folder, and deploy plugin picks it from here. If the war is not in this folder, or you failed to provide the correct path.War will not be deployed to the server.
d Enter the server detail and credential of the user that we created on the tomcat in above steps
3.7 : Save the job and hit “Build Now”, Check the logs by clicking the ball icons on the left side of the screen.
There will be log traces like you have for Maven, when you build it using eclipse or command line args.
And few jenkins log signifying the deployment like below :
Deploying /home/gagan/.jenkins/jobs/TalentHuntV2/workspace/target/TalentHunt-1.0.war to container Tomcat 7.x Remote
3.8 : Verifying you did it correct.In case you did everything right, you can test the deployment of your project on the servers.
Step 4 ( Optional ) – Enable Auto Build
You can also configure Jenkins to build your code automatically when anyone commits the code.
4.1 Look out for section “Build Trigger” section.
4.2 Select the event that you want for build trigger. I chose that Jenkins should poll SVN for an updates. If update in code is found it will trigger the build.
For this we just need to select option”Poll SCM” and enter appropriate time, i used pattern */5 * * * * .
Important : For configuration of project that use artifacts from other project please do read my next blog. Building Artifacts Using Jenkins
Jenkins can help you automate a number of other processes/tasks. WAR/EAR development is the one you can start, once you are into Jenkins am sure you will be tempted to explore it further.
You can post any issues that you get in Jenkins configuration on this blog, We at Paxcel Technology will be happy to help you.
Enterprise Java Group