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?
The
copy-resourcesgoal ofmaven-resources-pluginshould be used to copy resources to a generic output folder (for example a "distribution" folder), not to thetargetfolder 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.htmlFor example:
In your POM you have bound
copy-resourcesto thevalidatephase that takes precedence over theprocess-resourcesphase used by default, so your<overwrite>switch has no effect because the copy fromconfig/${target.environment}happens always before the copy fromsrc/main/resources.If your
log4j2.xmlis already present inconfig/DEV,config/QAandconfig/PROD, you should simply remove it fromsrc/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: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