Following are the different way to run Spring Boot main class:
1. Single main class in the Spring Boot Application
A. via <properties> section
<properties> <start-class>com.company.MyAppMain1</start-class> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Note:
- When we use
start-class property in properties section of pom.xml, we don't need to pass explicit value to plugin spring-boot-maven-plugin
B. via Custom property <custom-mainClass>
<properties> <custom-mainClass>com.company.MyAppMain1</custom-mainClass> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${custom-mainClass}</mainClass> </configuration> </plugin> </plugins> </build>
Note:
Custom property custom-mainClass can be hard coded inside the property file as mentioned above 1.B
Pass by maven property using build as mentioned below:
mvn -U clean install -Dcustom-mainClass=com.company.MyAppMain1
C. via Hard coded value in plugin spring-boot-maven-plugin section
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.company.MyAppMain1</mainClass> </configuration> </plugin> </plugins> </build>
2. Multiple main class in the Spring Boot Application
A. via org.springframework.boot.loader.PropertiesLauncher
Step 1:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass> org.springframework.boot.loader.PropertiesLauncher </mainClass> </configuration> </plugin> </plugins> </build>
Step 2: Use below command to start application
java -Dloader.main=com.company.MyAppMain1 -jar myapp.jar
OR
java -cp myapp.jar -Dloader.main=com.company.MyAppMain1 org.springframework.boot.loader.PropertiesLauncher
Note:
- Use
-Dloader.main as Java VM arguments to pass dynamic main-class during start of application as shown in step 2 org.springframework.boot.loader.PropertiesLauncher has moved to org.springframework.boot.loader.launch.PropertiesLauncher
B. via Custom property <custom-mainClass>
<properties> <custom-mainClass>com.company.MyAppMain1</custom-mainClass> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${custom-mainClass}</mainClass> </configuration> </plugin> </plugins> </build>
Note:
Pass by maven property using build as mentioned below:
mvn -U clean install -Dcustom-mainClass=com.company.MyAppMain1
This also a dynamic main-class but its good only during maven build. Not applicable during application startup as shown in step 2 of section 2.A
mvn clean install? looks like error the packing, please try it and let us know.;