我有一个Web应用程序Maven项目,我想根据正在运行的配置文件自定义web.xml文件。我正在使用Maven-War-plugin,它允许我定义一个“资源”目录,可以过滤文件。但是,单独过滤对我来说还不够。
更详细地说,我希望包含(或排除)整个安全部分,具体取决于我正在运行的配置文件。这是部分:
....
....
<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文件并根据配置文件选择合适的文件?
答案 0 :(得分:71)
有没有办法获得两个web.xml文件并根据配置文件选择合适的文件?
是的,在每个配置文件中,您可以添加maven-war-plugin
的配置,并将每个配置配置为指向其他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>
...
作为必须在每个配置文件中指定maven-war-plugin
配置的替代方法,您可以在POM的主要部分中提供默认配置,然后仅针对特定配置文件覆盖它。
或者更简单一点,在POM的主<build><plugins>
中,使用属性引用webXml
属性,然后在不同的配置文件中更改它的值
<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>
...
答案 1 :(得分:44)
我在项目中实施了第三个妥协选项。它将所有内容保存在一个web.xml中,同时仍然使它和pom.xml可读。在我的情况下,我有时需要有安全性,有时根本没有安全性,具体取决于环境。
所以我做的是:
在pom.xml中,定义两个配置文件(或者您需要的多个配置文件)。在配置文件中,包括两个属性。如果您需要安全性,请将它们留空,如下所示:
<enable.security.start></enable.security.start>
<enable.security.end></enable.security.end>
如果要排除所有安全性,请按以下方式对其进行定义:
<enable.security.start><!--</enable.security.start>
<enable.security.end>--></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-plugin配置为使用过滤。我看起来像这样:
<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>
...
因此,基本上,当您选择要包含安全性的配置文件时,您的web.xml中会有两个额外的CRLF。当您选择不包含安全性的配置文件时,XML仍然在web.xml中,但它被注释掉,因此被忽略。我喜欢这个,因为您不必担心保持多个文件同步,但XML仍然可读(并且它位于web.xml文件中,人们自然会在其中查找)。
答案 2 :(得分:21)
评论Chris Clark回答。你可以反向 - 所以在开发中你不希望有任何约束(安全或jndi,其他)
<!-- ${enable.security.end}
<security-constraint>
...
</security-constraint>
${enable.security.start} -->
所以在开发中你已经注释了部分。但在生产中它将被翻译成(使用maven profile):
<!-- -->
<security-constraint>
...
</security-constraint>
<!-- -->
和评论部分将可见。
答案 3 :(得分:19)
答案 4 :(得分:10)
在2.1-alpha-2版本的maven-war-plugin中添加了一个新配置。
它的名字是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>
official documentation of filteringDeploymentDescriptors中提供了更多信息。
答案 5 :(得分:4)
对https://stackoverflow.com/a/3298876/2379360
的改进不要指定自定义属性,而是在不同的配置文件中使用默认属性maven.war.webxml。
<profiles>
<profile>
<id>profile1</id>
<properties>
<maven.war.webxml>path/to/custom/webXml</webXmlPath>
</properties>
</profile>
</profiles>
答案 6 :(得分:0)
is there a way to have two web.xml files and select the appropriate one depending on the profile?
除了matt b建议的方法之外,反过来考虑它是有用的,主要是因为在许多情况下,您必须捆绑maven插件未涵盖的应用程序服务器特定配置(afaik)。这些可能在配置文件之间存在差异。
具体来说,您可以使用包含不同配置文件的Web项目之间所有公共文件的父项目。然后,子项目可以具有不同的web.xml文件,其余的ID使用配置文件和maven-war-plugin
完成。例如,我使用此布局来实现针对不同目标环境(开发,uat等)的无人参与构建(除了指定配置文件之外)。
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>