Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How insert Maven profile properties in Spring context

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}'

like image 707
Jungle Avatar asked Aug 11 '16 09:08

Jungle


1 Answers

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.

like image 105
vbuhlev Avatar answered Oct 19 '22 09:10

vbuhlev