Java 如何同时启动两个springboot项目

Java 如何同时启动两个springboot项目,java,spring,maven,spring-boot-maven-plugin,Java,Spring,Maven,Spring Boot Maven Plugin,我的项目包括一个视图服务和一个接口服务。项目目录如下所示: Myproject/ springboot1/ springboot2/ public static void main(String[] args) { new SpringApplicationBuilder().parent(WrapperApplication.class).web(WebApplicationType.NONE) .child(Application1.class).w

我的项目包括一个视图服务和一个接口服务。项目目录如下所示:

Myproject/
    springboot1/
    springboot2/
public static void main(String[] args) {
    new SpringApplicationBuilder().parent(WrapperApplication.class).web(WebApplicationType.NONE)
        .child(Application1.class).web(WebApplicationType.SERVLET)
        .sibling(Application2.class).web(WebApplicationType.SERVLET)
        .run(args);
}
springboot1和springboot2都可以单独运行。我现在启动它们的方式是打开两个终端并执行以下命令:

springboot1

cd Myproject/springboot1/
mvn spring-boot:run
springboot2

cd Myproject/springboot2/
mvn spring-boot:run
有没有办法同时启动两个项目? 例如,将一个全局pom.xml文件添加到Myproject目录,并直接在Myproject目录中执行mvn spring boot:run。试试这个


mvn springboot1/spring boot:run | mvn springboot2/spring boot:run

是的,正如您所提到的,需要类似全局pom xml的方式,而不是通过将两个项目作为模块组合到打包应用程序(Myproject)中来实现。让我告诉你一些想法:

  • 使用全局pom文件创建一个新的springBoot应用程序,并将所有公共(1&2项目)jar或依赖项移动到此全局pom文件。此外,作为主应用程序的每个应用程序都将其删除,因为它不再需要作为单独的实体,但也要确保在各自的主文件中是否有自定义代码,而不是将该代码移动到打包项目的主文件中

  • 将项目1和2构建为jar包,并将它们作为依赖项添加到打包项目中

  • Project springboot1内部pom将如下所示:

    ...
    <groupId>org.springboot1.module</groupId>
    <artifactId>springboot1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    ...
    
    ...
    <groupId>org.springboot2.module</groupId>
    <artifactId>springboot2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    ... 
    
    ...
    <dependency>
        <groupId>org.springboot1.module</groupId>
        <artifactId>springboot1</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springboot2.module</groupId>
        <artifactId>springboot2</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    ...
    
    }

  • 运行打包应用程序的主类,它将从两个相关项目开始


希望以上步骤能够根据您的项目结构进行更多的更改。另外,为了更清楚,您还可以参考

,您可以创建一个包装器Spring引导应用程序,将它们作为单独的servlet启动。它们应该位于兄弟应用程序上下文中,并将包装器应用程序作为父上下文共享。大致如下所示:

Myproject/
    springboot1/
    springboot2/
public static void main(String[] args) {
    new SpringApplicationBuilder().parent(WrapperApplication.class).web(WebApplicationType.NONE)
        .child(Application1.class).web(WebApplicationType.SERVLET)
        .sibling(Application2.class).web(WebApplicationType.SERVLET)
        .run(args);
}

如果使用IntelliJ,则可以使用runDashboard@Kartik谢谢你的回答,但我想摆脱IDE。情况是这样的。这两个项目由两组人开发。我希望在开发出一个版本后,它将在提交到我们的git服务器后自动构建。并部署这两个项目。我可以使用shell来实现它,但我想知道是否有一种更优雅的方法。在这样做时,我是否必须首先将springboot1和springboot2打包到JAR中?运行mvn spring boot:直接在项目目录中运行似乎不起作用,因此,您不需要单独构建它们,而是直接在主项目上运行
mvn clean install
,将它们打包到主项目类路径中的jar中,因为您已经在全局pom文件中将它们声明为依赖项(请查看我的回答)。非常感谢您的回答,但我仍然有一些问题。Maven提醒我它找不到org.springboot1.module,org.springboot2.module。我不知道是什么引起了这个问题。我把代码上传到github。你能帮我调试一下吗?在你开始调试之前,这里是我的建议。你看,名字
org.springboot1.module
只是我回答中供你参考的一个例子。所以,选择一些SpringBoot两个简单的应用程序,现在试着将它们耦合起来,然后看看哪里卡住了,然后逐个修复。正如我已经提到的,它不会是直截了当的,你必须四处奔波才能让它工作。祝你一切顺利!