2

I am using Spring-boot to develop an application. The reason I need to specify multiple main classes is that, my program runs as a 'tool'. By starting with different main classes, I can finish tasks. I currently specify one main class in this way:

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>com.lu.qe.ClassificationService</mainClass> </configuration> </plugin> 

Then I can start my application by running at a terminal:

mvn spring-boot:run 

This only runs the "ClassificationService" main class. I also want to be able to possibly run another main class, like "ClassificationService_2". In such a case, how to achieve this? Is it possible to let 'mvn spring-boot:run' take a parameter?

2
  • you can define but i think at a time only one can run as there will be conflict in port etc when you will start other main class, but you can achieve it by creating two jars by changing their ports(server.port) and start them on same machine or you can define multiple connector configuration to start app on multiple port. Commented Sep 26, 2018 at 3:48
  • @kj007 I only need to run one at one time. It's not supposed to serve multiple people at the same time. I just want to have a convenient way to run one main class without changing and recompiling the application. So how to define multiple main classes in this case? Commented Sep 27, 2018 at 17:35

2 Answers 2

1

From command line argument main class can be pass but it will fail to run because multiple main class will be found..

mvn spring-boot:run -Dloader.main=DemoApplication 

But I think you can manage it by defining multiple profiles and then in command line you can pass profile argument..not checked but should work.

<profiles> <profile> <id>profile1</id> <properties> <spring.boot.mainclass>com.MainClass1</spring.boot.mainclass> </properties> </profile> <profile> <id>profile2</id> <properties> <spring.boot.mainclass>com.MainClass2</spring.boot.mainclass> </properties> </profile> </profiles> 
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't seem to work. I tried both "mvn spring-boot:run -Dloader.main=com.MainClass2" or "mvn spring-boot:run -Dloader.main=profile2", it always start my original main class. The second main class simply print out "hello world" and it didn't work.
This should be a typical need. In my spring project I have a main class. But I might have a bunch of "tool/utility" class that I need to run them from their main classes. In an IDE like Eclipse or IntelliJ, I can run any main class easily. But at a command line, how to be able to run any main class defined in my project?
1

I'm also developing a 'tool' that requires multiple main classes. springboot does not support this demand but I found maven parent pom feature exactly satisfy my requirement.

step 1 - to change a pom.xml to a parent pom, change the packaging type to 'pom', a parent pom looks like this:

<modelVersion>4.0.0</modelVersion>	<groupId>com.example</groupId>	<artifactId>test</artifactId>	<version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>

step 2 - create another pom file let's say service1.xml, and set this, pay attention to the relativePath element:

<parent>	<groupId>com.example</groupId>	<artifactId>test</artifactId>	<version>0.0.1-SNAPSHOT</version>	<relativePath>./pom.xml</relativePath>	</parent>	<modelVersion>4.0.0</modelVersion>	<groupId>com.example</groupId>	<artifactId>service1</artifactId>	<version>0.0.1-SNAPSHOT</version>

step3 - then you can specify the pom file when run a mvn command

mvn -f service1.xml spring-boot:run mvn -f service1.xml clean package

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.