M2E-WTP FAQ

From Eclipsepedia

Revision as of 07:41, 30 May 2012 by Fbricon.gmail.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
M2E-WTP
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Frequently asked questions about m2e-wtp :

Contents


(Converted from https://github.com/sonatype/m2eclipse-wtp/wiki/Frequently-Asked-Questions)

Common problems

How do I change the context root of my project?

By default, m2e-wtp resolves the context root of a standalone web application from the <warName> value defined in the maven-war-plugin configuration :

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.2</version>
  <configuration>
     <warName>mywebapp</warName>
  </configuration>
</plugin>

If no value is defined, it falls back on the <finalName> value defined in (or inferred from) the <build> section of a war project. So if you need to access the project-war web module from http://localhost:8080/project/ you should define :

<project>
 <groupId>foo.bar</groupId> 
 <artifactId>web-project<artifactId> 
 <version>1.0.0-SNAPSHOT</version> 
 <packaging>war</packaging> 
 <build> 
   <finalName>project</finalName> 
   ...
 </build>
</project>

However, you can also customize the context root used in WTP by setting a custom property in your pom.xml. For instance, if you need to use "/" as your context root, just define a <m2eclipse.wtp.contextRoot> property in the <properties> section of your pom.xml :

<project>
 <groupId>foo.bar</groupId> 
 <artifactId>web-project<artifactId> 
 <version>1.0.0-SNAPSHOT</version> 
 <packaging>war</packaging> 
 <build> 
   <finalName>project</finalName> 
   ...
 </build>
 <properties>
   <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
   ...
 </properties>
</project>

<m2eclipse.wtp.contextRoot> takes precedence over the <warName> and <finalName> values. Update your maven project configuration, restart tomcat, the context root change will be detected and tomcat will ask to update its configuration. Now the same application is accessible at http://localhost:8080/

If your web application is deployed via an EAR package, then the context root is controlled by either the deployed file name of the webapp (for Java EE >= 5), or the context root defined in your application.xml, as usual.

What is this web resources folder?

target/m2e-wtp/web-resources/ is a folder containing automatically generated resources, that need to be deployed on the application server. These generated resources are :

  • pom.properties and MANIFEST.MF generated by the mavenarchiver plugin
  • resources defined in the <webResources> section of the maven-war-plugin's configuration, or filtered web.xml

The target/m2e-wtp/web-resources/ is derived. In Eclipse lingo, it means it'll display a warning if you try to manually edit the files it contains, as they'll most likely be overridden automatically in the next (incremental or not) project build.

If you look in your <project>/.settings/org.eclipse.wst.common.component file, you will see /target/m2e-wtp/web-resources is defined BEFORE the regular war source directory :

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

The rationale behind this order is, if two files from two different source folders collide, WTP will deploy the first one it finds. So if you filter your web.xml sitting under src/main/webapp/WEB-INF, you will want the filtered version to be deployed on the server, that is target/m2e-wtp/web-resources/WEB-INF/web.xml. If, for some reason, you would like to disable the use of target/m2e-wtp/web-resources/, well, know that you can. i

That web resources folder is causing me some trouble, can I get rid of it?

Remember, target/m2e-wtp/web-resources/ is used to allow the deployment of automatically generated web resources with WTP.

On some occasions however, having target/m2e-wtp/web-resources/ might cause some troubles (incompatibilities with WTP editors, IBM RAD, using Servlet.getRealPath(...) in your code).

As a workaround, you can choose to not use target/m2e-wtp/web-resources/ and generate the pom.properties and MANIFEST.MF files in your source directory instead (It'll be your responsibility to add these files to your SCM ignore list).

In order to remove target/m2e-wtp/web-resources/ from the list of deployed folders, you need to change some preferences :

  • on your project only : right-click on the project > Properties > Maven > WTP : check "Enable Project Specific Settings" and uncheck "Maven Archiver generates files under the build directory"
  • on the whole workspace : Window > Preferences > Maven > WTP : uncheck "Maven Archiver generates files under the build directory"

Please note that this setting will be overridden if web resource filtering is in use, that is if the maven-war-plugin configuration declares <webResources> or sets <filterDeploymentDescriptor> to true. The reason is simple : you don't want to see your source files overwritten by the filtering mechanism (and it would also lead to some not-so-funny build loops).

How do I add my web project classes to another project's classpath?

If you need to share your web project classes with another project, the recommended approach is to move the classes to a separate module that builds a JAR, and then declare a dependency on that JAR from your webapp as well as from any other projects that need it.

Another approach involves setting the <attachClasses> attribute of the maven-war-plugin to true :

<project>
 ...
 <groupId>com.company</groupId>
 <artifactId>mywebapp</artifactId>
 <version>1.0.0-SNAPSHOT</version>
 ...
 <build>
   <plugins>
     <plugin>
       <artifactId>maven-war-plugin</artifactId>
       <version>2.1.1</version>
       <configuration>
         <attachClasses>true</attachClasses>
       </configuration>
     </plugin>
   </plugins>
 </build>
 ...
</project>

This would generate a mywebapp-1.0-SNAPSHOT-classes.jar in your local repository. Other maven projects can then add a dependency to the mywebapp jar having the classes classifier :

<dependencies>
 <dependency>
   <groupId>com.company</groupId>
   <artifactId>mywebapp</artifactId>
   <version>1.0.0-SNAPSHOT</version>
   <classifier>classes</classifier>
 </dependency>
</dependencies>

BUT, in the Eclipse workspace, mywebapp is not registered as a jar with the classes classifier, so your dependent projects would have some compilation errors. On the other hand, adding mywebapp to a project classpath, without the war packaging and the classifier would fix the compilation error but would break CLI builds.

A workaround exists though, we need to change the dependency whether the project is built in Eclipse or not. In your dependent project, you can configure the following :

<dependencies>
 ...
 <dependency>
   <groupId>com.company</groupId>
   <artifactId>mywebapp</artifactId>
   <version>1.0.0-SNAPSHOT</version>
   <classifier>${webClassifier}</classifier>
 </dependency>
 ...
</dependencies>
...
<properties>
 ...
 <webClassifier>classes</webClassifier>
</properties>
...
<profiles>
 <profile>
   <id>m2e</id>
   <activation>
     <property>
       <name>m2e.version</name>
     </property>
   </activation>
   <properties>
     <webClassifier></webClassifier>
   </properties>
 </profile>
</profiles>

The m2e profile is automatically activated when the project is built with m2e, ignored in other circumstances. In that case only, the dependent project will use an empty classifier to reference the web project, which will be added to the classpath as expected.

How to use M2E-WTP in IBM RAD?

IBM has put together some documentation on how to use M2E and Rational Application Developer :