Thursday, August 16, 2012

Webapps with Eclipse and Maven (maven-war-plugin)


Webapps with Eclipse and Maven (maven-war-plugin)


Web applications created by the Eclipse IDE contains an annoying folder WebContent to host the web resources and deployment descriptors. Quite natural for Eclipse users, this feature ignores the Maven convention and force the developers to hack the pom files in order to get the project up and running in Eclipse. In this aspect, Eclipse if far behind the other IDEs regarding Maven support, even if you consider the very good M2Eclipse plugin. So, for you lazy Christmas hackers, here it is a solution for the Maven integration problems in Eclipse based on the maven-war-plugin.
ERRATA: please use the WTP plugin instead of changing the project structure.  I figured out the wtp plugin after writting this blog, so I suggest you to check just the 1st and the 3th steps of the below instructions. I kept the original blog information in case you really need or want to change the project structure.
  1. Create a Web Project in Eclipse: right-click on the Project Explorer > New > Project > Web \ Dynamic Web Project
  2. Create a pom.xml file in the project root folder, with the following content:
  3. <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>org.cejug</groupId>
     <artifactId>webapp-test</artifactId>
     <packaging>war</packaging>
     <version>1.0-SNAPSHOT</version>
     <name>webapp-test Maven Webapp</name>
     <url>http://maven.apache.org</url>
     <repositories>
      <repository>
       <id>maven2-repository.dev.java.net</id>
       <name>Java.net Repository for Maven</name>
       <url>http://download.java.net/maven/2</url>
      </repository>
     </repositories>
    
     <build>
      <finalName>${project.name}</finalName>
      <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
         <webResources>
          <resource>
           <directory>$/WebContent</directory>
          </resource>
         </webResources>
         <warSourceDirectory>WebContent</warSourceDirectory>
         <warSourceExcludes>WebContent/WEB-INF/lib/*.jar</warSourceExcludes>
         <archiveClasses>false</archiveClasses>
         <archive>
          <manifest>
           <addClasspath>true</addClasspath>
           <classpathPrefix />
          </manifest>
          <manifestEntries>
           <url>${pom.url}</url>
           <Implementation-Build>$</Implementation-Build>
           <Implementation-Title>${project.name}</Implementation-Title>
           <Implementation-Vendor>CEJUG</Implementation-Vendor>
           <Implementation-Version>${project.version}</Implementation-Version>
           <Built-By>${user.name}</Built-By>
           <Built-OS>${os.name}</Built-OS>
           <Build-Date>$</Build-Date>
           <SCM>$</SCM>
          </manifestEntries>
         </archive>
        </configuration>
       </plugin>
      </plugins>
     </build>
    </project>
     
  4. Compile and prepare the project for eclipse:
    mvn -Dwtpversion=2.0 compile eclipse:eclipse
Notice the usage of the plugin -Dwtpversion to enable Maven to add Eclipse WTP Support to the project. That simple flag do the trick :). Actually just using that flag will work, but not all plugins of Eclipse will work out of the box without the WebContent folder - it is up to you to decide if it is worthy to modify your project structure or just go straight ahead with the plain Maven folder.
Done, now you can refresh the project in Eclipse and continue to work. Remember that jjust the resources folder is hard coded in Eclipse, the src/main/java continues as Maven expects. Perhaps some day we can have the confluence between the conventions of Maven and Eclipse and then we will finally becomes free of this daily basis hacks.

No comments:

Post a Comment