I want to use maven for replacing some of my *.properties files in my war file.
Therefore I created folders dev, test and prod in my resources folder. Now I only want to have one of these folders used in the resources folder path in my war file when executing the corresponding profile. The result is that maven is copying all other folders as well so those are double in the classpath. Here is my configuration:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-dev-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- source -->
<directory>src/main/resources/dev</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
How do I configure Maven now that its only copying the contents of folder src/main/resources/dev to target/classes?
Thank you for you help
Maven by default copies all files in "src/main/resources" to the output folder, so in your current configuration it would probably create "dev", "test" and "prod" folders with their contents and then additionally copy the development resources without a "dev" prefix.
I would suggest to leave the default resources folder as it is and use it only for profile-independent resources. In your profile you can then configure additional resource folders:
<profile>
<id>dev</id>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources-dev</directory>
</resource>
</resources>
</build>
</profile>
It should also be possible to configure this in the common build section using properties defined in the profiles:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources-${projectStage}</directory>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<projectStage>dev</projectStage>
</properties>
</profile>
</profiles>
If you for some reason can't change your current directory structure then you would have to configure exclusions for the default resource folder to not copy the nested profile-dependant folders:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>dev/**</exclude>
<exclude>test/**</exclude>
<exclude>prod/**</exclude>
</excludes>
</resource>
</resources>
</build>
The "main" part in the maven directory structure exists for this purpose. Instead of creating directories inside the resource dir, you should create more dirs in your src directory. This is the default behavior, and it's already implemented by letting test resources override main resources during the test phases of the maven lifecycle.
Your structure should look like this:
src
main
resources
a.properties
b.properties
c.properties
test
resources
b.properties
c.properties
dev
resources
b.properties
With this structure, your "main" target will have the default property files, the test phases would temporarily overwrite b and c, and the dev profile will only overwrite b with yet another custom version, in the final deployable.
In order to achieve this, you'd have to configure the maven-resources-plugin
the way you already do in our question, for a dev profile.
And when you do, also consider: what version of property files should you use when running unit tests while building using the dev profile?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With