Home > Software design >  Springboot not reading Application.yml file when I set spring.active.profile
Springboot not reading Application.yml file when I set spring.active.profile

Time:06-26

I have application-dev.yml and application-prod.yml. I have Database props in it. When I set the spring.active.profile to dev and When I am doing a maven clean install, it's throwing me datasource.url not found error. (It's just not reading the yml files.) But when I create another application.yml file and put the same props, it's reading it and the maven clean install is a success.

How Should I make my application read props from respective env files? (I know how to set profiles in IntelliJ) Below is my POM.

I cannot put an include tag in POM because I want to read yml files according to profiles.

See the image to get an idea of what I am trying to do. You can see the maven tool as well that I am using.

enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.askvedicastrologers</groupId>
    <artifactId>ask-vedic-astrologers</artifactId>
    <version>1.0</version>
    <name>AskVedicAstrologers</name>
    <description>AskVedicAstrologers project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>
<!--    <packaging>jar</packaging>-->
<!--    <modules>-->
<!--        <module>../search-engine</module>-->
<!--        <module>../post-engine</module>-->
<!--    </modules>-->
    <dependencies>
<!--        &lt;!&ndash;  Search-Engine module dependency  &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>com.askvedicastrologers</groupId>-->
<!--            <artifactId>search-engine</artifactId>-->
<!--            <version>0.0.1-SNAPSHOT</version>-->
<!--        </dependency>-->

<!--        &lt;!&ndash;  Post-Engine module dependency  &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>com.askvedicastrologers</groupId>-->
<!--            <artifactId>post-engine</artifactId>-->
<!--            <version>0.0.1-SNAPSHOT</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>3.1.3</version>
        </dependency>
        <!--       Spring Security         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>8.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

Use spring.profiles.active=dev instead.

CodePudding user response:

If you have application-dev.yml and application-prod.yml, then you need to have one more application.yml. And on this application.yml file, you need to add

spring:
      active:
        profile: dev

For prod, config adds prod.

Reference

I think this should solve your issue!

  • Related