Tomcat Maven Integration For Faster Development

In this article, we'll take a look at how you can use Apache Tomcat in conjunction with Apache Maven to speed up your development cycle.  

We'll start with some Maven basics, including a look at the various versions of the Maven Tomcat plugin, and then move on to some more advanced concepts, such as archetypesintegrating Maven into your IDE, and continuous deployment.

Tip: Tcat Server integrates seamlessly with Maven to make continuous deployment on Apache Tomcat simple, fast, and effective. Download Tcat now.

About Apache Maven

Apache Maven has swiftly become the most widely used open-source build automation and project management tool across all segments of the Java community.  Maven's Project Object Model approach to builds allows the automation of otherwise very tedious tasks such as gathering dependencies, generating JavaDocs, and distributing a build to various repository systems and application servers.  

To facilitate a uniform build system that speeds up the development cycle through a convention over configuration philosophy, a key focus of the Maven developers has been to support a wide variety of frameworks and use cases through a well developed plugin infrastructure.  Using these plugins, known as "archetypes", it is possible to quickly describe complex processes such as directory cleaning or deployment to multiple targets in an orderly lifecycle model using Maven "phases".

Why You Should Be Using Maven And Tomcat Together

If you're already using a tool like Ant to build and deploy applications to your server, you may be wondering why you should consider using Maven.  The answer is that Maven's approach to build management is so flexible and so well thought out that it has been adopted virtually across the board in the Java community.

There are plugins to automate tasks related to basically every major framework and task, including projects which automate build processes for other JVM-reliant languages such as Groovy, Scala, and Clojure.  Since all of these plugins have been built with the same convention over configuration model in mind, using Maven means applying the work of the entire Java community to making your build process and fast and error-free as possible.  

Need to add a dependency?  No problem - declare it in POM.xml, and Maven will automatically download the necessary components from its repositories.  Need to switch source management systems?  Simply replace your old plugin with a new one.  Integrating Maven into your Tomcat-based development process now means hours and hours of work saved in the future.

How Maven Works

In this section, we'll provide a brief overview of how a typical Maven build works, for users who are new to Maven.  If you already know some things about Maven and are only interested in integration with Tomcat, click here to skip ahead.

In this section, we'll use some Maven-specific vocabulary you may not be familiar with.  An "archetype" is a project template.  A "goal" is a specific group of ordered phases associated with a given plugin, similar to an Ant "task".  A "phase" is one step within a goal.  

Here's a basic overview of how Maven's build process works the first time you use it:

  1. User calls the "archetype" plugin with the "generate" goal to generate a Maven project of the specified type from the standard model for this type of project.  
  2. Maven executes a series of steps associated with this goal, checking local repositories for the dependencies (both Java-standard JARs and Maven plugins) required to build the given archetype.  Maven organizes these files into directories, using the same layout for every project.  This "Standard Directory Layout" helps guarantee that all Maven projects will look familiar to all users, even if they are built with different frameworks.
  3. The user modifies the package's POM.xml configuration file with configuration information appropriate for their project.  Large pieces of this process can be automated via other plugins.
  4. The user calls a Maven "phase" on the project, such as compile, test, or deploy.  Maven automatically triggers all necessary preceding phases to reach the requested phase.  Phases provide command abstraction based on information inherited from the POM.  Thus, calling the "package" phase on a JAR project will execute different steps than calling the same command on a WAR project.

Note that multiple Maven phases and goals can be called in a single line, allowing a user to execute a series of complex build tasks into a specific order on the fly.

Maven Tomcat Plugins

Like many of its functionalities, Maven provides Tomcat-specific goals through the use of independently developed plugins.  There are currently two Tomcat plugins available for Maven.  Here's a look at how to find them and how they differ from one another, to help you find the best plug-in for your needs.

The Mojo Tomcat Plugin

Mojo is a Codehaus-hosted project aimed at Maven plugin-in development. The Tomcat plugin created under this project is the most commonly used tool for integrating Tomcat-specific goals into Maven build files.  

This plugin allows automated manipulation of WAR and exploded applications using the Tomcat Manager, to accomplish goals such as deployment, re-deployment, and start/stop.  

For example, the command "tomcat:deploy" would execute the entire build lifecycle as specified in the project's POM.xml file, and deploy it to the specified Tomcat server.

The Build-Force Tomcat Plugin

Build-Force is a fledgling Maven2 plugin project hosted on Google Code, which released an alternative plugin for Tomcat in late 2009 that defines a wide variety of additional Tomcat goals for manipulating web applications and the Tomcat server itself during the build process, without restarting the Tomcat server.  

These goals include sets to copy web applications to the server for IDE integration, single-JAR deployment to speed up multi-module web app development, and maintenance tasks such as cleaning Tomcat logs and temp directories without stopping the server.

These goals are aimed exclusively at a development environment, and as such can only be used on a locally hosted Tomcat server as of the current version (1.0).

Note that there is no need to choose between these plugins.  You can use elements of both as necessary as you develop your project.

Configuring the Maven Tomcat Plugin

As both of the Tomcat plugins for Maven rely on the Tomcat Manager to execute their goals, using the plugins requires some additional configuration.  Here's an easy step-by-step guide to help you get through this process.

Step 1 - Enable The Tomcat Manager

For security reasons, the Tomcat Manager web application is disabled by default in all standard distributions of Tomcat.  To enable the application on your server, make the following edits to your tomcat-users.xml file:

<tomcat-users>

 <role rolename="manager"/>

 <role rolename="admin"/>

 <user username="admin" password="yourchoice" roles="admin,manager"/>

</tomcat-users>

You can use an alternative username and password if you wish, as long as you add the role "manager" and grant it to a user with the "admin" role.  

Step 2 - Make Maven Aware Of Your Tomcat Server

Next, you'll have to edit Maven's settings.xml file to allow it to login to the manager application.  This file is located in the ".m2/settings" directory.  Make the following changes, substituting the values you chose in Step 1.

<settings>

 <servers>

  <server>

   <id>servername</id>

   <username>admin</username>

   <password>yourchoice</password>

  </server>

 </servers>

</settings>

Step 3 - Define The Path And Server In The POM

Finally, you'll need to add the specific path for deployment to your project's POM.xml file.  Make the following modifications, substituting the appropriate paths, URL, and port, as well as the appropriate groupId and artifactId for the plugin you are using.

<build>

 <finalName>nameofcontext</finalName>

 <plugins>

  <plugin>

    <groupId>org.codehaus.mojo</groupId>

    <artifactId<tomcat-maven-plugin</artifactId>

      <configuration>

        <server>servername</server>

        <url>http:/yourserver:8080/manager</url>

        <path>/path/to/context</path>

      </configuration>

  </plugin>

 </plugins>

</build>

Tomcat-Related Maven Archetypes

The Maven "Archetype" plugin provides two key Maven features: generating a project from a standard template, and creating a new general template from an existing project.  This allows projects to quickly be formatted into Maven's Standard Directory Layout.

There are many Maven archetypes, but the one most relevant to Apache Tomcat are the "webapp" and "wicket" archetypes.  These generate basic frameworks for Java web applications and Apache Wicket framework projects.  

This allows you to generate a basic project build format that documents all necessary dependencies and so on with minimum hassle.  From these frameworks, you can build up your own idioms and create custom archetypes to match the kinds of applications you build most frequently.

These archetypes are currently somewhat outdated when retrieved from Maven's official repository, as they have not yet been migrated from Maven to the new Maven2 format.  To remedy this, MuleSoft developers and Java developer Matthew McCullough teamed up to update the archetypes to the new version.  

These updated webapp and Wicket archetypes implement multiple Maven2-motivated changes such as new lifecycle extensions, improved directory format, and new variable naming conventions, as well as new deployment and redeployment phases for pre and post integration testing leveraged from the Maven repository.  

A patch has been submitted to the Maven archetype trunk, but until the changes are integrated into the next release, you can use access these updated archetypes at the Muleforge repository.  To update Maven with these archetypes, use the following command:

mvn archetype:generate -DarchetypeCatalog=http:/dist.muleforge.org/maven2/

Matthew McCullough also has created a blog post and screencast detailing how the archetypes can be used.

Integrating Maven with Eclipse

If you're reading this article looking for tips to speed up your development cycle, chances are you have already integrated your Tomcat development server into an IDE such as Eclipse for rapid development with graphical tools.  (If you haven't, you should head over to our illustrated Tomcat Eclipse tutorial!)

However, using Tomcat with Eclipse is only the beginning.  Integrating Tomcat, Eclipse, and Maven allows for some really awesome development scenarios.  Not only can you edit Maven POM files within Eclipse, using Eclipse's support features, you can also execute the builds you create from right within Eclipse, utilize Eclipse's repository interaction functionalities with the Maven plugin and dependency repositories, and more.

Eclipse and Tomcat can be integrated using the open-source m2eclipse plug-in developed by Sonatype, a company led by Jason van Zyl, the original creator of Maven.

Other IDEs that support Maven integration with their own plugins include Netbeans, IntelliJ, and JBuilder.

Maven and Tcat Server - Seamless Continuous Deployment

Recently, Maven founder Jason van Zyl and Senior MuleSoft Architect Dan Diephouse presented a webinar on continuous deployment using Maven and Tcat server.  

Using the Tcat Maven Publishing Plugin, application builds can be published directly into the Tcat Server repository as part of the build lifecycle.  They can then be deployed to groups of Tomcat servers with a single click from Tcat Server's central web console.  

 

MuleSoft's Tcat Server makes enterprise Tomcat administration intuitive and simple, providing advanced features such as application provisioning and rollback, configuration management with server profiles, deep diagnostics, and reliable remote restart from a central web console.  

Tcat Server is built on 100% unmodified Apache Tomcat, and is available as a free download for unlimited use during development and pre-production.  

Download Tcat Server today to push your development and deployment cycle into top speed!

 

Recommended for you

Download tcat

Download

Related Articles