Reputation: 1662
I would like to configure a properties files depending on a profile, either ´dev´ or ´prod´, however the placeholders in the properties file are not replaced when executing the maven install goal.
Here is the pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>abc.myproject</groupId>
<artifactId>myartifact</artifactId>
<packaging>war</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>projectname</name>
<description>Site Description</description>
<url>http://www.myproject.abc</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<tomcat-deploy-path>output</tomcat-deploy-path>
</properties>
<dependencies>
// various dependencies
</dependencies>
<build>
<finalName>v${project.artifactId}#${project.version}</finalName>
<filters>
<filter>src/main/resources/profiles/${build.profile.id}/mongo.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<outputDirectory>${tomcat-deploy-path}</outputDirectory>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
</profiles>
</project>
The placeholder mongo.properties file:
database.name=${database.name}
database.port=${database.port}
database.host=${database.host}
database.username=${database.username}
database.password=${database.password}
The profiles/dev/mongo.properties file:
database.name=mydatabase
database.port=27017
database.host=localhost
database.username=root
database.password=psw1
The directory structure is as follows:
> myproject
pom.xml
> src
> main
> java
> resource
> profiles
mongo.properties
> dev
mongo.properties
> prod
mongo.properties
> webapp
> test
I followed the instructions in this blog article: http://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/
Upvotes: 3
Views: 5152
Reputation: 11
Try using @..@
instead of ${}
, for example
[email protected]@
For more info refer to: https://docs.spring.io/spring-boot/docs/1.4.x/reference/html/howto-properties-and-configuration.html
Upvotes: 0
Reputation: 137229
I just tested your setup and it worked correctly: the file WEB-INF/classes/mongo.properties
was correctly replaced.
The problem you are having revolves around the fact that you are outputting the final war
inside a folder that is not under target
. This is a very bad idea. Currently, the final WAR is generated inside the directory output
, which corresponds to the property tomcat-deploy-path
in your POM.
<tomcat-deploy-path>output</tomcat-deploy-path>
The big problem with such a setup is that running mvn clean
will not delete that directory and subsequent builds won't override it, meaning the WAR will never be updated and you will keep an old obsolete WAR. I strongly suggest that you move this property under the Maven build directory, something like
<tomcat-deploy-path>${project.build.directory}/output</tomcat-deploy-path>
so that it is properly cleaned when running mvn clean
.
As such, to resolve your problem, you should manually delete the folder output
and rerun mvn clean install
. It should generate a proper and new WAR.
Upvotes: 1