Currently in our application we have multiple main classes and executing them individually using below commands separately.
java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain1
java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain2
java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain3
Now trying to use spring boot. What do we do to achieve the same?
In pom.xml have
…….
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
……..
using spring boot and executing the command
java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain1
getting error as [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project MyApp:The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid
Spring Boot allows us to define the Main class in the configuration when we have multiple main classes declared in the application. As we are using a MAVEN build, we have to configure the POM. xml for Spring Boot to identify the main class of the application. Voila!
If Spring Boot project contains multiple main classes, Spring Boot will fail to start or packag for deployment.
In this example we are having two SpringBoot applications that we want to merge into one and for that we will create a new SpringBoot application for package them both and add the two old applications as maven dependencies. Then we will ask Spring to boot-up the application.
Spring Boot gives several ways:
java -cp app.jar -Dloader.main=com.company.MyAppMain1 org.springframework.boot.loader.PropertiesLauncher
pom.xml
<properties>
section:<properties>
<start-class>com.company.MyAppMain1</start-class>
</properties>
Note that this property will only be evaluated if you use spring-boot-starter-parent
as <parent>
in your pom.xml
.
spring-boot-maven-plugin
:<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Note: plugin configuration can be performed in Maven profile, so by activating different profiles, you'll run app with different main class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With