Maven:如何在web.xml文件中填充变量

Raf*_*nso 5 eclipse m2eclipse maven

我正在通过Eclipse在Maven Web项目中工作.在web.xml,我有一个context-param,其值应根据我运行Maven时使用的配置文件而改变.

<context-param>
    <param-name>producao</param-name>
    <param-value>${ambiente.producao}</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

在项目的pom文件中,我有以下配置:

<project>

    <profiles>
        <profile>
            <id>prod</id>
            <properties>
                <ambiente.producao>true</ambiente.producao>
            </properties>
        </profile>
        <profile>
            <id>desenv</id>
            <properties>
                <ambiente.producao>false</ambiente.producao>
            </properties>
        </profile>
    </profiles>

    <build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/webapp/WEB-INF/</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/web.xml</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
                <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>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

resources根据我在互联网上找到的参考资料使用两个标签作为maven-war-plugin插件.但是,它没有按预期工作.在Eclipse中,我运行目标为clean install的maven,以及proddesenv作为"Profiles" .在我运行Maven后,我观察到,在web.xml${ambiente.producao}属性中没有被替换.因此,我想知道我做错了什么.我应该只使用Filtering资源还是maven-war-plugin?

谢谢,

Rafael Afonso

Fre*_*con 16

不要相信你在互联网上阅读的所有内容.你应该从资源列表中删除src/main/webapp/WEB-INF /,它只会过滤你的web.xmltarget/classes

您的maven-war-plugin配置只需要:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

现在,无论您是在Maven CLI中构建还是在Eclipse中构建,都需要启用其中一个配置文件.在命令行中,您需要运行

mvn clean package -Pdesenv

在Eclipse中(假设您使用的是Luna Java EE发行版或更新版本),您可以通过按下启用所需的配置文件Ctrl+Alt+P.如果启用desenv,您将看到已过滤的web.xml,target/m2e-wtp/web-resources/WEB-INF/这是将发布到您的应用程序服务器的文件.

如果您碰巧同时启用两个配置文件,则pom.xml中列出的最新配置文件优先.

就个人而言,我会删除生产配置文件并将生产属性放在pom.xml的默认属性部分中.这样,您始终可以使用实际值过滤web.xml,并且使用非生产设置意外构建和部署应用程序的可能性较小.

并且为了desenv在Eclipse中自动启用配置文件,您可以添加以下激活规则:

<profile>
  <id>desenv</id>
  <!-- This profile is only activated when building in Eclipse with m2e -->
  <activation>
    <property>
      <name>m2e.version</name>
    </property>
  </activation>
  <properties>
    <ambiente.producao>false</ambiente.producao>
  </properties>
</profile>
Run Code Online (Sandbox Code Playgroud)

有关m2e-wtp中动态资源过滤支持的更多信息,请访问:https://developer.jboss.org/en/tools/blog/2011/05/03/m2eclipse-wtp-0120-new-noteworthy

还有一个截屏视频:http//screencast.com/t/hwodHqODBB