本文介绍了Maven:如何在web.xml文件中填入一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

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

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

 < project> 

<个人资料>
<个人资料>
< id> prod< / id>
<属性>
< ambiente.producao> true< /ambiente.producao>
< / properties>
< / profile>
<个人资料>
< id> desenv< / id>
<属性>
< 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>

我同时使用资源标记为maven -war-plugin插件根据我在互联网上发现的引用。但是,它没有按预期的方式工作。在Eclipse中,我使用目标 clean install 和Profiles或者 prod desenv 运行maven。在我运行Maven后,我发现在 web.xml 中, $ {ambiente.producao} 属性是没有替换
因此,我想知道我做错了什么。我应该只使用过滤资源还是maven-war-plugin?



谢谢,



Rafael Afonso


解决方案

不要相信你在互联网上看到的一切。您应该从资源列表中删除src / main / webapp / WEB-INF /,将只将web.xml过滤到 target / classes



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

 code><插件> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-war-plugin< / artifactId>
< version> 2.5< / version>
< configuration>
< filteringDeploymentDescriptors> true< / filteringDeploymentDescriptors>
< / configuration>
< / plugin>

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

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



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



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



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

 <型材> 
< id> desenv< / id>
<! - 仅在使用m2e构建Eclipse时激活此配置文件 - >
< activate>
< property>
< name> m2e.version< / name>
< / property>
< / activation>
<属性>
< ambiente.producao> false< /ambiente.producao>
< / properties>
< / profile>

更多关于m2e-wtp中的动态资源过滤支持,请执行以下操作:



还有一个屏幕录像:


I am working in a Maven web project through Eclipse. In the web.xml, I have a context-param which value should change according the profile I use when I run the Maven.

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

In the pom file for project I have the following configuration:

<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>

I am using both resources tag as maven-war-plugin plugin according to the references I found on the internet. However, it did not work as expected. In the Eclipse I run the maven with the goals clean install and as "Profiles" either prod or desenv. After I run the Maven, I observed that in the web.xml, the ${ambiente.producao} property is not replaced.Therefore, I would like to know what I did wrong. Should I use only Filtering resource or the maven-war-plugin?

Thanks,

Rafael Afonso

解决方案

Don't believe everything you read on the Internet. You should remove src/main/webapp/WEB-INF/ from the resources list, it will just filter your web.xml to target/classes

Your maven-war-plugin configuration just needs :

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
  </configuration>
</plugin>

Now, whether you build in Maven CLI or within Eclipse, you need to enable one of these profiles. In command line, you need to run

In Eclipse (assuming you're using the Luna Java EE distribution or more recent), you can enable the profile you want by pressing Ctrl+Alt+P. If you enable desenv, you'll see the filtered web.xml under target/m2e-wtp/web-resources/WEB-INF/ This is the file that will get published to your application server.

If you happen to enable both profiles at the same time, the latest profile listed in your pom.xml takes precedence.

Personally, I would remove the production profile and put the production properties in the default properties section of your pom.xml. That way, you always filter your web.xml with actual values and you'll have less chances of accidentally building and deploying your app with non-production settings.

And in order to enable the desenv profile automatically in Eclipse, you can add the following activation rule :

<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>

More on dynamic resource filtering support in m2e-wtp here: https://developer.jboss.org/en/tools/blog/2011/05/03/m2eclipse-wtp-0120-new-noteworthy

And a screencast there : http://screencast.com/t/hwodHqODBB

这篇关于Maven:如何在web.xml文件中填入一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:38