Maven:自定义web-app项目的web.xml [英] Maven: Customize web.xml of web-app project
问题描述
我有一个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文件,并根据配置文件选择适当的?
< blockquote>
是否有办法获得两个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的主要部分提供默认配置,然后只为特定的配置文件覆盖它。
或者更简单,在主<$ c您的POM的$ c>< build>< plugins> ,使用属性来引用 webXml
属性,然后只需更改它的值在不同的个人资料中
< properties>
< webXmlPath> path / to / default / webXml< / webXmlPath>
< / properties>
< profiles>
< profile>
< id> profile1< / id>
< properties>
< webXmlPath>路径/到/ 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>
...
I have a web application Maven project, and I want to customize the web.xml file depending on the Profile that is running. I am using the Maven-War-plugin, which allows me to define a "resources" directory, where the files may be filtered. However, filtering alone is not sufficient for me.
In more detail, I want to include (or exclude) the whole section on security, depending on the profile I an running. This is the part:
....
....
<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>
....
....
If this is not done easily, is there a way to have two web.xml files and select the appropriate one depending on the profile?
is there a way to have two web.xml files and select the appropriate one depending on the profile?
Yes, within each profile you can add a configuration of the maven-war-plugin
and configure each to point at a different 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>
...
As an alternative to having to specify the maven-war-plugin
configuration in each profile, you can supply a default configuration in the main section of the POM and then just override it for specific profiles.
Or to be even simpler, in the main <build><plugins>
of your POM, use a property to refer to the webXml
attribute and then just change it's value in different profiles
<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>
...
这篇关于Maven:自定义web-app项目的web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!