0

According to this (and also this):

The Spring Environment has an API for this, but you would normally set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE).

I am using version 2.7.3 of Spring Boot:

<dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.7.3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

We have a src/main/resources/application.properties file like this:

spring.datasource.driver-class-name=com.teradata.jdbc.TeraDriver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.TeradataDialect
#---
spring.config.activate.on-profile=local
spring.datasource.username=
spring.datasource.url=jdbc:teradata://TDTEST/TMODE=ANSI,CHARSET=UTF8
spring.datasource.password=
#---
spring.config.activate.on-profile=prod
spring.datasource.username=
spring.datasource.url=

We built a fatjar using the maven shade plugin. But when we try to run it like this:

SPRING_PROFILES_ACTIVE=local java -ea -jar target/td-api-1.0-SNAPSHOT.jar

We get this message:

17:09:42.663 [main] INFO cdao.dpde.app.App - No active profile set, falling back to 1 default profile: "default"

Why? How can I fix this?

morpheus
  • 18,676
  • 24
  • 96
  • 159

2 Answers2

0

Adjust your application.yml/properties to the following

spring:
  application:
    name: service-name

  profiles:
    active: ${PROFILE:local}

and run the following command

java -jar td-api-1.0-SNAPSHOT.jar --PROFILE=environement

the advantage of the above is you can default your profile to a fallback profile if the environment was not provided.

The command above takes into consideration that you are inside of the /target dir

Roland
  • 198
  • 1
  • 13
0

As best as I can remember the problem in our case was a misconfigured pom.xml due to which the .properties file was not making it to the fat jar. There was no issue with the .properties file or with Spring. The solution was to use the spring-boot-maven-plugin to package the application instead of the shade plugin. pom.xml looks like:

<build>
    <finalName>${project.artifactId}</finalName>
        <plugins>
        <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
        <version>3.0.2</version>        
        </plugin>       
        </plugins>
    </build>
morpheus
  • 18,676
  • 24
  • 96
  • 159