Maven: how to populate a variable in web.xml

I work in a Maven web project through Eclipse. In web.xml , I have a parameter parameter whose value should change according to the profile that I use when starting Maven.

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

In the pom file for the 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 use the resources tag as a maven-war-plugin according to the links I found on the Internet. However, this did not work as expected. In Eclipse, I run maven with the goals of a clean install and as โ€œProfilesโ€ either prod or desenv. After starting Maven, I noticed that in the web.xml ${ambiente.producao} property is not replaced. Therefore, I would like to know what I did wrong. Should I use only a filter resource or maven-war plugin?

Thanks,

Rafael Afonso

+5
source share
1 answer

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

You only need the maven-war-plugin configuration:

 <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 are building in the Maven CLI or inside Eclipse, you need to enable one of these profiles. At the command prompt you need to run

mvn clean package -Pdesenv

In Eclipse (if you are using a Luna Java EE or later distribution), you can enable the profile you want by pressing Ctrl+Alt+P If you enable desenv , you will see the filtered web.xml under target/m2e-wtp/web-resources/WEB-INF/ This is the file that will be published on your application server.

If you need to enable both profiles at the same time, the last profile specified in your pom.xml takes precedence.

Personally, I delete the production profile and put the production properties in the default properties section of your pom.xml. This way you always filter your web.xml with the actual values, and you will have less chance of accidentally creating and deploying your application with non-production settings.

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

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

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

+16
source

Source: https://habr.com/ru/post/1209543/


All Articles