Deploy Java Web Maven Project on Remote Tomcat Server

This post shows the way to configure and deploy a JavaWeb Maven project on a remote Tomcat server.

Tomcat Server Configuration

On Tomcat server, tomcat-admin-webapps is required for deploying project remotely (tomcat8-admin-webapps etc.). Once the tomcat-admin-webapps is installed, there should exist host-manager and manager in the tomcat/webapps folder.
To get access to the manager website, we need to set up users with specific roles in the tomcat/conf/tomcat-users.xml. Here is an example of setting up a user for deploying projects:

In tomcat-users.xml

1
2
3
4
5
<tomcat-users ...>
<role rolename="manager-gui"/> <!-- For manage online on /manager/html -->
<role rolename="manager-script"/> <!-- For remote deployment -->
<user username="yourUsername" password="yourPassword" roles="manager-gui,manager-script"/>
</tomcat-users>

Java Web Maven Project Configuration

We need to configure the Tomcat user in pom.xml:

pom.xml

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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yourGroupId</groupId>
<artifactId>yourArtifactId</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Your App Name</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>yourFinalName</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<urlEncoding>UTF-8</urlEncoding>
<path>/</path>
<finalName>yourFinalName</finalName>
<server>tomcat</server>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://example.com:8080/manager/text</url>
<server>exampleServer</server>
<path>/projectPath</path>
</configuration>
</plugin>
</plugins>
</build>
</project>

The plugin tomcat-maven-plugin is used for deploying Java Web project on remote tomcat server. In the configuration label, as server label is used, the server should be configured in the setting.xml (default as ~/.m2/setting.xml) as follows:

setting.xml

1
2
3
4
5
6
7
8
9
<settings>
<servers>
<server>
<id>exampleServer</id>
<username>yourUsername</username>
<password>yourPassword</password>
</server>
</servers>
</settings>

The advantage of using server label is to avoid uploading sensitive information (username and password of tomcat-user) as a Git project. Of course, it's also possible to indicate the username and password directly in the pom.xml:

Another usage in pom.xml

1
2
3
4
5
6
7
8
9
10
11
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://example.com:8080/manager/text</url>
<username>yourUsername</username>
<password>yourPassword</password>
<path>/projectPath</path>
</configuration>
</plugin>

To deploy, run tomcat:deploy for the first time, and run tomcat:redeploy for updating the project.