简体   繁体   English

具有配置文件的多个 Spring Boot 应用程序类

[英]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.我需要能够在容器环境中运行我的 Spring Boot,并且作为一个独立的胖 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.在前一种模式下,应用程序需要向 Consul 注册自己,因为这是用于服务发现的,而在后一种模式中,我们需要完全禁用此集成。

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)为了实现这一点,我创建了 2 个应用程序类:(我实际上在这里使用了一个抽象父类来进行重复的注释,但为了简洁我省略了这个)

@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.请注意配置文件的使用,并且独立版本不包括 Consul 的自动配置类。

When I try to build the JAR via Maven I get the following error:当我尝试通过 Maven 构建 JAR 时,出现以下错误:

[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缺口

You have two options你有两个选择

  1. Define main class during the build in spring-boot-maven-plugin .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 .但是您需要使用PropertyLauncher而不是默认的org.springframework.boot.loader.JarLauncher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot多个配置文件DataSource - Spring Boot multiple profiles DataSource 为多个 DBMS 和多个配置文件配置 Spring 引导 - Configure Spring boot for multiple DBMS and multiple profiles Spring 引导中的配置文件特定 application.properties 文件和多个配置文件的默认值注入 - Profile specific application.properties file in Spring boot and default injection of values for multiple profiles 在 Spring Boot 中使用 spring 配置文件读取多个属性文件 - Read Multiple Properties File using spring profiles in Spring Boot Spring Boot配置配置文件 - Spring boot configuration profiles Spring Profiles和Spring Boot Profiles是一样的吗? - Are Spring Profiles and Spring Boot Profiles the same? 使用application.properties文件激活配置文件以进行弹簧启动测试 - Activate profiles for spring boot test using application.properties file Spring Boot - 配置文件(application.properties)为 HTTP 请求定义 baseUrl - Spring Boot - Profiles (application.properties) to define baseUrl for HTTP requests 根据系统属性在 Spring Boot 应用程序中设置配置文件 - Setting profiles in Spring Boot application based on system property 修改活动配置文件并在Spring Boot应用程序中刷新ApplicationContext运行时 - Modify active profiles and refresh ApplicationContext runtime in a Spring Boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM