Search code examples
javaspringgradleprofile

How to run gradle with profile? Duplicate key: spring


I have following content in application.yml:

spring:
  jpa:
    generate-ddl: false
    hibernate:
      ddl-auto: none
      dialect: org.hibernate.dialect.SQLServer2012Dialect
  profiles:
    active: @activeProfiles@
   ...
  spring:
    profiles: test
  datasource:
     url: jdbc:h2://localhost:1433;database=testdb
     username: sa
     password:

and I start application like this:

gradle bootRun -Pspring.profiles.active=test

I see following error:

:common-classes:compileJava UP-TO-DATE                                         
:common-classes:processResources UP-TO-DATE      
:common-classes:classes UP-TO-DATE      
:common-classes:jar UP-TO-DATE      
:compileJava UP-TO-DATE                                                        
:processResources UP-TO-DATE      
:classes UP-TO-DATE      
:findMainClass                 
:bootRun                                                                       
15:45:12.450 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSet
15:45:12.452 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSet
, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
15:45:12.453 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUr
15:45:12.642 [restartedMain] DEBUG org.springframework.boot.logging.ClasspathLog
15:45:12.644 [restartedMain] ERROR org.springframework.boot.SpringApplication -
org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode
 in 'reader', line 70, column 1:
    spring:
    ^
Duplicate key: spring
 in 'reader', line 82, column 14:
    password:
             ^

        at org.springframework.beans.factory.config.YamlProcessor$StrictMapAppen
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.const
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseCo
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(Base
        at org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructo
        at org.yaml.snakeyaml.Yaml$1.next(Yaml.java:471)
        at org.springframework.beans.factory.config.YamlProcessor.process(YamlPr
        at org.springframework.beans.factory.config.YamlProcessor.process(YamlPr
        at org.springframework.boot.env.YamlPropertySourceLoader$Processor.proce
        at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlProper
        at org.springframework.boot.env.PropertySourcesLoader.load(PropertySourc
        at org.springframework.boot.context.config.ConfigFileApplicationListener
        at org.springframework.boot.context.config.ConfigFileApplicationListener
        at org.springframework.boot.context.config.ConfigFileApplicationListener
        at org.springframework.boot.context.config.ConfigFileApplicationListener
        at org.springframework.boot.context.config.ConfigFileApplicationListener
        at org.springfk.boot.context.config.ConfigFileApplicationListener.onAppl
        at org.springframework.boot.context.config.ConfigFileApplicationListener
        at org.springframework.context.event.SimpleApplicationEventMulticaster.i
        at org.springframework.context.event.SimpleApplicationEventMulticaster.m
        at org.springframework.context.event.SimpleApplicationEventMulticaster.m
        at org.springframework.boot.context.event.EventPublishingRunListener.env
        at org.springframework.boot.SpringApplicationRunListeners.environmentPre
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringA
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
        at com.finvale.MarketplaceApplication.main(MarketplaceApplication.java:1
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.devtools.restart.RestartLauncher.run(Restart

What did I wrong?


Solution

  • You have declared spring twice in your configuration:

    spring:
      ...
      spring:
         profiles: test
    

    though it has to be declared only once as follows:

    spring:
       ...
       profiles: test
    

    Just remove spring: which goes before profiles: