日食 +梅文 + Tomcat:使用自定义选项构建 WAR 时测试 Web 应用程序
我正在使用 Eclipse (Helios),带有“m2eclipse”插件。我正在开发一个基于 Maven 的 Web 应用程序项目,我在 Eclipse 内设置的本地 Tomcat 服务器上测试该项目。
一般来说,这或多或少效果很好。 “m2eclipse”有时可能很不稳定......但在大多数情况下,它使我的 POM 和 Eclipse 项目设置保持同步,并且同样使部署的代码在 Tomcat 中保持最新。
然而,最近我又增加了一个皱纹。我有一个 JavaScript 包含文件,从测试环境转到实际生产环境时需要有所不同。这些差异太大,无法通过 Maven 过滤和令牌替换来干净地处理。我需要的是在我的项目中保留两个单独的文件,并且仅部署适合构建配置文件的文件。
我通过 Maven POM 中的一些“targetPath”技巧实现了这一点:
<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>
这构建了一个完美的 WAR 文件,当我将其部署到某个外部 Tomcat 服务器时,它可以正常工作。然而,问题是当在 Eclipse 中运行 Tomcat 时,Eclipse 不使用该 WAR 文件。相反,Eclipse 使用应用程序的分解版本,部署到如下所示的神秘目录:
<workspace>/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/MyApp
... 显然,在 Maven 执行上面所示的小技巧之前,应用程序的文件已复制到此位置。因此,当在 Eclipse 内部进行本地测试时,预期位置中根本不存在任何 JavaScript 包含内容。
有办法解决这个问题吗?也许是 Eclipse 更改,或者 Maven 更改,将文件从 Maven 的“target”目录拉到 Eclipse 的“wtpwebapps”目录?也许还有另一种方法可以完全解决特定于配置文件的包含文件问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 eclipse (Helios) 3.6 开始,“Java EE Module Dependencies”选项被“Deployment Assembly”选项取代。您可以在此“部署程序集”选项中配置要部署到在 Eclipse 内运行的 tomcat 的文件(项目属性 ---> 部署程序集)
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 )
您可以使用 maven-cargo-plugin 部署到本地服务器,而不是直接从 Eclipse 运行。然后,这将使用您的 Maven 生成的战争。
如果您正在进行的测试可以自动化,那么可以将其合并为完全由 Maven 运行的集成测试套件的一部分。
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.