Spring Boot allows developers to easily configure and run their applications through the command line. One feature that can be particularly useful is the ability to assign an active profile from the command line. This can allow you to run your application with different configurations depending on the environment, such as development or production.



Methods of assign an active profile from command line

Here’s how you can assign an active profile from the command line in Spring Boot:

  1. First, ensure that you have the necessary profiles set up in your application.properties or application.yml file. You can create multiple profiles by adding the “spring.profiles” property and specifying the names of the profiles you want to use. For example:
spring.profiles.active=dev
  1. To assign an active profile from the command line, you can use the “spring.profiles.active” command line argument. For example, if you want to run your application with the “production” profile, you can use the following command:
java -jar my-application.jar --spring.profiles.active=production
  1. you can also use environment variable
export SPRING_PROFILES_ACTIVE=production
  1. You can also use -D option
java -Dspring.profiles.active=production -jar my-application.jar
  1. Another way to assign an active profile from the command line is to use the Spring Boot Maven Plugin. You can configure the plugin in your pom.xml file to specify the active profile when building and running your application. For example:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <profiles>
                    <profile>production</profile>
                </profiles>
            </configuration>
        </plugin>
    </plugins>
</build>

In this example, the “production” profile will be active when you run the command “mvn spring-boot:run”

  1. You can also use the Spring Application class to assign an active profile programmatically. For example:
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.setAdditionalProfiles("production");
        app.run(args);
    }
}

By using this approach, you can dynamically set the active profile based on certain conditions or user input.



Assigning multiple active profiles

It’s worth noting that you can also assign multiple active profiles at the same time by comma separating the profiles names in the command line argument or in the configuration file. For example:

java -jar my-application.jar --spring.profiles.active=dev,production


Conclusion

In conclusion, Spring Boot provides several ways to assign an active profile from the command line, including using command line arguments, environment variables, the Spring Boot Maven Plugin, and programmatically through the Spring Application class. These options allow you to easily switch between different configurations and environments, giving you more control over how your application runs.



Leave a Reply