Reputation: 107
i created two maven profile cause i want to deploy my app to heroku, so i have one profile dev with db properties that located on my PC, and prod with properties for heroku db. POM.xml below
<modelVersion>4.0.0</modelVersion>
<groupId>com.phone-book</groupId>
<artifactId>phone-book</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>controller</module>
<module>dao</module>
<module>model</module>
<module>service</module>
</modules>
<name>Phonebook web app</name>
<build>
<filters>
<filter>profiles/${build.profile.id}/config.properties </filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>controller/src/main/webapp/WEB-INF/spring</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>8.0.30.2</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
</profiles>
i created folder in each maven module profile that contains folders dev and prod and wrote my prop like in this tutorials https://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/ Finaly create spring context with this param, see below
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${url.property}" />
<property name="username" value="${user.property}" />
<property name="password" value="${password.property}" />
<property name="initialSize" value="20" />
<property name="maxActive" value="100"/>
</bean>
but when i make my app, properties does not replace and i get something like that
Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL '${url.property}'
Upvotes: 6
Views: 4516
Reputation: 509
I don't see where you load the concrete properties file in your application context.
I think that you need something similar in your app context:
<context:property-placeholder location="classpath:profiles/${build.profile.id}/config.properties" />
The filtering for the controller module should be in its pom file so that it is applied during its build lifecycle. That way the filtering is applied when the root is build and not when the controller module is build.
The modules should have the root pom set as their parent so that they can inherit the profiles and the properties.
Upvotes: 4
Reputation: 11017
Seems your profile properties are inconsistent along with where you copy the properties to
prod : profile should be
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
copy location properties to a location in classpath, it should be something usually like(unless you have specified a different location in your maven)
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
and you should be having properties files such as,
config.properties
user.property=xxxxx
..
in prod/dev folders
and when you run, you should clearly invoke your mvn profiles something like this
mvn -P dev clean install
or
mvn -P prod clean install
and as @vbhlev suggested you need to have this in beans xml
<context:property-placeholder location="classpath:/config.properties" />
Upvotes: 2