Spring Boot properties file externalization

840 views Asked by At

I currently have 3 properties files:
application.properties

spring.profiles.active=@activatedProperties@

application-develoment.properties

#DB properties:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/mydb
db.username=user
db.password=pswd

#Data source management:
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.current_session_context_class=thread

application-production.properties

#DB properties:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/myproddb
db.username=admin
db.password=admin

#Data source management:
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=false
hibernate.current_session_context_class=thread

I have my maven profiles set as follows in my pom.xml

<profiles>
    <profile>
        <id>development</id>
        <properties>
            <activatedProperties>development</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>production</id>
        <properties>
            <activatedProperties>production</activatedProperties>
        </properties>
    </profile>
</profiles>

and my DataSourceConfig's: annotaion @PropertySource(value = "classpath:application.properties")

Everything works fine when I compile with development or production profile, but now I want to externalize the properties files to the /conf/localhost directory of my Tomcat server, any idea on how to do that?

2

There are 2 answers

0
PaulNUK On

Use Spring Cloud Config server to give you a centralised, externalised configuration (and a lot more besides).

https://spring.io/guides/gs/centralized-configuration/

0
KSTN On

You can pass new configuration files

java -jar myproject.jar --spring.config.location=classpath:/new.properties,file:/home/override.properties

this will override configuration defined in configuration file inside the jar.