Eclipse + Maven + Tomcat: Testing Web Applications When Building a WAR Using Custom Options

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

This usually works more or less great. "m2eclipse" can sometimes be flaky ... but for the most part it keeps the settings of my POM and my Eclipse project in sync, and also stores the deployed code in Tomcat.

However, recently I added a wrinkle. I have one JavaScript file that should be different when moving from a test environment to a real production environment. The differences are too significant to be handled by Maven filtering and token replacement. I needed to save two separate files in my project and use only those that are suitable for the build profile.

I accomplished this with some "targetPath" tricks 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 creates a perfect WAR file that works great when I deploy it to an external Tomcat server. However, the problem is that Eclipse does NOT use this WAR file when running Tomcat inside Eclipse. Instead, Eclipse works with an exploded version of the application deployed in such a hidden directory:

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

... and, apparently, the application files are copied to this place. PRIOR TO Maven does the little trick shown above. Thus, when testing locally inside Eclipse, JavaScript is not present at all at the expected location.

Is there any way to solve this problem? Perhaps changing Eclipse or changing Maven to pull a file from the "target" directory from Maven to the Eclipse directory "wtpwebapps"? Maybe there is another approach to solving the problem with profile-include-include-file?

+6
source share
2 answers

Starting with eclipse (Helios) 3.6, the Java EE Module Dependencies option is replaced by the Deploy Deployment option. You can configure which files should be deployed to tomcat running inside Eclipse in this "deployment assembly" (Project Properties ---> Assembly Assembly)

enter image description here

+7
source

You can deploy to a local server using the maven-cargo plugin, instead of using Eclipse directly. Then it would use your war generated by maven.

If the tests you do can be automated, it can be included in the integrated test suite, which will be fully run by maven.

+1
source

Source: https://habr.com/ru/post/891304/


All Articles