Search code examples
javaspringmaven

My .properties file isn't being picked up and my maven profile properties aren't injected


I am trying to inject specific details into my datasource.properties file by using maven profiles in my pom.xml

For some reason, it applies the details to other places where its needed such as the hibernate.cfg.xml but it doesn't apply it to my datasource.properties?

i have added the configuration for where the resources are but not sure what i am missing?

here is the relevant info from my pom.xml

.......
<profiles>
        <profile>
            <id>development</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>environment</name>
                    <value>dev</value>
                </property>
            </activation>
            <properties>
                 <db.username>some username</db.username>
                 <db.password>some password</db.password>
                 <db.url>some db url</db.url>
            </properties>
        </profile>
    </profiles>
    
    <properties>
        <db.username>some username</db.username>
        <db.password>some password</db.password>
        <db.url>some db url</db.url>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <warName>APP</warName>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp/META-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>META-INF</targetPath>
                        </resource>                     
                        <resource>
                            <directory>src/main/resources</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/hibernate.cfg.xml</include>
                                <include>**/datasource.properties</include>
                            </includes>
                        </resource>
                        <resource>
                            <directory>src/main/webapp</directory>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
.......

here is what the format is like in my datasource.properties file

datasource.driverClassName=some driver
datasource.url=${db.url}
datasource.username=${db.username}
datasource.password=${db.password}
datasource.initialPoolSize=10
datasource.minPoolSize=10
datasource.maxPoolSize=20
datasource.maxStatements=50


Solution

  • if you want to replace the placeholder in build time by maven, you have to use resource filtering:

    Add this to your <build> section in your pom.xml

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    

    which your code you do not replace the values and access environment variables at runtime