Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse + Maven + Tomcat: testing web apps when the WAR is built with custom options

I am using Eclipse (Helios), with the "m2eclipse" plugin. I am working on a Maven-based web application project, which I test on a local Tomcat server that is setup inside Eclipse.

Generally, this works more or less great. "m2eclipse" can sometimes be flaky... but for the most part it keeps my POM and my Eclipse project settings in sync, and likewise keeps the deployed code current in Tomcat.

However, recently I've added a wrinkle. I have one JavaScript include file that needs to be different when going from the test environment to the real production environment. The differences are too significant to be cleanly handled by Maven filtering and token substitution. What I needed was to keep two separate files in my project, and only deploy the one appropriate for the build profile.

I accomplished this with some "targetPath" trickery in my Maven POM:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.1.1</version>
   <configuration>
      <webResources>
         <resource>
            <!-- take the stuff from a "prod" or "non-prod" subdirectory and copy it one level up -->
            <directory>src/main/webapp/js/${profile.name}</directory>
            <targetPath>js</targetPath>
         </resource>
         <resource>
            <!-- now don't deploy those profile-specific subdirectories -->
            <directory>src/main/webapp/js</directory>
            <excludes>
               <exclude>prod</exclude>
               <exclude>non-prod</exclude>
            </excludes>
         </resource>
      </webResources>
   </configuration>
</plugin>

This builds a perfect WAR file, which works fine when I deploy it some external Tomcat server. However, the problem is that Eclipse does NOT use that WAR file when running Tomcat inside of Eclipse. Instead, Eclipse works with an exploded version of the application, deployed to a cryptic directory like this:

<workspace>/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/MyApp

... and apparently the app's files are copied to this location PRIOR TO Maven doing the little trickery shown above. Therefore, when testing locally inside of Eclipse, no JavaScript include is present at all in the expected location.

Is there a way around this problem? Maybe an Eclipse change, or a Maven change, to pull the file from Maven's "target" directory to Eclipse's "wtpwebapps" directory? Maybe there's another approach to solving the profile-specific-include-file issue altogether?

like image 442
Steve Perkins Avatar asked Jun 24 '11 17:06

Steve Perkins


2 Answers

Starting from eclipse (Helios) 3.6 , the option “Java EE Module Dependencies” is replaced by “Deployment Assembly” option . You can configure which files to be deployed to the tomcat running inside of Eclipse in this “Deployment Assembly” option (Project Properties ---> Deployment Assembly )

enter image description here

like image 149
Ken Chan Avatar answered Nov 06 '22 16:11

Ken Chan


You could deploy to the local server using the maven-cargo-plugin instead of running from Eclipse directly. This would then use your maven generated war.

If the tests you are doing can be automated, this could then be incorporated as part of an integration test suite that would be completely run by maven.

like image 30
Robin Avatar answered Nov 06 '22 15:11

Robin