About Francisco Ribeiro Junior

Francisco is a senior software engineer working in the telecom/banking domain focused on Web Content Management. He has been technical lead on many projects for different Brazilian and worldwide clients.

Using an Embedded Tomcat with Maven tomcat plugin

When developing a java web application using the Eclipse WTP, we need to have an installation of tomcat in the machine in order to execute the application.

If you are using Maven on your project, you can use the tomcat plugin to run an embedded tomcat installation and test the application. It is very simple as you can see below.

OBS: To execute the steps given in this post you need to have installed in your eclipse the m2e plugin.
 
 

  1. Create an Maven Project in eclipse, select the following maven archetype: maven-archetype-webapp
  2. Insert the groupid and artifactid information

When you create a web project using this archetype, it already adds an index.jsp file.

Now we need to add the plugin information in the pom.xml as we can see below. In this example we are using the plugin for tomcat 7.x:

<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>br.com.xicojunior</groupId>
  <artifactId>test</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>teste Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>test</finalName>
    <plugins>
	        <plugin>
	          <groupId>org.apache.tomcat.maven</groupId>
	          <artifactId>tomcat7-maven-plugin</artifactId>
	          <version>2.2</version>
	        </plugin>
	</plugins>
  </build>
</project>

The only change we made in the file was to add the section to add the tomcat 7 plugin.

After adding the plugin information, we can test it using the following maven goals:

clean install tomcat7:run

To execute those goals in eclipse:

  1. Right Click in the project
  2. Run as… -> Maven build…
  3. Add the goals in the goals field
  4. Click Run

Expect to see the following in the console:

[INFO] Running war on http://localhost:8080/test
[INFO] Creating Tomcat server configuration at C:\workspace\pessoal\redis\teste\target\tomcat
[INFO] create webapp with contextPath: /test
18/01/2014 17:43:53 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
18/01/2014 17:43:53 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
18/01/2014 17:43:53 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
18/01/2014 17:43:54 org.apache.tomcat.websocket.server.WsSci onStartup
INFO: JSR 356 WebSocket (Java WebSocket 1.0) support is not available when running on Java 6. To suppress this message, run Tomcat on Java 7, remove the WebSocket JARs from $CATALINA_HOME/lib or add the WebSocketJARs to the tomcat.util.scan.DefaultJarScanner.jarsToSkip property in $CATALINA_BASE/conf/catalina.properties. Note that the deprecated Tomcat 7 WebSocket API will be available. 
18/01/2014 17:43:54 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]

Then we just need to test the given url in a browser, in my case: http://localhost:8080/test

  • You can find more information and options in the plugin page.

 

Reference: Using an Embedded Tomcat with Maven tomcat plugin from our JCG partner Francisco Ribeiro Junior at the XICO JUNIOR’S WEBLOG blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Using an Embedded Tomcat with Maven tomcat plugin"

  1. anonymous says:

    I need this to develop a command line interface where a text or XML file can be sent to a JIRA to execute multiple individual/unique tasks.

    i need help how to read the command from the command line and parse that in java??

Leave a Reply


five × 4 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books