0

I have application.properties with that code:

spring.config.additional-location=file:///C:/Users/user/Desktop/project/cfg.properties
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true

and in cfg.properties it looks like that:

spring.datasource.url=jdbc:oracle:thin:@correctDbUrl
spring.datasource.username=user
spring.datasource.password=pass

I think there is something wrong with the path - can't it be outside the project or what happened? This is an error (url is correct, it works when placed directly in application.properties):

 Description:

 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

 Reason: Failed to determine suitable jdbc url

Gradle:

 buildscript {
ext {
    springBootVersion = '2.0.3.RELEASE'
}
repositories {
    maven { url "someRepository" }
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.abc'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
maven { url "someRepository" }
mavenCentral()
}


dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile(group: 'com.hynnet', name: 'oracle-driver-ojdbc6', version: '12.1.0.1')
compile("org.springframework.boot:spring-boot-starter-data-jpa")
}
Helosze
  • 333
  • 2
  • 7
  • 20

1 Answers1

2

Set the following environment variable.

SET SPRING_CONFIG_LOCATION=classpath:/application.properties,file:C:/Users/user/Desktop/project/cfg.properties

you can remove "spring.config.additional-location" from the property file.

Sahal
  • 278
  • 2
  • 10
  • It works, thank you. But shouldn't that solution override the application.properties instead of adding properties to it from another files? And one more question - what if I have more projects with other configurations? am I forced to change this environment variable every time? – Helosze Jul 17 '18 at 10:57
  • 1
    If you are testing your code in eclipse. You can set environment in the eclipse runtime configuration for that particular application. Or you can start the application by giving --spring.config.location parameter on application startup. you can refer this link https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – Sahal Jul 17 '18 at 11:01
  • Ok, thanks. ;) But I still doesn't know why the additional location doesn't work and why spring.config.location doesn't override application.properties. ;) – Helosze Jul 17 '18 at 11:05
  • @Helosze spring.config.location has overridden the default behavior. Since we have provided "classpath:/application.properties" again in the environment variable. It's able to get those values. – Sahal Jul 17 '18 at 11:12
  • And do you know any solution to avoid using SPRING_CONFIG_LOCATION? For example, if I want to create my own environment variable like PROJECT_CFG and set it's value (path) to cfg.properties and add it to application.properties? It's simply external file config, but I can't find anything that works. – Helosze Jul 17 '18 at 11:41
  • 2
    You can use @PropertySource("file:${external.config.location}") in the application configuration class. Then give external.config.location=C:/Users/user/Desktop/project/cfg.properties in your application.properties – Sahal Jul 17 '18 at 11:53