Servlets, Tomcat, Maven, and Jenkins

Today, I had a little bit trouble setting up a Jenkins job to compile and deploy a Java Web archive with servlets on Tomcat 7 using Maven. I found valuable information spread over many Web pages and I’ll summarize my results here.

First of all, the project’s pom.xml file:

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
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SimpleBackend</groupId>
	<artifactId>SimpleBackend</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<resources>
			<resource>
				<directory>src</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<configuration>
					<url>http://SERVER:8080/manager/text</url>
					<server>tomcat</server>
					<path>/PATH</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.3.1</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>14.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-math3</artifactId>
			<version>3.1.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>

Keep in mind that packaging is set to war and not jar, because we want to have a Web archive at the end. With servlets developed against version 3.0 of the Servlet API and for which you have used Java annotations, attribute failOnMissingWebXml must be set to false. Finally, note that for the Tomcat Maven plugin, the URL of the Tomcat server must be http://SERVER:8080/manager/text and not http://SERVER:8080/manager/html. In the configuration part for the Tomcat Maven plugin you find a reference to a server named tomcat. You define Tomcat manager login and password in your local Maven settings.xml file under this name. For example:

1
2
3
4
5
6
7
8
9
<settings>
   <servers>
      <server>
         <id>tomcat</id>
         <username>USERNAME</username>
         <password>PASSWORD</password>
      </server>
   </servers>
</settings>

This file should be available locally on your development system and on the server on which Jenkins is running. Put it to $JENKINS_HOME/.m2, which is /var/lib/jenkins/.m2 on Ubuntu.
On the server where Tomcat is running, create a user that is allowed to use Tomcat’s manager interface by adding the following line to file $TOMCAT_HOME/conf/tomcat_users.xml:

1
<user username="USERNAME" password="PASSWORD" roles="manager-gui,manager-script"/>

It’s important to add both roles! Of course, USERNAME and PASSWORD must be equal to the ones used in file settings.xml.
Finally, you can create a new Tomcat Maven 2/3 job and use tomcat:redeploy as goal.

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.


*