In this guide, we are going to use Docker to configure Jenkins and Tomcat to achieve Continuous Integration/Continuous Deployment.
- Jenkins and Tomcat Setup. Copy the downloaded Jenkins.war file (downloaded from the previous section) and copy it to the webapps folder of the tomcat directory. Now open the command prompt. From the command prompt, browse to the directory where the tomcat is located. Then, go to the bin directory of this tomcat folder and run the startup.bat file.
- Jenkins is an open source continuous integration tool written in Java. It is a server-based system running in a servlet container such as Apache Tomcat. I have confused how it work on Tomcat, and may i know the Difference between these two?
We will install and set up Tomcat and Jenkins using Docker, and thereafter we are going to create a Jenkins Job to deploy your Java application over the Tomcat server.
This guide is assuming that you already have basic knowledge of Docker and Docker is up and running on your host machine. So let's begin with the guide.
Jenkins Set up
Download the latest Jenkins.war from Towards the right top click on the link latest and greatest to download the latest war file. Deploy the Jenkins.war file in the local web server of our machine. Tomcat; Start the server and open the browser and hit the URL http://localhost:8080/jenkins. To work Download Jenkins from below this link. It is a web archive (war) file which we will be using to run Jenkins on our apache tomcat server. Once downloaded move Jenkins.war file to webapps directory of apache tomcat and restart tomcat server. Tomcat is a JEE servlet container engine and Jenkins is an Java web application that can be deployed onto tomcat. The confusing part may be that Jenkins also packages its own webserver jars, enabling easy deployment.
You can spin up a Docker container using the Jenkins image.
Apache Tomcat Windows Update
We are running Jenkins on port 8080
. You can see the running container here:
To extract the Jenkins password from Jenkins's container, you can run below-mentioned command:
Copy the admin password from your terminal and open http://your-ip-addr:8080
in your preferred browser. Next, run the initial setup wizard and select Recommended Plugins. Jenkins will install all the necessary plugins.
If everything goes well, you will land on the Jenkins' dashboard area, which looks something like this:
Tomcat Set up
Now, it's time to spin the Tomcat container. For this, we are going to use CentOS, which will enable us to set up a customized Tomcat server. You can also use tomcat image as per your choice, however for this guide we are going to stick to the plan. So let's start by pulling the latest Centos image from Docker Hub:
To run the container from the pulled image in detached mode, use the command below:
![Jenkins Jenkins](https://static.javatpoint.com/tutorial/jenkins/images/java-and-tomcat-setup-for-jenkins8.png)
As Jenkins has already occupied port 8080
on the machine, here we have opened port 8888
for Tomcat in spite the default port being 8080
.
To install Tomcat in your Centos container, follow the steps:
Chorus of the hebrew slaves composer. Go to /opt directory, in your tomcat_container
:
Open https://tomcat.apache.org/download-80.cgi on your browser. Find 'Core' and download the Tomcat binary. At the time of writing this article, the current version is 8.5.50.
Extract the tar file:
![Jenkins Jenkins](https://developers.perfectomobile.com/download/attachments/21434935/g.png?version=1&modificationDate=1505290679823&api=v2)
Here you are going to find two shell script files named startup.sh
and shutdown.sh
, assign 'execute' permission to both. Both these files allow us to start and stop the Tomcat server, within our container:
![Jenkins Jenkins](https://profile-images.xing.com/images/d3c9865178a0fea541be6ac3ea7565de-1/raman-sharma.1024x1024.jpg)
To run the Tomcat server, Java is required. In order to do so, run following command to satisfy the requirement. Here we are going to install OpenJDK 7 using Yum:
It's time to run our Tomcat server; use startup.sh
file to do so:
In your terminal, you can see the message Tomcat started
. Now, go to your browser; Tomcat should be accessible at http://your-ip-addr:8888
.
In case you want to access the Manage App, you will have to do some additional settings, as by default, the Manager App section can only be accessed from the local system (i.e., 127.0.0.1
).
To overcome this, you will need to make some changes to context.xml
file. To do so follow the below-mentioned steps:
Now, find the context.xml
file:
Edit the file that resides in webapps directory i.e. /opt/apache-tomcat-8.5.50/webapps/host-manager/META-INF/context.xml
, find the Value class name org.apache.catalina.valves.RemoteAddrValve
and comment it:
Let's do the same thing on the other file as well, i.e., /opt/apache-tomcat-8.5.50/webapps/manager/META-INF/context.xml
.
To access the default username and password through the Manager App section, go to conf/tomcat-users.xml
and make the following changes:
Now, as part of your pipeline, you will have to add a user and define roles on your Tomcat server. To copy the file from the remote machine (in our case, the Jenkins server), you will need to define the role Manager Script, copy the lines and add it to the end of the file just before the closing tag :
Since you have made configuration changes in the Tomcat server, you will need to restart the server for the changes to take effect.
You can now access the Tomcat Web Application Manager dashboard; username and password are tomcat
and s3cret
, respectively.
Let us move to the next part of the tutorial.
Configure Jenkins
Jenkins Apache Tomcat Tutorial
It's time to set up Maven and an additional plugin so that Jenkins can connect to Tomcat container over SSH. For this, we are going to install Maven Integration
and Publish over SSH
plugin in Jenkins.
Login to your Jenkins environment and go to Dashboard > Manage Jenkins > Manage Plugins > Available, as shown below:
For Maven Integration:
For Publish over SSH:
Post-installation, you need to configure SSH server details so that Jenkins can connect the Tomcat container. In Jenkins, go to Dashboard > Manage Jenkins > Configure System and find SSH Servers. Enter details as shown below:
Test your connection before saving the details. For the sake of this article, we have used the password authentication method, however, it is not recommended in a production environment. We recommend you use a SSH key instead.
Deploying WAR file on Tomcat Server using Jenkins
As you are through with the configuration part, now you can proceed to create a Jenkins Job.
For this simple job, you would need to use Freestyle Project. Create a new item, as shown below:
Apache Jenkins
SCM
Go to the Source Code Management (also known as SCM), and select Git. Add your Github or GitLab URL in Repository URL field:
Poll SCM
In the Build Triggers section, select Poll SCM. It periodically polls the VCS to check if any changes were made. During the poll, if any changes were detected in repo since the last build, a new build is initiated.
Let's say you want to poll SCM every 30 minutes, then in the text field, add H/30 * * * *
(i.e., every 30 minutes, every hour, every day, every month, and every day of the week).
Post-Build Step
Go to Post-build Actions section and from the drop-down menu, select Send build artifacts over SSH, as shown below:
In the Source files field, mention the file name that is going to be uploaded to the server. In case you are not sure about the file name, you can use the pattern as mentioned above **/*.war
.
Other than this, make sure that you mention the Remote Directory; in our case, it is /root/tomcat
. Whereas, for Exec command, as you are using Docker for your Tomcat server, use the below-mentioned commands:
The above will copy hello-world.war
file to your dockerized tomcat container in /opt/apache-tomcat-8.5.50/webapps
. Winnie the pooh drawings. Plus, you are making sure that you restart Tomcat to see the changes.
![Download Download](https://images.idgesg.net/images/article/2020/01/jenkins-fig04-100826350-large.jpg)
As Jenkins has already occupied port 8080
on the machine, here we have opened port 8888
for Tomcat in spite the default port being 8080
.
To install Tomcat in your Centos container, follow the steps:
Chorus of the hebrew slaves composer. Go to /opt directory, in your tomcat_container
:
Open https://tomcat.apache.org/download-80.cgi on your browser. Find 'Core' and download the Tomcat binary. At the time of writing this article, the current version is 8.5.50.
Extract the tar file:
Here you are going to find two shell script files named startup.sh
and shutdown.sh
, assign 'execute' permission to both. Both these files allow us to start and stop the Tomcat server, within our container:
To run the Tomcat server, Java is required. In order to do so, run following command to satisfy the requirement. Here we are going to install OpenJDK 7 using Yum:
It's time to run our Tomcat server; use startup.sh
file to do so:
In your terminal, you can see the message Tomcat started
. Now, go to your browser; Tomcat should be accessible at http://your-ip-addr:8888
.
In case you want to access the Manage App, you will have to do some additional settings, as by default, the Manager App section can only be accessed from the local system (i.e., 127.0.0.1
).
To overcome this, you will need to make some changes to context.xml
file. To do so follow the below-mentioned steps:
Now, find the context.xml
file:
Edit the file that resides in webapps directory i.e. /opt/apache-tomcat-8.5.50/webapps/host-manager/META-INF/context.xml
, find the Value class name org.apache.catalina.valves.RemoteAddrValve
and comment it:
Let's do the same thing on the other file as well, i.e., /opt/apache-tomcat-8.5.50/webapps/manager/META-INF/context.xml
.
To access the default username and password through the Manager App section, go to conf/tomcat-users.xml
and make the following changes:
Now, as part of your pipeline, you will have to add a user and define roles on your Tomcat server. To copy the file from the remote machine (in our case, the Jenkins server), you will need to define the role Manager Script, copy the lines and add it to the end of the file just before the closing tag :
Since you have made configuration changes in the Tomcat server, you will need to restart the server for the changes to take effect.
You can now access the Tomcat Web Application Manager dashboard; username and password are tomcat
and s3cret
, respectively.
Let us move to the next part of the tutorial.
Configure Jenkins
Jenkins Apache Tomcat Tutorial
It's time to set up Maven and an additional plugin so that Jenkins can connect to Tomcat container over SSH. For this, we are going to install Maven Integration
and Publish over SSH
plugin in Jenkins.
Login to your Jenkins environment and go to Dashboard > Manage Jenkins > Manage Plugins > Available, as shown below:
For Maven Integration:
For Publish over SSH:
Post-installation, you need to configure SSH server details so that Jenkins can connect the Tomcat container. In Jenkins, go to Dashboard > Manage Jenkins > Configure System and find SSH Servers. Enter details as shown below:
Test your connection before saving the details. For the sake of this article, we have used the password authentication method, however, it is not recommended in a production environment. We recommend you use a SSH key instead.
Deploying WAR file on Tomcat Server using Jenkins
As you are through with the configuration part, now you can proceed to create a Jenkins Job.
For this simple job, you would need to use Freestyle Project. Create a new item, as shown below:
Apache Jenkins
SCM
Go to the Source Code Management (also known as SCM), and select Git. Add your Github or GitLab URL in Repository URL field:
Poll SCM
In the Build Triggers section, select Poll SCM. It periodically polls the VCS to check if any changes were made. During the poll, if any changes were detected in repo since the last build, a new build is initiated.
Let's say you want to poll SCM every 30 minutes, then in the text field, add H/30 * * * *
(i.e., every 30 minutes, every hour, every day, every month, and every day of the week).
Post-Build Step
Go to Post-build Actions section and from the drop-down menu, select Send build artifacts over SSH, as shown below:
In the Source files field, mention the file name that is going to be uploaded to the server. In case you are not sure about the file name, you can use the pattern as mentioned above **/*.war
.
Other than this, make sure that you mention the Remote Directory; in our case, it is /root/tomcat
. Whereas, for Exec command, as you are using Docker for your Tomcat server, use the below-mentioned commands:
The above will copy hello-world.war
file to your dockerized tomcat container in /opt/apache-tomcat-8.5.50/webapps
. Winnie the pooh drawings. Plus, you are making sure that you restart Tomcat to see the changes.
Build Now
Cuda clion. Initiate the build process from the Project's Jenkins Page, and go to the console log output to see if the build was a success or if there is any error/exception.
Your build finished with a SUCCESS message. Now go to your web browser and open http://your-ip-addr:8888/hello-world
. You will see your application index page content. In our case, output is:
Voila!
You have successfully deployed a sample application over Tomcat Server running on Docker with the help of Jenkins Jobs.