Use a custom database.properties file only in production in a resthub bootstrapped maven java project running in Intellij

359 views Asked by At

I am using resthub to bootstrap my spring-backbone webapp.

https://github.com/resthub/resthub.github.io/blob/master/docs/spring/layout.md#environment-specific-properties

According to https://github.com/resthub/resthub.github.io/blob/master/docs/spring/mongo.md . They already have a default database properties file.

Things work fine so far, but I have to deploy the project on heroku, which has different dbname, port & other settings.

To override default db properties provided by resthub, a file named database.properties can be created in the classpath. But I want that file to be picked only for deployment on heroku.

How shall I proceed about configuring it such that it works locally on intellij with default db properties but takes up database.properties for heroku.

I have already read about Maven Profiles, but it talks about how to have different files for different environments and I'm looking for not to have a particular file for a particular environment, instead use the default one provided in one of the resthub jars.

Update

Just went through What is the order of precedence when there are multiple Spring's environment profiles as set by spring.profiles.active

resthub-mongodb profile is always olded, in case of production (identified by -Dprod=true), a new profile called heroku is loaded which in turns loads prod\database.properties

<context:property-placeholder location="classpath*:prod/database.properties"/>

But I guess the mongo related beans are already defined using properties defined in resthub-mongodb & hence, loading loading heroku profile to change the db properties doesn't work.

What is the solution in such a case ?

Possible Solution

For now, I ended up using one of the profile - heroku or resthub-mongodb depending on the system property.

Is there any other way ?

1

There are 1 answers

0
bmeurant On

You can let maven handling your environements with resource configuration.

Add in your pom or parent pom :

<env>local</env>

...

<resources>
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>${basedir}/src/main/config/${env}</directory>
        <filtering>false</filtering>
    </resource>
</resources>

This allows you to provide multiple configuration directories ; one for each your environment : local, heroku ...

With this conf, you will be able to provide a specific database.properties file for your heroku environment while keeping resthub defaults locally (not providing custom database.properties in config/local):

src/
   main/
       config/
             local/
             heroku/
                   database.properties

Adding -Denv=heroku option to any of your maven command run will add all config/ directory content in your classpath. local is the default environement and used if no option is given.