Skip to content

technologyMavenTomcatSetup

CSchowalter edited this page Aug 6, 2013 · 10 revisions

In order to get our own servers and services running we use Tomcat servers which are deployed with the help of maven. This tutorial aims to help to understand the basic setup of a new service. Both Maven and Tomcat can also be installed via the packet-manager of most Linux-Distributions, but we will stick to the generic way, which works the same for as good as any (linux) system. Let's get started by opening a new terminal!

Install Maven

We decided to stick to version 3.0.5 for the time being. If you feel like testing a newer version, feel free to tell us if that worked any better, but you might be on your own if you encounter any problems.

Switch to your download directory via

cd ~/Downloads

and download the Maven binary archive

wget http://apache.openmirror.de/maven/maven-3/3.0.5/binaries/apache-maven-3.0.5-bin.tar.gz

if this URL does not work, you can find mirrors here: http://maven.apache.org/download.cgi

Unpack the archive

tar -zxf apache-maven-3.0.5-bin.tar.gz

Since we want to operate Maven the way Apaches's documentations intend to (makes life easier when encountering problems), it is recommended to move it to the appropriate directory

sudo mkdir /usr/local/apache-maven
sudo mv apache-maven-3.0.5 /usr/local/apache-maven/

Install tomcat

Follow Sebastian's Instructions at part 7 until the part where you start the empty tomcat server

pom.xml

In the topmost folder of your Maven project which is named after your project, you will notice a file called "pom.xml". This file contains a lot of information Maven needs to assist you from the compilation of your code until deploying it to your Tomcat server. The syntax is plain XML, so remember to check for proper nesting when editing the file.

Right after the XML name space declaration and modelversion, you will find , and . This is meta-information about your project and is used for naming the folders and files accordingly.

dependencies

Maven can gather dependency packages from its own repository. To let it know which packages to download, you need to set up the section. The obligatory javax.servlets you need for your web applications can for example be setup like this:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

You might wonder, why there is "dependencies" around "dependency". Just think of "dependencies" as an list of dependencies, and "dependency" as an element of this list. Let's add JUnit as another element and it becomes obvious:

  <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>
    </dependency>
</dependencies>

To find out the IDs and other attributed, take a look at the Maven repository

build options

The most important thing you'll need for compiling and packaging a web application are two plugins. These belong to the build section. If we think in XML, that means we have tags around tags and between those, there are two .

The first one ist the compiler-plugin. Searching the Maven repository, you will find out its IDs and possible version-numbers (remember, we stuck to 3.0), so you can take them from there. Additionally, we want to specify a certain Java version. This belongs to a environment which contains the tags and

The second one is the war-plugin, which is responsible for creating WAR-files.

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
    <webResources>
        <resource>
        <directory>tomcat/</directory>
        <targetPath>WEB-INF</targetPath>
        </resource>
    </webResources>
</configuration>

notice that contains a path. This is where you will find the war-file after successfull packaging.

Finally, the build-part should look somehow like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>tomcat/</directory>
                        <targetPath>WEB-INF</targetPath>
                    </resource>
                </webResources>
            </configuration>
       </plugin>
    </plugins>
</build>

web.xml

The web.xml file, found in the tomcat-directory of your project, influences the behavior of your tomcat-server. It contains information about

  • the servlets' names ** this is needed for other parts of the configuration
  • what the servlet-classes are,
  • if there is a context listener which needs to be updated
  • the servlet-mapping ** especially the url-pattern is of interest as you might not want your URLs to resemble the Java-Servlets's names

commands to get going

If you want to start a project from scratch, cd to the directory you wish your project to be located and use the following line adapted to your project to let Maven generate the directory structure for you:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

As soon as you have some code worth compiling, tell Maven to do so:

mvn compile

Notice how verbose Maven is on you command line. It is highly recommended to read the console output as in case of compilation errors, it usually gives good hints where to start looking for the problems' source.

In case the compilation (finally) succeeded, Maven packs a WAR-file for you if you enter

mvn package war:war

You can find the file in the folder set in the pom.xml

Clone this wiki locally