我正在通过 Eclipse 处理 Maven Web 项目。在 web.xml ,我有一个上下文参数,它的值应该根据我在运行 Maven 时使用的配置文件而改变。

<context-param> 
    <param-name>producao</param-name> 
    <param-value>${ambiente.producao}</param-value> 
</context-param> 

在项目的 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> 

我同时使用 resources根据我在互联网上找到的引用资料,标记为 maven-war-plugin 插件。但是,它没有按预期工作。在 Eclipse 中,我使用目标全新安装和 prod 或 desenv 作为“配置文件”运行 maven。运行 Maven 后,我在 web.xml 中观察到了这一点。 , ${ambiente.producao}属性不会被替换。
因此,我想知道我做错了什么。我应该只使用过滤资源还是 maven-war-plugin?

谢谢,

拉斐尔·阿方索

请您参考如下方法:

不要相信您在 Internet 上阅读的所有内容。您应该从资源列表中删除 src/main/webapp/WEB-INF/,它只会将您的 web.xml 过滤为 target/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> 

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

mvn clean package -Pdesenv



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

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

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

并且为了启用 desenv profile 自动在 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> 

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

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


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!