Maven:自定义web-app项目的web.xml

我有一个Web应用程序的Maven项目,我想定制web.xml文件取决于正在运行的configuration文件。 我正在使用Maven-War-plugin,它允许我定义一个“资源”目录,文件可能被过滤。 但是,单靠过滤对我来说是不够的。

更详细地说,我想包括(或排除)关于安全性的整个部分,这取决于正在运行的configuration文件。 这是部分:

.... .... <security-constraint> <web-resource-collection> <web-resource-name>protected</web-resource-name> <url-pattern>/pages/*.xhtml</url-pattern> <url-pattern>/pages/*.jsp</url-pattern> </web-resource-collection> <auth-constraint> <role-name>*</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>${web.modules.auth.type}</auth-method> <realm-name>MyRealm</realm-name> </login-config> <security-constraint> .... .... 

如果这不是很容易,有没有办法有两个web.xml文件,并根据configuration文件select适当的一个?

有没有办法有两个web.xml文件,并根据configuration文件select适当的一个?

是的,在每个configuration文件中,您可以添加maven-war-pluginconfiguration,并将其configuration为指向不同的web.xml

 <profiles> <profile> <id>profile1</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webXml>/path/to/webXml1</webXml> </configuration> </plugin> ... 

作为必须在每个configuration文件中指定maven-war-pluginconfiguration的替代方法,您可以在POM的主要部分中提供默认configuration,然后针对特定configuration文件覆盖它。

或者更简单一些,在POM的主要<build><plugins>中,使用一个属性来引用webXml属性,然后在不同的configuration文件中更改它的值

 <properties> <webXmlPath>path/to/default/webXml</webXmlPath> </properties> <profiles> <profile> <id>profile1</id> <properties> <webXmlPath>path/to/custom/webXml</webXmlPath> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webXml>${webXmlPath}</webXml> </configuration> </plugin> ... 

在我的项目中实现了第三个妥协选项。 它将所有内容保留在一个web.xml中,同时使它和pom.xml都可读。 就我而言,我有时需要有安全感,有时甚至没有安全感,这取决于环境。

所以我做的是:

在pom.xml中,定义两个configuration文件(或者你需要的很多)。 在configuration文件中包含两个属性。 当你想要安全时,你可以把它们留空,就像这样:

 <enable.security.start></enable.security.start> <enable.security.end></enable.security.end> 

当你想排除所有的安全性时,你可以按如下定义它们:

 <enable.security.start>&lt;!--</enable.security.start> <enable.security.end>--&gt;</enable.security.end> 

然后,您将拥有一个包含以下内容的web.xml文件:

 ${enable.security.start} <security-constraint> ... // all of the XML that you need, in a completely readable format ... </login-config> ${enable.security.end} 

必须将pom.xml maven-war-pluginconfiguration为使用过滤。 我看起来像这样:

  <configuration> <webResources> <resource> <filtering>true</filtering> <directory>src/main/webapp</directory> <includes> <include>**/web.xml</include> </includes> </resource> </webResources> <warSourceDirectory>src/main/webapp</warSourceDirectory> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> ... 

所以,基本上,当您select包含安全性的configuration文件时,您会在web.xml中获得两个额外的CRLF。 当您selectconfiguration文件不包括安全性时,XML仍然在web.xml中,但它被注释掉,所以被忽略。 我喜欢这个,因为你不必担心保持多个文件同步,但XML仍然是可读的(在web.xml文件中,人们会自然而然地find它)。

“马特b”已经公布了答案,这是做的最maven的方式。 这是我build议99%的时间做的方式。

但是,有时您的configuration文件可能会非常复杂,如果只有一个XML节有所不同,那么为每个环境复制整个文件就没有什么意义了。 在这些情况下,您可以滥用属性过滤来实现您的目标。

警告,一个非常大胆的解决scheme是遵循的,而不会是心灰意冷:

在你的pom.xml中:

注意StackOverflow编辑器!

html实体转义是解决scheme的一部分。 如果用大于号和小于号的符号replace全部解决scheme,解决scheme将不起作用。 请留下答案是…

 <properties> <test.security.config> &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;protected&lt;/web-resource-name&gt; &lt;url-pattern&gt;/pages/*.xhtml&lt;/url-pattern&gt; &lt;url-pattern&gt;/pages/*.jsp&lt;/url-pattern&gt; &lt;/web-resource-collection&gt; &lt;auth-constraint&gt; &lt;role-name&gt;*&lt;/role-name&gt; &lt;/auth-constraint&gt; &lt;/security-constraint&gt; &lt;login-config&gt; &lt;auth-method&gt;${web.modules.auth.type}&lt;/auth-method&gt; &lt;realm-name&gt;MyRealm&lt;/realm-name&gt; &lt;/login-config&gt; &lt;security-constraint&gt; </test.security.config> </properties> 

在你的web.xml中

 .... ${test.security.config} .... 

因为不存在的属性评估为空string,所以没有该属性集的configuration(或属性是空的xml标记)将在这里评估为空行。

这是丑陋的,XML很难修改这种forms。 但是,如果你的web.xml很复杂,而且你的web.xml有4-5个拷贝的风险更大,那么这可能是一个适合你的方法。

克里斯·克拉克评论回答。 你可以反向 – 所以在开发中你不想有任何约束(安全或jndi,其他)

 <!-- ${enable.security.end} <security-constraint> ... </security-constraint> ${enable.security.start} --> 

所以在开发中你已经注释掉了部分。 但是在制作中它会被翻译成(用maven profile):

 <!-- --> <security-constraint> ... </security-constraint> <!-- --> 

和评论部分将是可见的。

在2.1-alpha-2版本中,maven-war-plugin中添加了新的configuration。 它的名字是filteringDeploymentDescriptors ,它确实是你想要的。

这工作:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors> </configuration> </plugin> 

这也适用于:

 <properties> <maven.war.filteringDeploymentDescriptors>true</maven.war.filteringDeploymentDescriptors> </properties> 

filteringDeploymentDescriptors的官方文档中提供了更多信息。

 is there a way to have two web.xml files and select the appropriate one depending on the profile? 

除了由matt提出的方法之外,另一种方式也是有用的,主要是因为在很多情况下,您将不得不捆绑maven插件(afaik)未涵盖的应用程序服务器特定configuration。 这些可能在configuration文件之间有很大差异。

具体而言,可以使用具有不同configuration文件的Web项目之间的所有常用文件的父项目。 然后子项目可以有不同的web.xml文件,其余的id用configuration文件和maven-war-plugin 。 例如,我使用这种布局来实现针对不同目标环境(开发,uat等)的无人值守构build(除指定configuration文件外)

 WebPc ├── common │ ├── css │ ├── images │ ├── js │ └── WEB-INF │ └──├── wsdl │── pom.xml │ ├── WebPc-DEV │ ├── pom.xml │ └── src │ └── main │ └── webapp │ └── WEB-INF │ ├── geronimo-web.xml │ ├── ibm-web-bnd.xml │ ├── ibm-web-ext.xml │ └── web.xml ├── WebPc-UAT │ ├── pom.xml │ └── src │ └── main │ └── webapp │ └── WEB-INF │ ├── geronimo-web.xml │ ├── ibm-web-bnd.xml │ ├── ibm-web-ext.xml │ └── web.xml 

WebPc的pom有以下pom

 <groupId>my.grp</groupId> <artifactId>WebPc</artifactId> <packaging>pom</packaging> <profiles> <profile> <id>DEV</id> <activation> <activeByDefault>true</activeByDefault> </activation> <modules> <module>WebPc-DEV</module> </modules> </profile> <profile> <id>UAT</id> <modules> <module>WebPc-UAT</module> </modules> </profile> </profiles> <build> <pluginManagement> <plugins> <!-- copy common resources located on parent project common folder for packaging --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <resourceEncoding>${project.build.sourceEncoding}</resourceEncoding> <webResources> <resource> <directory>../common</directory> <excludes> <exclude>WEB-INF/**</exclude> </excludes> </resource> <resource> <directory>../common/WEB-INF</directory> <includes> <include>wsdl/*.wsdl</include> <include>wsdl/*.xsd</include> </includes> <targetPath>WEB-INF</targetPath> </resource> </webResources> </configuration> </plugin> </plugins> </pluginManagement> </build> 

这是WebPc-DEV的POM

 <parent> <groupId>my.grp</groupId> <artifactId>WebPc</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>WebPc-DEV</artifactId> <packaging>war</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> </plugin> </plugins> </build> 

https://stackoverflow.com/a/3298876/2379360的改进;

而不是指定一个自定义属性,使用您的不同configuration文件中的默认属性maven.war.webxml。

 <profiles> <profile> <id>profile1</id> <properties> <maven.war.webxml>path/to/custom/webXml</webXmlPath> </properties> </profile> </profiles>