在最终战争中打包一个经过Maven转换的web.xml [英] Package a Maven-transformed web.xml in the final war
问题描述
我想先让Maven处理web.xml
文件,然后再将其包含在最终的 war 中.这将取决于配置文件,因此不能选择编辑源XML.
I want to make Maven process the web.xml
file prior to including it in the final war. This is going to be profile-dependent, so editing the source XML is not an option.
我正在使用xml-maven-plugin
将新元素添加到文件中(您可以在此问题中获取其他详细信息),并成功实现了这一目标,将插件将转换后的文件放在target/generated-resources/xml/xslt
中(我想这是默认的目标目录).
I'm using the xml-maven-plugin
to add the new elements to the file (you can get additional details in this question), and had success in achieving it, having the plugin put the trasformed file in target/generated-resources/xml/xslt
(I guess that's a default destination directory).
现在,我需要指示Maven在打包应用程序时选择转换后的文件(而不是src/main/webapp/WEB-INF/
中的文件).
Now I need to instruct Maven to pick up that transformed file (and not the one in src/main/webapp/WEB-INF/
) when packaging the application.
但是我被困在这里,我不知道该怎么走.
But I'm stuck here, I have no idea how to go further.
作为旁注,我将xml-maven-plugin
绑定到process-resources
阶段.
整个插件配置都在最后.
As a side note, I bound the xml-maven-plugin
to the process-resources
phase.
The whole plugin configuration follows at the end.
可以随意建议其他文件夹结构,配置更改,即使这不是正确的选择方式,等等.这是一个测试设置,我正在用它来学习Maven.
Feel free to suggest another folder structure, configuration changes, or even if this isn't the right way to do it, etc — this is a test setup, I'm using it to learn Maven.
<profile>
<id>release-production</id>
<activation>
<activeByDefault>true</activeByDefault></activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
<phase>process-resources</phase>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/webapp/WEB-INF</dir>
<includes>
<include>web.xml</include>
</includes>
<stylesheet>src/main/resources-xml/webxml-transform.xsl</stylesheet>
</transformationSet>
</transformationSets>
</configuration>
</plugin>
</plugins>
</build>
</profile>
我已经看到问题的答案:
I already saw the answer to the question: How to transform web.xml during packaging with maven?, but I need to add whole XML elements to the web.xml
, so I guess that using properties won't work here (at least, I had an error when I tried to define a property with XML content).
推荐答案
这比我想象的要容易(尽管我猜这可以通过更简单的配置来完成).
我当然愿意提出建议.
It was easier than I thought (although I guess this could be done with a simpler configuration).
I'm open to suggestions, of course.
xml-maven-plugin具有一个(未记录?)附加属性:
The xml-maven-plugin has an (undocumented?) additional property: <outputDir />
in its TransformationSet
element (here's my full plugin configuration).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
<phase>process-resources</phase>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/webapp/WEB-INF</dir>
<includes>
<include>web.xml</include>
</includes>
<stylesheet>src/main/resources-xml/webxml-transform.xsl</stylesheet>
<outputDir>target/generated-resources</outputDir>
</transformationSet>
</transformationSets>
</configuration>
</plugin>
第二步:配置 war 插件以读取另一个web.xml文件
告诉Maven-war-plugin新的(转换后的)web.xml
文件所在的位置:
Second step: configure the war plugin to read another web.xml file
Tell the maven-war-plugin where the new (transformed) web.xml
file is:
将xml-maven-plugin
绑定到 process-resources 阶段,我知道当打包 war 时,转换后的web.xml
就在那里.
Having bound the xml-maven-plugin
to the process-resources phase, I know that the transformed web.xml
will be there when the war is packaged.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>target/generated-resources/web.xml</webXml>
</configuration>
</plugin>
这篇关于在最终战争中打包一个经过Maven转换的web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!