Saturday, October 01, 2011

Recipe#7: Simple Maven Webapp in Eclipse

How to create a webapp using Maven, and run it on Jetty in Eclipse. This recipe creates an initial webapp that I want to use as playground for ExtJS 4.x. Requires that you already have Eclipse and Maven configured. Everything is done in the Eclipse IDE.

* Menu File > New > Other.
* Select a wizard: Maven > Maven Project [Next].
* Select project name and location: use default workspace location. [Next].
* Select an archetype: maven-archetype-webapp. [Next].
* Specify archetype parameters: Group Id {com.strive}, Artifact Id {extjs4-projects}, Version {0.1-SNAPSHOT}, Package {com.strive.extjs4projects}. [Finish].
* Create the Java sources directory (src/main/java) and set it as a source folder (right-click: Build Path > Use as Source Folder).
* Open the POM and update #name:{ExtJS4 Sample Projects}, remove #dependencies, #build/finalName:{extjs4}.
* Specify properties and plugins (compiler, surefire, war, and jetty) in the POM:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jdk.version>1.6</jdk.version>
  </properties>
  <build>
    <finalName>extjs4</finalName>    
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
        </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.9</version>
      </plugin>
      <plugin>
          <groupId>org.mortbay.jetty</groupId>
          <artifactId>maven-jetty-plugin</artifactId>
          <version>6.1.26</version>
          <configuration>
            <contextPath>/${project.build.finalName}</contextPath>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1.1</version>
      </plugin>
    </plugins>    
  </build>


* Create a Jetty launch configuration to run the webapp in Eclipse: Run > Run configurations. Maven Build | New. Name {JettyRun}, browse workspace and select current project, Goals {jetty:run}. [Apply] then [Run].