Home > Net >  Externalising properties file not working in Spring Boot App
Externalising properties file not working in Spring Boot App

Time:08-03

I have Spring Boot App with version 2.3.12.RELEASE in Gradle with Liquibase changesets.

This code is multi module app with about four modules in hexagonal architecture.

enter image description here

and the resources folder structure looks like this for doctors-data-deploy module:

enter image description here

I have removed properties file from above resources folder and tried externalising them:

enter image description here

My Issue is I want to put configs files outside of the code, how can I externalise these files from code.

I tried below stuffs for this, tried below configuration in intellij configuration while running the app

1. -Dspring.profiles.active=local -Dserver.port=8080 -Dajp.port=0 -Dspring.datasource.url=jdbc:oracle:thin:@localhost:1521:OraDoc -Dspring.datasource.username=system -Dspring.datasource.password=MyPasswd123 -Dspring.config.location=classpath:file:/Users/abc/codebaseDownloadedOne/configs/application.yml,classpath:file:/Users/abc/codebaseDownloadedOne/configs/application-local.yml

2. -Dspring.profiles.active=local -Dserver.port=8080 -Dajp.port=0 -Dspring.datasource.url=jdbc:oracle:thin:@localhost:1521:OraDoc -Dspring.datasource.username=system -Dspring.datasource.password=MyPasswd123 -Dspring.config.location=classpath:/Users/abc/codebaseDownloadedOne/configs/application.yml,classpath:/Users/abc/codebaseDownloadedOne/configs/application-local.yml

3. -Dspring.profiles.active=local -Dserver.port=8080 -Dajp.port=0 -Dspring.datasource.url=jdbc:oracle:thin:@localhost:1521:OraDoc -Dspring.datasource.username=system -Dspring.datasource.password=MyPasswd123 -Dspring.config.location=classpath:/Users/abc/codebaseDownloadedOne/configs/ -Dspring.config.name=application.yml,application-local.yml

4. -Dspring.profiles.active=local -Dserver.port=8080 -Dajp.port=0 -Dspring.datasource.url=jdbc:oracle:thin:@localhost:1521:OraDoc -Dspring.datasource.username=system -Dspring.datasource.password=MyPasswd123 -Dspring.config.additional-locaton=file:/Users/abc/codebaseDownloadedOne/configs/

None of the above 4 approaches worked, can someone please let me know what's wrong with my configuaration?.

Below is my application yml file

server:
  tomcat:
    connectionTimeout: 300000
management:
  endpoint:
    mappings:
      enabled: true
spring:
  liquibase:
    change-log: classpath:/db/changelog/db.changelog-master.xml
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    hikari:
      minimum-idle: 5
      maximumPoolSize: 20
      idleTimeout: 30000
      maxLifetime: 2000000
      connectionTimeout: 30000
      poolName: data-pool
  jpa:
    hibernate:
      use-new-id-generator-mappings: false
      ddl-auto: none
    database-platform: org.hibernate.dialect.Oracle12cDialect
  quartz:
    job-store-type: jdbc
    jdbc:
      initialize-schema: never
    properties:
      org:
        quartz:
          scheduler:
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
            useProperties: false
            misfireThreshold: 60000
            clusterCheckinInterval: 5000
            isClustered: true
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true

and error I am getting is this : basically it tried to look for master.xml which it does not getting.

Caused by: liquibase.exception.ChangeLogParseException: classpath:/db/changelog/db.changelog-master.yaml does not exist
    at liquibase.parser.core.yaml.YamlChangeLogParser.parse(YamlChangeLogParser.java:27)
    at liquibase.Liquibase.getDatabaseChangeLog(Liquibase.java:377)

File /db/changelog/db.changelog-master.yaml is existing in folder called /db/changelog/ but still it shows that it does not exist means spring boot is not able to find the application.yml when I put back application.yml in the resources folder then it works but I wanted to externalise this.

Can someone please help on this ?

CodePudding user response:

How Spring Boot handles externalized configuration files is documented here: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files

Possible problems with your config used:

  • ad 1) mixed classpath: and file:
  • ad 2) i guess /Users/abc/... is not on your classpath
  • ad 3) same as 2)

CodePudding user response:

Correct answer is this : It was only typo issue in my config, corrected it.

-Dspring.config.additional-location=file:/Users/abc/codebaseDownloadedOne/configs

  • Related