Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'spring.profiles.active' imported from location 'class path resource [application-dev.yml]' is invalid

I updated Spring cloud application to the latest Spring boot version 2.5.0.

But during startup I get this exception:

11:05:05.038 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property 'spring.profiles.active' imported from location 'class path resource [application-dev.yml]' is invalid in a profile specific resource [origin: class path resource [application-dev.yml] from skyshop-mail-1.0.jar - 42:17]
        at org.springframework.boot.context.config.InvalidConfigDataPropertyException.lambda$throwOrWarn$1(InvalidConfigDataPropertyException.java:125)

application.yml

spring:
    application:
        name: mail-service
    profiles:
        active: dev

application-dev.yml file:

logging:
    file:
        name: ${java.io.tmpdir}/application.log
    level:
        com:
            backend: DEBUG
        org:
            springframework: DEBUG
            springframework.web: DEBUG
jwt:
    expiration: 86400
    secret: test112322
server:
    port: 8020
    servlet:
        context-path: /mail
spring:
    application:
        name: mail-service
    profiles:
        active: local 
    data:
        web:
            pageable:
                one-indexed-parameters: true # Fix pagination starting number to start from 1
        rest:
            basePath: /mail
    jackson:
        default-property-inclusion: non_null
    jmx:
        enabled: false   
    datasource:
        url: jdbc:mariadb://localhost:3306/database
        driverClassName: org.mariadb.jdbc.Driver
        jpa:
            hibernate:
                ddl-auto: update
            properties:
                hibernate:
                    dialect: org.hibernate.dialect.MariaDBDialect
            show-sql: true
        username: root
        password: qwerty
    oauth2:
        resource:
            jwt:
                key-pair:
                    alias: mytestkey
                    store-password: mystorepass
info:
    build:
        version: 1.0
eureka:
    client:
        serviceUrl:
            defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
    instance:
        preferIpAddress: true

Do you know how I can fix this issue?

like image 204
Peter Penzov Avatar asked Jun 11 '21 11:06

Peter Penzov


People also ask

How do I read properties from YAML file in spring boot?

In a Spring Boot application, we can use properties files, YAML files, environment variables, and command-line arguments to externalize our configuration. This is useful while working with the same application code in different environments. This post will discuss how to read values defined in the application.

How do you specify file path in application properties in spring boot?

Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.

How to get the active profile of a class in spring?

Alternatively, we could access the profiles by injecting the property spring.profiles.active: Here, our activeProfile variable will contain the name of the profile that is currently active, and if there are several, it'll contain their names separated by a comma. However, we should consider what would happen if there is no active profile at all.

Do I need to mention the active property in Spring Boot?

Show activity on this post. No need to mention spring.profiles.active property if file name is application-dev.yml ( spring boot new version ) Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid …

Can you activate a profile from a document in Spring Boot?

Prior to Spring Boot 2.4, it was possible to activate a profile from profile-specific documents. But that is no longer the case; with later versions, the framework will throw – again – an InvalidConfigDataPropertyException or an InactiveConfigDataAccessException in these circumstances.

What are profiles in Spring Framework?

In this tutorial, we'll focus on introducing Profiles in Spring. Profiles are a core feature of the framework — allowing us to map our beans to different profiles — for example, dev, test, and prod. We can then activate different profiles in different environments to bootstrap only the beans we need.


Video Answer


5 Answers

Spring Boot 2.4 has improved the way that application.properties and application.yml files are processed.

See here for details: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Config-Data-Migration-Guide

Long story short: If you have for example an application-local.yml and inside you defined

spring:
profiles:
    active: local 

then just remove this entry in the yaml file.

like image 63
René Winkler Avatar answered Oct 22 '22 12:10

René Winkler


The newer Spring Boot Version is very strict with naming convention based on environments. You cannot inject application properties during runtime. You will need to create application-{env}.properties for each environments, which will be picked based on active profile.

But if you don’t feel comfortable doing all these changes, you can still use older processor. Just add below as JVM arugments while running Spring Boot. Make sure to add it in Dockerfile Entrypoint as well, if you are using Docker.

-Dspring.config.use-legacy-processing=true

Check documentation here

like image 31
Abu Ehteshamuddin Avatar answered Oct 22 '22 14:10

Abu Ehteshamuddin


In your application-dev.yml, you declare :

spring:
    application:
        name: mail-service
    profiles:
        active: local 

2 solutions :

  1. rename application-dev.yml to application-local.yml and use local profile
  2. change spring.profiles.active to dev in application-dev.yml
like image 6
user3088799 Avatar answered Oct 22 '22 14:10

user3088799


No need to mention spring.profiles.active property if file name is application-dev.yml ( spring boot new version )

like image 13
Debashish Avatar answered Oct 22 '22 12:10

Debashish


Since version 2.4 (Spring Boot 2.4):

Profiles can no longer be activated from profile specific documents.

https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

One way forward could be to use spring.profiles.group.*

  1. From application-dev.yml remove: profiles: active: local

  2. rename application-dev.yml -> application-dev123.yml

  3. In application.properties define group "dev": spring.profiles.group.dev=local,dev123

Group named "dev" now is replacement for previous profile named "dev".

like image 7
IndustryUser1942 Avatar answered Oct 22 '22 12:10

IndustryUser1942