Maven randomly does not filter resources

1.3k views Asked by At

This is kind of maddening, and I've never seen it happen before in several years of working with Maven. A single, simple project (that I did not write myself) will randomly fail to filter resources, and I cannot figure out what might cause it. I can't share the project source code, but I'll try to share as much of the POM as I can. Keep in mind that the problem is not with the code, it's with Maven randomly deciding not to filter resources.

I initially had this configured in my POM's 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 in it that should be replaced by Maven profile properties. I have configured my build profiles like so:

<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 build, I run this command:

mvn clean package -P stage

Now, this project uses Spring and it uses the same Spring config for testing and execution, so the context will create a database connection when test cases are run. Most of the time, the build will complete and the test cases will pass. However, about 1 time in 10, the test cases will fail because the properties were not replaced and Spring tries to connect to "${db.url}" rather than "jdbc:oracle:thin:@xxx.xxx.com:1521:xxx".

Weirdly, about 9 times in 10, the packaged JAR will have the same problem, despite having just passed the test cases. I checked the target/classes directory, and the files there have the exact same problem. I figured something odd was going on with the Maven resource plugin at a certain point in the lifecycle of the build and maybe it was overwriting the files incorrectly.

My bandaid solution

In the Maven lifecycle, the order is compile->test->package. So to force the resources to be filtered on the two phases that were giving me headaches, I configured the resources plugin to run on both the compile and the test phase:

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

This appears to be working consistently, but I still have no earthly idea why I needed to to do this for a single project out of the dozens I've worked on in the last few years. I've never seen Maven do something intermittently before, which has me worried that it will break again. Any thoughts would be appreciated.

1

There are 1 answers

1
blackbuild On BEST ANSWER

As Taylor wrote in a comment above:

Are you running eclipse or some such at the same time? It might be overwritting the maven-filtered resource in an attempt to be "helpful".

Problem is a running a eclipse. Depending on your eclipse configuration (m2e yes/no), eclipse will simply overwrite your resource files once it decides to refresh your project.

This can be resolved by using Eclipse Maven integration (as opposed to eclipse:eclipse) which does correct filtering of resources "on-the-fly"

Or, of course, by stopping the eclipse while building, which is not really feasible as workflow model.