使用多个主类运行 spring boot [英] Running spring boot with multiple main classes

查看:48
本文介绍了使用多个主类运行 spring boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我们的应用程序中,我们有多个主类,并分别使用以下命令单独执行它们.

Currently in our application we have multiple main classes and executing them individually using below commands separately.

java -Xmx1024M -cp/path/to/jar/MyApp.jar com.....MyAppMain1

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain1

java -Xmx1024M -cp/path/to/jar/MyApp.jar com.....MyAppMain2

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain2

java -Xmx1024M -cp/path/to/jar/MyApp.jar com.....MyAppMain3

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain3

现在尝试使用弹簧靴.我们该怎么做才能达到同样的目标?

Now trying to use spring boot. What do we do to achieve the same?

在pom.xml中有

…….
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>

……..

使用spring boot并执行命令

using spring boot and executing the command

java -Xmx1024M -cp/path/to/jar/MyApp.jar com.....MyAppMain1

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain1

出现错误[错误] 无法在项目 MyApp 上执行目标 org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli):目标 org.codehaus.mojo:exec-maven- 的参数mainClass"plugin:1.6.0:java 缺失或无效

getting error as [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project MyApp:The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid

推荐答案

Spring Boot 给出了几种方式:

Spring Boot gives several ways:

  • 将主类指定为系统属性:
java -cp app.jar -Dloader.main=com.company.MyAppMain1 org.springframework.boot.loader.PropertiesLauncher

  • 在Maven pom.xml 部分配置主类:
    • configure main class in Maven pom.xml <properties> section:
    • <properties>
        <start-class>com.company.MyAppMain1</start-class>
      </properties>
      

      请注意,只有在 pom.xml 中使用 spring-boot-starter-parent 作为 时,才会评估此属性.代码>.

      Note that this property will only be evaluated if you use spring-boot-starter-parent as <parent> in your pom.xml.

      • spring-boot-maven-plugin配置主类:
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>             
            <configuration>    
              <mainClass>${start-class}</mainClass>
            </configuration>
          </plugin>
        </plugins>
      </build>
      

      注意:插件配置可以在Maven配置文件中进行,因此通过激活不同的配置文件,您将运行具有不同主类的应用程序.

      Note: plugin configuration can be performed in Maven profile, so by activating different profiles, you'll run app with different main class.

      这篇关于使用多个主类运行 spring boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆