Search code examples
mavenmaven-resources-plugin

maven-resource-plugin overwrite=true ignored - newer file is kept


I want to overwrite the log4j2.xml in src/main/resources for this i put 3 files in config/PROD, config/DEV, config/QA

and added this copy-resources element:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <!-- in validate phase copy the folder in config folder with name given by variable into classes-->
            
            <executions>
                <execution>
                  <id>copy-resources</id>
                  <phase>validate</phase>
                  <goals>
                    <goal>copy-resources</goal>
                  </goals>
                  <configuration>
                    <overwrite>true</overwrite>
                    <outputDirectory>${basedir}/target/classes</outputDirectory>
                    <resources>          
                      <resource>
                        <directory>config/${target.environment}</directory>
                      </resource>
                    </resources>              
                  </configuration>          
                </execution>
              </executions>
        </plugin>

But this only works if log4j2.xml has an older "Changed" date than the files on config. Even adding the overwrite in global config instead of per execution it does not help.

Only changing the changed timestamp of files is helping. Is there a way to make this work?


Solution

  • The copy-resources goal of maven-resources-plugin should be used to copy resources to a generic output folder (for example a "distribution" folder), not to the target folder that is the default destination of the default goal.

    If you have a default resource directory and a per-environment directory, just specify them in the <build><resources> section as explained in https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

    For example:

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
            <resource>
                <directory>config/${target.environment}</directory>
            </resource>
        </resources>
    
        ....
    
    </build>
    

    In your POM you have bound copy-resources to the validate phase that takes precedence over the process-resources phase used by default, so your <overwrite> switch has no effect because the copy from config/${target.environment} happens always before the copy from src/main/resources.

    If your log4j2.xml is already present in config/DEV, config/QA and config/PROD, you should simply remove it from src/main/resources; if you want to keep it for any other environment you have to override the default configuration of the plugin (inherited from the Super POM) in this way:

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>process-resources</phase>
                    <configuration>
                        <overwrite>true</overwrite>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    

    Note also that ${basedir} has been deprecated in favor of ${project.basedir}, see https://maven.apache.org/ref/3.9.4/maven-model-builder/#model-interpolation