user697911
user697911

Reputation: 10531

Is it possible to specify multiple main class in pom file?

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?

Upvotes: 2

Views: 5655

Answers (2)

Kyle Zhang
Kyle Zhang

Reputation: 51

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

Upvotes: 1

kj007
kj007

Reputation: 6254

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>

Upvotes: 1

Related Questions