Mar*_*kis 52 java profile maven-2 web.xml web-applications
我有一个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>
....
....
Run Code Online (Sandbox Code Playgroud)
如果不能轻松完成,有没有办法获得两个web.xml文件并根据配置文件选择合适的文件?
mat*_*t b 74
有没有办法有两个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>
...
Run Code Online (Sandbox Code Playgroud)
作为必须maven-war-plugin在每个配置文件中指定配置的替代方法,您可以在POM的主要部分中提供默认配置,然后仅针对特定配置文件覆盖它.
或者更简单一点,在<build><plugins>POM 的主体中,使用属性来引用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>
...
Run Code Online (Sandbox Code Playgroud)
小智 45
我在项目中实现了第三个妥协选项.它将所有内容保存在一个web.xml中,同时仍然使它和pom.xml可读.在我的情况下,我需要有时具有安全性,有时根本没有安全性,具体取决于环境.
所以我做的是:
在pom.xml中,定义两个配置文件(或者您需要的多个配置文件).在配置文件中,包括两个属性.当你需要安全性时,你将它们留空,如下所示:
<enable.security.start></enable.security.start>
<enable.security.end></enable.security.end>
Run Code Online (Sandbox Code Playgroud)
如果要排除所有安全性,请按以下方式定义它们:
<enable.security.start><!--</enable.security.start>
<enable.security.end>--></enable.security.end>
Run Code Online (Sandbox Code Playgroud)
然后,您有一个web.xml文件,其中包含以下内容:
${enable.security.start}
<security-constraint>
...
// all of the XML that you need, in a completely readable format
...
</login-config>
${enable.security.end}
Run Code Online (Sandbox Code Playgroud)
必须将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>
...
Run Code Online (Sandbox Code Playgroud)
因此,基本上,当您选择要包含安全性的配置文件时,您会在web.xml中获得两个额外的CRLF.当您选择不包含安全性的配置文件时,XML仍然在web.xml中,但它已被注释掉,因此会被忽略.我喜欢这个,因为您不必担心保持多个文件同步,但XML仍然可读(并且它位于web.xml文件中,人们自然会在其中查找).
And*_*wik 21
评论克里斯克拉克回答.你可以反向 - 所以在开发中你不希望有任何约束(安全或jndi,其他)
<!-- ${enable.security.end}
<security-constraint>
...
</security-constraint>
${enable.security.start} -->
Run Code Online (Sandbox Code Playgroud)
所以在开发中你已经注释了部分.但在生产中它将被翻译成(使用maven profile):
<!-- -->
<security-constraint>
...
</security-constraint>
<!-- -->
Run Code Online (Sandbox Code Playgroud)
和评论的部分将是可见的.
Bri*_*arr 19
"matt b"已经发布了答案,这是最常用的方式.这是我建议99%的时间这样做的方式.
但是,有时候,您的配置文件可能非常复杂,当只有一个XML节不同时复制每个环境的整个文件没有多大意义.在这些情况下,您可以滥用属性过滤来实现目标.
警告,接下来是一个非常管道的磁带解决方案,并不适合胆小的人:
在你的pom.xml中:
html实体转义是解决方案的一部分.如果用大于号和小于号的符号替换所有解决方案,该解决方案将无效.请保留答案......
<properties>
<test.security.config>
<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>
</test.security.config>
</properties>
Run Code Online (Sandbox Code Playgroud)
在你的web.xml中
....
${test.security.config}
....
Run Code Online (Sandbox Code Playgroud)
由于不存在的属性计算为空字符串,因此未设置此属性的配置(或属性为空xml标记)将在此处计算为空行.
这很丑陋,而且这种形式很难修改xml.但是,如果您的web.xml很复杂,并且您更有可能将4-5个web.xml副本变得不同步,那么这可能是一种适合您的方法.
Kri*_*nck 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>
Run Code Online (Sandbox Code Playgroud)
这也有效:
<properties>
<maven.war.filteringDeploymentDescriptors>true</maven.war.filteringDeploymentDescriptors>
</properties>
Run Code Online (Sandbox Code Playgroud)
filterDeploymentDescriptors的官方文档中提供了更多信息.
| 归档时间: |
|
| 查看次数: |
59789 次 |
| 最近记录: |