Build and Deploy maven project with embedded tomcat

LinkedIn Tweet Facebook
Build and Deploy maven project with embedded tomcat

You can generate, build and run Java web apps without the need of IDE like Eclipse or NetBeans.

You can do this simply from the command line with maven.

You don't need tomcat installed for this. You just need Java and Maven installations.

Generate the Web app with maven-archetype-webapp

mvn archetype:generate -DgroupId=com.metamug -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

This will generate a folder mywebapp

Build the project

Enter inside the project with

  cd mywebapp

Once inside the webapp. you can run the following to verify if the webapp has generated proper maven project.

mvn compile

But wait, the whole point of writing this article is to fix the issues in the generated webapp. Only after we fix those issues, we can run the project.

Change the tomcat plugin to tomcat7 in pom.xml

The tomcat plugin supported by maven is tomcat7. So it must be updated in the plugin section of pom.xml

<build>
  <finalName>mywebapp</finalName>
  <plugins>
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <port>9090</port>
        <path>/</path>
      </configuration>
    </plugin>
  </plugins>
</build>

Update web.xml version.

The web.xml generated by maven-archetype-webapp is old and doesn't allow expression language to work. This can't be fixed even if you add maven dependency javax.el-api

The generated web.xml uses servlet API 2.3 This web.xml file should be replaced

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

Change the xsd version from 2.3 to 3.0.

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Archetype Created Web Application</display-name>
</web-app>

Run the application

Finally, run the application with the tomcat7 plugin.

    mvn clean install tomcat7:run

You can access the application in the browser with

http://localhost:9090/

The war is bundled and deployed on tomcat.

Add the JSTL Dependency to pom.xml

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

To identify Web API Classes add javax dependency

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

After adding the dependency you rerun with

mvn clean install tomcat7: run

It rebuilds the application and deploys the war file again.

You can edit the generated index.jsp and you will see the changes to the browser. The below JSP file uses both JSTL and Expression Language to confirm the configuration is done correctly.


<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

 <c:set var="x" value="5" />
 <h4> The value of x is: <c:out value="${x}" /> </h4>