This post will show how to automate the deployment process of a Java Web Application (Student Enrollment Application developed using MYSQL DB with Hibernate ORM in a REST based Jersey2 Spring environment) using Jenkins Continuous Integration – to build the project, run the unit tests, upload the built artifacts to a Sonatype Snapshot repository, run the Cobertura Code Coverage reports and deploy the application to the Amazon EC2. The details of the actual application are explained in the earlier post given by the link Building Java Web Application Using Jersey REST With Spring.
1. Install Jenkins as a Windows Service
Navigate to jenkins-ci.org website using an Internet browser and download the Windows native package (the link is underlined for easy identification) as shown from the right side pane of the Download Jenkins tab.
Once the download is complete, uncompress the zip file and click on the jenkins-1.xxx.msi file. Proceed through the configuration steps to install the Jenkins as a Windows service.
2. Modify Default Jenkins Port
By default Jenkins runs on the port 8080. In order to avoid conflict with other applications, the default port can be modified by editing the jenkins.xml found under C:\Program Files (x86)\Jenkins location. As shown below, modify the httpPort to 8082.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Once the modification is saved in jenkins.xml file, restart the Jenkins service from the Windows Task Manager->Services and right clicking on the Jenkins service and choose Stop Service to stop the service as shown below.
Once the status of the service changes to stopped, restart the service by right clicking on the Jenkins service and choose Start Service to start the service again.
Navigate to localhost:8082 to verify if the Jenkins restart was successful as shown below – Jenkins Dashboard will be displayed. Note that it takes a while before the Jenkins service becomes available.
3. Install Plugins
On the Jenkins Dashboard, navigate to Manage Jenkins –> Manage Plugins as shown in the snapshot below.
Install the following plugins and restart Jenkins for the changes to take effect.
- GitHub Plugin (for integrating Github with Jenkins)
- Jenkins Cobertura Plugin (for Cobertura support)
- Deploy to Container Plugin (for deploying the WAR to the Tomcat Container on EC2 instance)
- Jenkins Artifactory Plugin (for deploying the built Maven artifacts to the Snapshot repository)
4. Configure System
On the Jenkins Dashboard, navigate to Manage Jenkins –> Configure System as shown in the snapshot below.
Navigate to the JDK section and click on “Add JDK” to add the JDK installation as shown in the snapshot below. Specify a JDK Name, choose the JDK Version to install and follow the on-screen instructions to save the Oracle Login credentials. Save the changes.
Next, proceed to the Git section and click on “Add Git” to add the Git installation as shown in the snapshot below. Specify Git Name, specify the path to Git executable and Save the changes.
Next, proceed to the Maven section and click on “Add Maven” to add the Maven installation as shown in the snapshot below. Specify Maven Name, choose the Maven Version to install and Save the changes.
Proceed to the Git plugin section and enter the values for Github Username and Email Address as credentials as shown below. Save the changes.
Proceed to the Artifactory section and click on “Add” to add the information about the artifactory servers. Specify the URL for the snapshot repository and provide the deployer credentials created from the Artifactory server website as shown below. Click on “Test Connection” to test if the connection parameters are good to save and Save the changes.
Next, proceed to the Email Notification section and enter the SMTP Server details as shown below. Click on the Advanced button to add the further details required and Save the changes. Click on “Test configuration by sending test e-mail”, enter the test e-mail recipient and click on “Test configuration” to see if the email is successfully sent.
5. Create a New Jenkins Job
From the Jenkins Dashboard, click on “New Job” to create a new job. Enter a name for the job and choose “Build a maven2/3 project” as option and click on OK as shown below.
From the New Job Configuration screen, proceed to the Source Code Management section and specify the Git Repository URL for the project as shown below. Save the changes.
Next, from the Build Triggers section, select the options desired as shown below and Save the changes.
Proceed to the Build section, enter the maven goals for building a snapshot as shown below and Save the changes.
Proceed to the Build Settings section. Select the option for Email Notification and enter the values for the email recipients as shown below. Save the changes.
Under the Post-build Actions, click on “Add post-build action” button and select “Deploy war/ear to a container”. In the Amazon EC2, a Tomcat Manager (manager as username) instance has to be configured with roles manager-gui and manager-script to allow the remote deployment of the WAR/EAR to the Tomcat Container. The configuration steps can be found in the link https://help.ubuntu.com/13.04/serverguide/tomcat.html under the section of “Tomcat administration webapps”
Once the Tomcat Manager webapp configuration is complete in the Amazon EC2 instance, enter the details necessary for the deployment as shown below. Save the changes.
Similarly, from the Post-build Actions, click on “Add post-build action” button and select “Publish Cobertura Coverage Report”. Enter the Cobertura XML Report Pattern as shown below and save the changes.
6. Configure settings.xml
In order to upload the built Maven artifacts to the artifactory server, configure the Jenkins settings.xml found in C:\Program Files (x86)\Jenkins\tools\hudson.tasks.Maven_MavenInstallation\Maven_3.1\conf folder with the same parameters as found in the default settings.xml (usually found under C:\Program Files\Apache Software Foundation\apache-maven-3.1.0\conf for a Windows machine) of the Maven installation on the system.
Typically, the server section needs to be configured in the settings.xml for Jenkins matching with the details of the Artifactory server.
1 2 3 4 5 6 7 8 9 10 11 12
7. Update pom.xml
The pom.xml file for the project needs to be configured with the following plugins under the build section for the deployment to snapshot repository and for running the Cobertura Coverage report.
Also add parent, scm and developer section to comply with the requirements put forth by the Artifactory server management as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
8. Build Now
Once the above configuration steps are complete, click on “Build Now” under the Jenkins –> Upload REST Snapshot Artifacts (or the respective Job name) to build the project based on the configuration.
The console output has the detailed logs of what steps were initiated by the configuration and the outcome of the entire build.
The timestamp of the WAR deployed to Amazon EC2 instance can be checked to see if the deployment is successful. In the same way, the snapshot repository can be checked to see if the upload of the artifacts is successful.
Thus the entire process of building the project along with unit tests whenever a SCM change is triggered or under another condition, running code coverage reports, uploading the artifacts built to the snapshot artifactory repository, deploying the WAR to the remote server container and triggering emails to the recipients can be automated with a click of a button through Jenkins.