Create Web App Using Maven, Add Jetty Plugin & Convert to Eclipse

Using Maven you can create the foundation for a java web project with a simple command. Also, you can run it easily using Jetty Maven plugin.

maven-jetty-eclipse

Objectives:

  • How to create web app project using maven?
  • How to run it using Jetty Maven plugin?
  • How to convert Maven project into Eclipse project?

Environment:

  • Apache Maven
  • Eclipse
  • JDK 1.6

( 1 ) Create an Empty Web App from Maven Template

  • Open command prompt
  • Navigate to the location where you want to create the project at e.g. “C:\Workspace\Maven_Projects”
  • Type the following command and press enter.
>mvn archetype:generate 
-DgroupId=com.hmkcode //enter your group id
-DartifactId=hello-webapp //enter your articfact id
-DarchetypeArtifactId=maven-archetype-webapp //this should be the same
-DinteractiveMode=false

  • Now, you should get the following project created with the same directory structure and 3 files “web.xml, index.js and pom.xml”

( 2 ) Add Jetty Maven Plugin

  • To add Jetty Maven plugin we need to add the following <plugin> to pom.xml
<build>
	  <finalName>hello-webapp</finalName>
	  <plugins>
		  <plugin>        
			  <groupId>org.mortbay.jetty</groupId>        
			  <artifactId>maven-jetty-plugin</artifactId>        
			  <version>6.1.10</version>        
			  <configuration>
				<scanIntervalSeconds>10</scanIntervalSeconds>                  
			  </configuration>        
		  </plugin>
	  </plugins>
  </build>
  • The final pom.xml should look like this
<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>com.hmkcode</groupId>
  <artifactId>hello-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>hello-webapp 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>hello-webapp</finalName>
	  <plugins>
		  <plugin>        
			  <groupId>org.mortbay.jetty</groupId>        
			  <artifactId>maven-jetty-plugin</artifactId>        
			  <version>6.1.10</version>        
			  <configuration>
				<scanIntervalSeconds>10</scanIntervalSeconds>                  
			  </configuration>        
		  </plugin>
	  </plugins>
  </build>

</project>

( 3 ) Jetty:Run

  • Now, to run jetty just type the following command
>mvn jetty:run

( 3 ) Convert to Eclipse

  • To have better editing for the project you can imported to Eclipse.
  • To convert Maven based project into Eclipse project we can run the following Maven command:
>mvn eclipse:eclipse -Dwtpversion=2.0

Before, executing the command add the following to pom.xml under <plugins>

<plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
                <source>1.6</source>
                <target>1.6</target>
          </configuration>
</plugin>

  • Now, open Eclipse> File >> Import.. >> General >> Existing Project into Workspace
  • Select the project “hello-webapp”
  • Finish

You can Import it as an existing General project or as an existing Maven project if you have m2e Maven eclipse plugin installed on your eclipse.