Maven resource filtering and build profiles
December 06, 2011 19:52:15 Last update: December 06, 2011 19:52:15
Resource files under the
When filtering is turned on, constructs like
The build command "
the contents of
Sometimes you want different resource definitions for different environments, e.g., dev vs. prod. You can achieve that by defining profiles in
In the above,
You can check active profiles with:
src/main/resources directory are copied verbatim to the target/classes directory during build. But resources can be filtered by turning on filtering in pom.xml:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
When filtering is turned on, constructs like
${...} are replaced with actual values if they are defined. For example, create a file test.properties:
project.stage=${project.stage}
The build command "
mvn package" simply copies test.properties to target/classes/. But if you build with:
mvn -Dproject.stage=dev package
the contents of
target/classes/test.properties becomes:
project.stage=dev
Sometimes you want different resource definitions for different environments, e.g., dev vs. prod. You can achieve that by defining profiles in
pom.xml:
<profiles> <profile> <id>dev</id> <properties> <project.stage>Development</project.stage> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <project.stage>Production</project.stage> </properties> </profile> </profiles>
In the above,
dev is the default profile, prod is defined but not active unless you specifically activate it. With the above profile:
- the command "
mvn package" changes the contents oftarget/classes/test.propertiesto:project.stage=Development
- if you activate the
prodprofile with "mvn -P prod package", the contents oftarget/classes/test.propertiesbecomes:project.stage=Production
- system properties overrides values defined in profiles. The command "
mvn -Dproject.stage=dev package" createstarget/classes/test.propertieswith contents:project.stage=dev
- You can active both
devandprodprofiles with:mvn -P dev,prod package
but what will the output be?
You can check active profiles with:
mvn help:active-profiles