Maven does not accidentally filter resources

This is kind of crazy, and I have never seen this happen before during several years of working with Maven. A single simple project (which I myself did not write) will not be able to randomly filter resources, and I cannot understand what can cause it. I can not share the source code of the project, but I will try to use POM as much as possible. Keep in mind that the problem is not with the code, but with Maven , randomly deciding not to filter resources.

First, I configured it in the POM build tag:

<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> 

And in my src / main / resources directory, I have a file called spring -config.xml. This file has several properties that should be replaced with Maven profile properties. I configured my build profiles like this:

 <profile> <id>stage</id> <properties> <env.name>STAGE</env.name> <db.url>jdbc:oracle:thin:@xxx.xxx.com:1521:xxx</db.url> </properties> </profile> 

To create, I ran this command:

 mvn clean package -P stage 

Now this project uses Spring, and it uses the same Spring configuration for testing and execution, so the context will create a database connection when running test cases. Most of the time the build will complete and the test cases will pass. However, about 1 time in 10, test cases will not be executed, because the properties have not been replaced, and Spring is trying to connect to "$ {db.url}" and not "jdbc: oracle: thin: @ xxx.xxx .com: 1521: xxx. "

Oddly enough, about 9 times in 10, a packaged JAR will have the same problem, despite the fact that it just passed the test cases. I checked the target / classes directory and the files there have the same problem. I realized that something strange was happening with the Maven resource plugin at some point in the build life cycle and might be overwriting files incorrectly.

My bandage solution

In the Maven life cycle, order is the compile-> test-> package. Therefore, to make the resources filtered in two phases, which gave me headaches, I configured the resource plugin to work both at the compilation stage and at the testing stage:

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <id>this-is-silly</id> <phase>compile</phase> <goals> <goal>resources</goal> </goals> </execution> <execution> <id>why-must-i-exist</id> <phase>test</phase> <goals> <goal>resources</goal> </goals> </execution> </executions> </plugin> 

It seems that this works consistently, but I still have no earthly idea why I needed to do this for one of the dozens of projects that I have been working on over the past few years. I have never seen Maven do things intermittently before, and it bothers me that he will break again. Any thoughts would be appreciated.

+8
java maven maven-3 maven-resources-plugin
source share
1 answer

As Taylor wrote in the comment above:

Are you running eclipse or some of them at the same time? This could be overwriting a maven filtered resource in an attempt to be "useful."

The problem is the launch of the eclipse. Depending on your eclipse configuration (m2e yes / no), eclipse will simply overwrite your resource files as soon as it decides to update your project.

This can be solved using the Eclipse Maven integration (as opposed to eclipse: eclipse), which fixes resource filtering on the fly.

Or, of course, stopping the eclipse during construction, which is actually impossible to implement as a model of the work process.

+3
source share

All Articles