Saturday Aug 31

Key Concepts

Eclipse Web App using Maven

PDFPrintE-mail
Tuesday, 10 May 2011 09:23
AddThis Social Bookmark Button

Create a skeleton web application using Maven

Create a web app skeleton using the maven archetype plugin 

Create web application using Maven

mvn archetype:generate -DgroupId=ch.javatutorial.sample -DartifactId=java-webapp -DarchetypeArtifactId=maven-archetype-webapp

A project with the following project's pom.xml-file should be created:

Maven plugin configuration

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>ch.javatutorial.sampl</groupId>
  <artifactId>java-webapp</artifactId>
<packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>java-webapp</finalName>
  </build>
</project>

Create eclipse configuration files

Maven eclipse configuration command

 mvn -Dwtpversion=2.0 compile eclipse:eclipse

The plugin actually can create WTP R7, 1.0, 1.5 and 2.0 configuration files.

What version of Eclipse does WTP work with?

Eclipse Version WTP Version

WTP 1.0.x

Eclipse 3.1.x

WTP 1.5.x

Eclipse 3.2 (a.k.a. Eclipse Callisto)

WTP 2.0.x

Eclipse 3.3 (a.k.a. Eclipse Europa)

WTP 3.0.x

Eclipse 3.4 (a.k.a. Eclipse Ganymede)

WTP 3.1

Eclipse 3.5 (a.k.a. Eclipse Galileo)

WTP 3.2

Eclipse 3.6 (a.k.a. Eclipse Helios).

WTP 3.3

Eclipse 3.7/4.1

Eclipse’s “.classpath” and “.project” configuration files are created. And you will noticed a new “.setting” folder is created. Inside contains “org.eclipse.wst.common.component” and “org.eclipse.wst.common.project.facet.core.xml“, both files for WTP or Faces support in Eclipse.

The produced org.eclipse.wst.common.project.facet.core.xml should look like :

<faceted-project>
  <fixed facet="jst.java"/>
  <fixed facet="jst.web"/>
  <installed facet="jst.web" version="2.4"/>
  <installed facet="jst.java" version="1.5"/>
</faceted-project>

if you have declared a dependency to servlet-api the jst.web version will take the dependency version.
In the below example you will have a version 6.0 for your jst.web according to your dependency definition

jst.web facet version

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>servlet-api</artifactId>
    <version>6.0.32</version>
</dependency>

The element jst.java indicate the compliance level of the source. The JDK Version should be the same as the mentionned version in the compiler plugins 

The org.eclipse.wst.common.component file should look like


<project-modules id="moduleCoreId" project-version="1.5.0">
  <wb-module deploy-name="java-webapp">
    <property 
         name="context-root"    
         value="java-webapp"/>
    <wb-resource 
         deploy-path="/" 
         source-path="src/main/webapp"/>
    <property 
         name="java-output-path" 
         value="/target/classes"/>
    <wb-resource 
         deploy-path="/WEB-INF/classes" 
         source-path="src/main/resources"/>
  </wb-module>
</project-modules>

WTP support for version higher than 2.0

The support of wtp was removed from the plugin but you can still define your file settings manually using the additionalConfig parameter.

Let's imaging you have a webapp named web and you want to add a sub project called subModule. The webapp should run on Apache Geronimo

WTP requires three settings files :

  • .settings/org.eclipse.wst.common.component
  • .settings/org.eclipse.wst.common.project.facet.core.xml

Let's create the corresponding files in our Maven project

Component description

The file org.eclipse.wst.common.component defines the WTP name of the project, the various folders and eventually references to sub-projects.

The subModule project should be name subModule in your Eclipse Workspace.

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
  <wb-module deploy-name="web">
      
      <wb-resource deploy-path="/" source-path="src/main/webapp" tag="defaultRootSource"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/java"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/resources"/>
    
    <dependent-module archiveName="subModule.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/subModule/subModule">
            <dependency-type>uses</dependency-type>
    </dependent-module>
    
    <property name="context-root" value="web"/>
    <property name="java-output-path" value="/web/target/classes"/>
  </wb-module>
</project-modules>

The file should be located in your maven project structure as :

src\main\config\geronimo\org.eclipse.wst.common.component

Facet definition

Faceted Project Framework provides a powerful mechanism for extending the capabilities of the Web Tools Platform. Project facets are typically used as a way of adding functionality to a project. When a facet is added to the project it can perform any necessary setup actions such as copying resources, installing builders, adding natures, etc. Facets can also be used as markers for enabling user interface elements. In the following snippet we are using Apache Geronimo in version 3.0 and JSF 

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
     <runtime name="Apache Geronimo v3.0"/>
     <fixed facet="jst.web"/>
     <fixed facet="java"/>
     <fixed facet="wst.jsdt.web"/>
     <installed facet="java" version="1.6"/>
     <installed facet="jst.web" version="3.0"/>
     <installed facet="org.apache.geronimo.facet" version="1.3"/>
     <installed facet="jst.jsf" version="2.0"/>
     <installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>

The file should be located in your maven project structure as :

src\main\config\geronimo\org.eclipse.wst.common.project.facet.core.xml

Eclipse Plugin configuration

We have create the files required to configure our WTP project. We can now update our Eclipse plugin configuration to copy the xml files define in the previous chapter at the right place in our Eclipse settings folder.

<plugin> 
          <groupId>org.apache.maven.plugins</groupId> 
          <artifactId>maven-eclipse-plugin</artifactId>
          <version>2.8</version>
          <configuration>
              <projectNameTemplate>[artifactId]</projectNameTemplate>
              <wtpapplicationxml>true</wtpapplicationxml>
              <wtpversion>2.0</wtpversion>
              <downloadSources>true</downloadSources>
              <downloadJavadocs>true</downloadJavadocs>
              <useProjectReferences>true</useProjectReferences>
              <workspace>..</workspace>
              <additionalConfig>
                  <file>
                      <name>.settings/org.eclipse.wst.common.project.facet.core.xml</name>
                      <location>file:///${basedir}/src/main/config/${appserver.name}/org.eclipse.wst.common.project.facet.core.xml</location>
                  </file>
                  <file>
                      <name>.settings/org.eclipse.wst.common.component</name>
                      <location>file:///${basedir}/src/main/config/${appserver.name}/org.eclipse.wst.common.component</location>
                  </file>
              </additionalConfig>                    
          </configuration>
      </plugin>

To update our ecllipse project you can execute the following Maven command :

mvn eclipse:clean eclipse:eclipse

Conclusion

Using the additionalConfig parameter is not as convenient as generating the setting files by the plugin but it gives you much more control on WTP to provide the settings file by yourself. You can also define plenty of additional configuration using the same principle.


Related articles

Tags: eclipse, artifactid, dependency, version, create, java-webapp, jst.web

Comments  

 
0 #3 Arthur 2012-07-05 08:19
Thanks for the great post.!!

please won't you mind updating it according the evolution of both maven (from 2 to 3) and eclipse (from Indigo a new version is about to come out) ?

so the next I visit your post, you'll be current. Thanks you
Quote
 
 
0 #2 S├ębastien Dante Ursini 2012-05-25 13:23
Quoting Eildosa:
So it is not possible to generate the XML file for a 3.3 WTP plugin?

Meaning each time I need to import an EJB project into eclipse (without m2e) I'll need to manually set the facets?

The support of WTP was removed from the plugin but you can specify the faces configuration files using the configuration/additionalConfi g tags. I will add this procedure in the document
Quote
 
 
0 #1 Eildosa 2012-05-23 09:43
So it is not possible to generate the XML file for a 3.3 WTP plugin?

Meaning each time I need to import an EJB project into eclipse (without m2e) I'll need to manually set the facets?
Quote
 

Add comment


Security code
Refresh

Java Tutorial on Facebook