Tag Archives: Tomcat

Using the Tomcat Maven Plugin to run your application

If you really want to be proficient using the Tomcat Maven Plugin you should go here and have a proper read of the plugin goals and usage. I take it for granted that your Maven knowledge is already enough to understand them. If it’s not, then close this tab and start here.

Ok, if you are still here, the quickest way to run your project into an embedded tomcat 7 instance is adding this into your pom’s plugin section:

<plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.0</version>
</plugin>

Now, just do…

mvn tomcat7:run

… in your preferred Maven installation (command-line, Eclipse plugin, Netbeans plugin…) and go to http://localhost:8080/${project.artifactId}/ to check that the project is running fine. Obviously you need replace ${project.artifactId} for your project’s Maven artifactId.

If you want to use a different value for the url, instead of the ${project.artifactId}, then you just have to add a new configuration section to the plugin:

<plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <path>/mycontext</path>
        </configuration>
</plugin>

The plugin uses automatically the last version of Tomcat 7 out there. If, for whatever reason, you want to use a specific version that does not match this one, you can do it, but you’ll have to specify it in the dependencies of the plugin. A bit tedious but easy:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
	  <dependencies>
             <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-util</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-coyote</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jdbc</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-dbcp</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-servlet-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jsp-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jasper</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jasper-el</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-el-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-catalina</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-tribes</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-catalina-ha</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-annotations-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	       </dependencies>
</plugin>

I took the trick to specify the Tomcat version from http://nurkiewicz.blogspot.co.uk/2012/11/standalone-web-application-with.html.