Updated March 15, 2023
Introduction to tomcat deploy war
Tomcat is the most popular web server created by Apache foundation and has a lot of popularity among Java developers. This web server also acts as the servlet container where we can serve, upload and deploy the WAR files that stand for Web Archives files with an extension of .war. Tomcat web server comes along with tomcat web manager that helps you manage the deployment, undeploy, stop, start or run a particular deployed war file and its corresponding container. Most of the production environments that run web applications use the tomcat webserver to manage the deployment of their war files.
In this article, we will have a detailed look over how we can deploy a war file on the webserver of Apache tomcat.
How to file Tomcat deploy war
We can deploy a war file on the tomcat server in many ways. Let us have a look at some of them –
- Deploy WAR file using Maven dependency management tool –
To do so, you will need to change the configurations in the settings.xml file of Maven and mention the tomcat server in that file. The settings.xml configuration file can be found in either of the two locations –
- The directory where you installed Maven, namely nameOfMavenDir/conf/settins.xml
- The directory where the user is installed on the system can be nameofUserDir/.m2/settings.xml
After you find settings.xml, open it and search for the <server> tag as shown below –
<server>
<id> TomcatServer </id>
<username> payalEducba </username>
<password> payal@123 </password>
</server>
Create a sample web application with the maven tool to rest the deployment of the was file. You can create a new application of java web simply by entering the below command –
Mvn archetype: generate -Dgroup = com.educba -DartifactId = deployment of tomcat war -DarchetypeArtifactId = maven – archetype -webapp -DinteractiveMode = false
Executing the above command will create a new web application inside the directory name “deployment of tomcat war” that prints the message “Hello Educbies” if we deploy and run the application now. Before running it, we will make a small change in the pom.xml so that the maven deployment of the app will be allowed. This can be done by adding the below plugin –
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url> http://localhost:8080/manager/sample </url>
<server>TomcatServer</server>
<path>/EducbaSample</path>
</configuration>
</plugin>
The tomcat version 7 plugin works for versions 7 and 8 without affecting the functionality. The url tag inside the configuration helps us specify where to deploy our app. The server tag or element specifies the instance of the server that Maven and the path tag will recognize will help in specifying the path of context in our war deployment, which means we can access the application we are deploying by running the url – http://localhost:8080/manager/sample where we will find its output.
Now, the commands required for deploying a web application using Maven are as shown below –
mvn tomcat7:deploy
the command required for undeploying a web application using Maven is as shown below –
mvn tomcat7:undeploy
commands required for redeploying a web application after performing certain modifications using Maven are as shown below –
mvn tomcat7:redeploy
- Deploy web application war file using cargo plugin –
We can manipulate various applications and their corresponding containers using the versatile library of cargo in a standard way.
The first way is to use the maven plugin of the cargo library to deploy the war file. Here, we will use version 7. Let us create a simple web application with Maven by using the below command –
Mvn archetype: generate -Dgroup = com.educba -DartifactId = cargo-deploy -DarchetypeArtifactId = maven – archetype -web app -DinteractiveMode = false
Executing the above command will create a web application with a cargo deploy directory that prints the message “Hello Educbies” on execution in the browser even though there is no necessity for such a file for the tomcat maven plugin app but in the case of the cargo maven plugin. The app should have this file. The web.xml file will be very simple and basic as our app does not have any servlets. We can create a web.xml file in the WEB-INF folder of our project, which can have the below-mentioned code –
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javae"
xsi:schemaLocation="http://java.sun.com/xml/ns/java
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>educba-cargo-deploy</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
To recognize the cargo package inside the maven project, let us add the following plugin group inside the setting.xml file of maven dependencies –
<pluginGroups>
<pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>
We will now go for local deployment steps in cargo simply by editing our pom.xml file so that it helps in our requirements of deploying a war file.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.9.8</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<type>installed</type>
<home> path that represents the installation directory of tomcat (absolute not relative path)</home>
</container>
<configuration>
<type>existing</type>
<home> path that represents the installation directory of tomcat (absolute not relative path)</home>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
The latest version of cargo can be found on this site. We will have to mention the package of war else; our build will not be successful. We have added some elements to inform Maven that we will use the tomcat container that already exists in the system and is installed. The cargo’s maven plugin works for versions 2.a and 3.a of Maven. Its time to install the application by running the following command –
Mvn install
And the file can be deployed by using the command –
Mvn cargo:deploy
We can now run the url http://localhost:8080/educba-cargo-deploy
- The remote deployment using cargo libraries of Maven
We need to make a little change in the pom.xml file for this. If you don’t have Tomcat installed on your local machine and you want to access Tomcat on a remote machine, then you can do the remote deployment by making the following changes in pom.xml –
<configuration>
<container>
<containerId>tomcat8x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.remote.username>payal</cargo.remote.username>
<cargo.remote.password>payal@123</cargo.remote.password>
<cargo.tomcat.manager.url>http://localhost:8080/manager/educbaSample
</cargo.tomcat.manager.url>
</properties>
</configuration>
</configuration>
In case you are doing some changes in the existing deployment project on your Tomcat server, then before deploying the new version, you will need to undeploy the previous war file using the following command –
Mvn cargo:undeploy
And clean your project by using the command –
Mvn clean
And now install the maven project
Mvn install
For deploying use –
Mvn cargo:deploy
Conclusion
Tomcat web server is developed by apache and acts as a web container where we can deploy and manage our web archives that are war files.
Recommended Articles
This is a guide to Tomcat deploy to war. Here we discuss how we can deploy a war file on the webserver of Apache tomcat. You may also have a look at the following articles to learn more –