Spring Profile Active is not setting

39 Views Asked by At

I am trying to run the below command to start a springboot application

java -Dlog4j.configuration=file:///log4j.properties -Dspring.profiles.active=local -cp ./target/app-1.0.0-SNAPSHOT-jar-with-dependencies.jar MainClass

the jar above is prepared using maven-assembly-plugin the configuration for which is

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>${maven-assembly-plugin.version}</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Main class looks like

@SpringBootApplication
@Slf4j
public class MainClass {
    public static void main(String... args) {
        log.info(System.getProperty("spring.profiles.active"));
        ApplicationContext ctx = SpringApplication.run(AcctrvDataProcessor.class, args);
        log.info(Arrays.toString(ctx.getEnvironment().getActiveProfiles()));
        int exitCode = SpringApplication.exit(ctx);
        System.exit(exitCode);
    }
}

When I run the application from the above command I am seeing the below log:

15:45:07.091 [main] INFO MainClass - local
15:46:07.091 [main] INFO MainClass - No active profile set, falling back to 1 default profile: "default"

What could be preventing the spring profile actives from being set?

1

There are 1 best solutions below

3
M. Rizzo On

Why are you calling the main class? This is a spring boot application, so try starting by just invoking the jar.

java -jar -Dlog4j.configuration=file:///log4j.properties -Dspring.profiles.active=local app-1.0.0-SNAPSHOT-jar-with-dependencies.jar

And note it is important to have the -D parameters before the application jar or they will not be recognized.

If you have a hard requirement to use the -cp (classpath) amd not call the jar directly then you can set an environment variable to set the active profile in your shell and then trying starting the spring app that way.

export SPRING_PROFILES_ACTIVE=local