问题描述
我有一个maven项目,其中有一个war
和几个ear
项目.每个ear
项目都需要稍微不同的war
/WEB-INF/web.xml
.每个ear
的pom.xml
使用com.google.code.maven-replacer-plugin:replacer
和org.codehaus.mojo:truezip-maven-plugin
替换web.xml
中的标记,然后将新的web.xml
放置在最终的<project>-app.ear/web.war/WEB-INF
中.所有这些都非常适合构建和创建最终的EAR工件.
I have a maven project with one war
and several ear
projects. Each ear
project requires a slightly different war
/WEB-INF/web.xml
. Each ear
's pom.xml
uses com.google.code.maven-replacer-plugin:replacer
and org.codehaus.mojo:truezip-maven-plugin
to replace tokens in the web.xml
, and then place that new web.xml
in the final <project>-app.ear/web.war/WEB-INF
. This all works great with building and creating the final EAR artifacts.
我遇到的问题是,当我run
(使用Netbeans,但这没关系)时,用于部署(<project>/target/gfdeploy/first-app/web_war/WEB-INF/web.xml
)的web.xml
是标记化版本.我尝试为deploy
添加执行阶段,但这不起作用.
The problem I'm having is that when I run
(using Netbeans, but that shouldn't matter), the web.xml
used for deployment (<project>/target/gfdeploy/first-app/web_war/WEB-INF/web.xml
) is the tokenized version. I tried adding execution phases for deploy
, but that doesn't work.
如何确保运行部署具有修改后的web.xml
,以便可以在开发过程中测试我的应用?
How can I ensure that the run deploy has the modified web.xml
so I can test my app during development?
这是耳朵pom.xml
的相关部分:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>package-replace</id>
<phase>package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
<execution>
<id>deploy-replace</id>
<phase>deploy</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.parent.basedir}/${web.xml}</file>
<outputFile>${project.build.directory}/${web.xml}</outputFile>
<replacements>
<replacement>
<token>@REALM_NAME@</token>
<value>${web.realm}</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>truezip-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>package-replace-web-xml</id>
<goals>
<goal>copy</goal>
</goals>
<phase>package</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${web.xml}</source>
<outputDirectory>${project.build.directory}/${project.build.finalName}/${web.zip}/WEB-INF</outputDirectory>
</file>
</files>
</configuration>
</execution>
<execution>
<id>package-replace-web</id>
<goals>
<goal>copy</goal>
</goals>
<phase>package</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}/${web.zip}</source>
<outputDirectory>${project.build.directory}/${project.build.finalName}.ear</outputDirectory>
</file>
</files>
</configuration>
</execution>
<execution>
<id>deploy-replace-web-xml</id>
<goals>
<goal>copy</goal>
</goals>
<phase>deploy</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${web.xml}</source>
<outputDirectory>${project.build.directory}/gfdeploy/${project.artifactId}/web-${project.version}_war/WEB-INF</outputDirectory>
</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
推荐答案
我建议您保留默认的 src/main/webapp/WEB-INF/web.xml 完整功能,以便在开发过程中运行.然后,保留一个名为 src/main/webapp/WEB-INF/web-ear.xml 的文件,其中包含所有替换准备.
I suggest you to keep your default src/main/webapp/WEB-INF/web.xml fully functional for running during development. Then, keep a similar file called src/main/webapp/WEB-INF/web-ear.xml with all the replacement preparation.
将所有替换插件策略包装在maven配置文件中,并定位到 web-ear.xml 文件.将一个maven-war-plugin配置添加到此配置文件,该配置将在构建过程中使用替代的 web-ear.xml 文件,而不是默认的 web.xml (检查: http://maven.apache.org/plugins/maven-war-plugin/):
Wrap all your replacement plugin strategy inside a maven profile and targeted to the web-ear.xml file. Add to this profile a maven-war-plugin configuration that will use the alternative web-ear.xml file during build, instead of the default web.xml (check: http://maven.apache.org/plugins/maven-war-plugin/):
<profiles>
<profile>
<id>change-war-profile</id>
<build>
<plugins>
<!-- all your replacement plugins here -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src/main/webapp/WEB-INF/web-ear.xml</webXml>
</configuration>
</plugin>
<plugins>
</build>
</profile>
</profiles>
确保在EAR maven构建期间激活此配置文件:
Make sure to activate this profile during the EAR maven build:
mvn package -Pchange-war-profile
这篇关于使用Maven,如何在部署阶段之前修改部署文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!