nickcodefresh
nickcodefresh

Reputation: 917

Multiple Spring Boot application classes with profiles

I have the requirement to be able to run my Spring Boot within a container environment, and as a standalone fat JAR. In the former mode, the application needs to register itself with Consul as this is used for service discovery, whereas in the later we need to disable this integration completely.

To accomplish this I created 2 Application classes: (I actually used an abstract parent here for the duplicate annotations, but I'm omitting this for brevity)

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties
@Profile("!standalone")
public class Application extends SpringApplication {

    ...

}

and

@SpringBootApplication(exclude = {
    ConsulAutoConfiguration.class, ConsulBusAutoConfiguration.class })
@EnableConfigurationProperties
@Profile("standalone")
public class ApplicationStandalone extends SpringApplication {

    ...

}

Notice the use of profiles and that the standalone version excludes the auto configuration classes for Consul.

When I try to build the JAR via Maven I get the following error:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.M5:repackage (default) on project mio-lmpp-service: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.M5:repackage failed: Unable to find a single main class from the following candidates [tv.nativ.mio.lmpp.ApplicationConfig, tv.nativ.mio.lmpp.ApplicationConfigStandalone] -> [Help 1]

What's the best way round this?

Thanks

Nick

Upvotes: 0

Views: 4131

Answers (1)

luboskrnac
luboskrnac

Reputation: 24591

You have two options

  1. Define main class during the build in spring-boot-maven-plugin. You can also parametrize it for the build.

  2. Define main class on in property file or command line. But you need to use PropertyLauncher instead of default org.springframework.boot.loader.JarLauncher.

Upvotes: 3

Related Questions