I have to create test war and production war, which will have a different log4j.properties
file in the WEB-INF
directory. I have these files
The code above didn't work for me - had to change it to the following:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-prod-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- this is important -->
<overwrite>true</overwrite>
<!-- this as well (target/ was missing) -->
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/prod</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Create a "dev" and "prod" profile, selecting an alternate set of resources for each profile. Make Dev active by default.
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources/dev</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<resources>
<resource>
<directory>src/main/resources/prod</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
Build using the desired profile via:
mvn install -Pdev
or
mvn install -Pprod
The alternative way is to use maven-antrun-plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>build.env: ${build.env} </echo>
<delete file="src/main/resources/log4j.properties" />
<copy file="src/env/${build.env}/log4j.properties"
tofile="src/main/resources/log4j.properties" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Assume resource files are in following structure:
src/
env/
dev/
log4j.properties
local/
log4j.properties
prod/
log4j.properties
When do maven build, run the following commands per environment:
mvn package -Dbuild.env=dev
mvn package -Dbuild.env=local
mvn package -Dbuild.env=prod
Last response is working. But you need to give the version to make it work.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-prod-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- this is important -->
<overwrite>true</overwrite>
<!-- target -->
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- source -->
<directory>src/main/resources/prod</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I solved this using the maven-resources plugin, where i created the prod directory which has the resources for production environment and copied them to WEB-INF/classes directory in process-resources phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-prod-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>webapp/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/prod</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>