Deploy Maven based Spring WAR file to Tomcat 7


In this article, we will use Maven-Tomcat plugin to deploy a WAR file to Tomcat 7. We will use A sample Spring-based application PetClinic available at https://github.com/spring-projects/spring-petclinic

Download the application and extract the files.

Install Maven 3, Download Tomcat 7 from https://tomcat.apache.org/download-70.cgi

Now go to apache-tomcat-7.0.68conftomcat-users.xml

First, add an admin user with roles “manager-gui” and “manager-script”.

<role rolename=”manager-gui”/>
<role rolename=”manager-script”/><user username=”admin” password=”password” roles=”manager-gui,manager-script” />

53.1 tomcat-users

Now, let’s add Tomcat’s admin user that we created in the Maven setting file.

<servers>

<!– Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
–>
<server>
<id>tomcat-development-server</id>
<username>admin</username>
<password>password</password>
</server>

</servers>

Now the remaining thing is to declare a Maven-Tomcat Plugin in pom.xml. We are using sample Spring-based application PetClinic available at https://github.com/spring-projects/spring-petclinic. It already has that configuration but we need to change.

Locate Tomcat Plugin block in Pom.xml (https://github.com/spring-projects/spring-petclinic/blob/master/pom.xml). In my case, I changed port number to 8080.

Make sure that Server Name (tomcat-development-server) is same that we provided in settings.xml of Maven as Id.

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>tomcat-development-server</server>
<port>8080</port>
<warFile>targetpetclinic.war</warFile>
<path>/petclinic</path>
</configuration>

</plugin>

When we execute mvn tomcat7:deploy command, Maven deploy the WAR file to Tomcat 7 using Manager App “http://localhost:8080/manager/text” , on path “/petclinic“, using “tomcat-development-server” (in settings.xml of Maven) and username and password for basic authentication.

Before using mvn tomcat7:deploy, visit http://localhost:8080/

Click on Manager App; it will ask for Username and Password. Give username and password configured in the tomcat-users.xml.

Once you are authenticated properly, execute mvn tomcat7:deploy

53.2 Maven Tomcat Plugin Output

If you face proxy related issues while executing mvn command then visit- Solved: How to enable proxy setting in Maven to Download Jar files

Now Verify deployed application at http://localhost:8080/petclinic/

53.2 PetClinic Application
Done!

 

Leave a comment

Your email address will not be published. Required fields are marked *