6
votes

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

1
Did you try mvn clean install ? looks like error the packing, please try it and let us know.; - Jonathan JOhx
yes, tried it. Doing maven clean install didn't help. Looking close at the parent pom it is using exec-maven-plugin and is expecting the start class mainClass>${start-class}</mainClass>. I am not sure how to pass this start class for different programs we have. - opai

1 Answers

7
votes

Spring Boot gives several ways:

  • specify main class as system property:
java -cp app.jar -Dloader.main=com.company.MyAppMain1 org.springframework.boot.loader.PropertiesLauncher
  • configure main class in Maven 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.

  • configure main class for 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.