spring-boot-maven-plugin spring-boot.run.main-class is ignored (Groovy)

Viewed 1528

I have a spring-boot application with 2 main class as follows.

package org.arunm.springwebfluxrestservice.client


import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder

@SpringBootApplication
class ReactiveClientApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ReactiveClientApplication)
            .properties(Collections.singletonMap("server.port","8081"))
            .run(args)
    }
}

and

package org.arunm.springwebfluxrestservice.webservice


import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
@RestController
class ReactiveServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReactiveServiceApplication)
    }
}

spring-boot-maven-plugin is already included in pom.xml

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

I am trying to start ReactiveClientApplication using

 mvn spring-boot:run -Dspring-boot.run.main-class=org.arunm.springwebfluxrestservice.client.ReactiveClientApplication

I get the error

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.297 s
[INFO] Finished at: 2020-05-14T16:27:38+10:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.7.RELEASE:run (default-cli) on project spring-webflux-rest-service: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.2.7.RELEASE:run failed: Unable to find a single main class from the following candidates [org.arunm.springwebfluxrestservice.webservice.ReactiveServiceApplication, org.arunm.springwebfluxrestservice.client.ReactiveClientApplication] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.7.RELEASE:run (default-cli) on project spring-webflux-rest-service: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.2.7.RELEASE:run failed: Unable to find a single main class from the following candidates [org.arunm.springwebfluxrestservice.webservice.ReactiveServiceApplication, org.arunm.springwebfluxrestservice.client.ReactiveClientApplication]
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.2.7.RELEASE:run failed: Unable to find a single main class from the following candidates [org.arunm.springwebfluxrestservice.webservice.ReactiveServiceApplication, org.arunm.springwebfluxrestservice.client.ReactiveClientApplication]
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:148)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)

What am I doing wrong here ?

1 Answers

Based on the plugin documentation documentation for mainClass parameter it is not specified whether it can be run from command line. For other parameters it has been mentioned specifically.

So one could use maven profile with main class configuration.

<profiles>
    <profile>
        <id>client</id>
        <properties>
        <spring.boot.mainclass>org.arunm.springwebfluxrestservice.client.ReactiveClientApplication</spring.boot.mainclass>
        </properties>
    </profile>
    <profile>
        <id>service</id>
        <properties>
          <spring.boot.mainclass>org.arunm.springwebfluxrestservice.webservice.ReactiveServiceApplication</spring.boot.mainclass>
        </properties>
    </profile>
</profiles>

Spring Boot Maven Plugin

 <configuration>
          <mainClass>${spring.boot.mainclass}</mainClass>
 </configuration>

Console

mvn spring-boot:run -P client

Although this also works for me but it could be that maven is doing property replacement for the configuration parameter but I'm not 100% sure

mvn spring-boot:run -Dspring.boot.mainclass=org.arunm.springwebfluxrestservice.client.ReactiveClientApplication

References

SpringBoot: Unable to find a single main class from the following candidates

https://www.baeldung.com/spring-boot-main-class

Related