Maven Spring Boot插件:如何从另一个项目运行spring boot

https://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html

我有一个项目,有2个模块。

[Parent] |-pom.xml | [SpringBoot2App] | |-pom.xml | [test] | |-pom.xml (start goal here!) 

我想在一个单独的项目中运行集成测试(maven failsafe插件),这是另一个模块。

在父模块的集成测试期间,是否可以配置spring boot maven插件来启动/停止子模块?

我尝试过这样的事情没有成功

   org.springframework.boot spring-boot-maven-plugin  com.SimpleServiceApplication ../SpringBoot2App/target/classes  ../SpringBoot2App/target/test-classes      start  pre-integration-test    

哪个不起作用。

我还尝试在读取插件超级类的源代码后添加“project”参数https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot -tools /弹簧引导Maven的插件/ src目录/主/ JAVA /组织/ springframework的的/ boot /行家/ AbstractRunMojo.java

   com.SimpleServiceApplication ${project.parent.collectedProjects[0]}  

这指的是正确的项目,如调试所示,但也不起作用。

请不要评论[0],我知道[0]不干净,是一个需要直接了解父pom模块排序的耦合。

我在org / springframework / boot / SpringApplication上得到一个java.lang.NoClassDefFoundError

我将starter-web项目添加到测试pom.xml,结果相同

我不认为使用spring-boot-maven-plugin对另一个模块运行集成测试是不可能的,因为start目标似乎没有为您提供从本地存储库或Maven反应器解析应用程序的方法。可能就是你想要的。 您尝试过的project配置属性并非旨在以这种方式覆盖。 应仅使用插件目标文档中列出的属性配置插件执行。

相反,我认为你至少有两种可能的方法:

  1. 使用不同的插件来控制服务器; 要么
  2. 直接从代码中的测试运行服务器。

选项1

为此,我认为你需要一种方法来复制你想要运行的服务器工件,以及一些更通用的启动和停止它,比如cargo-maven2-plugin或process-exec-maven-plugin 。

只需在服务器工件构建中配置repackage目标:

  org.springframework.boot spring-boot-maven-plugin  true     repackage     

然后从集成测试模块中,您可以执行以下操作:

  maven-dependency-plugin   copy-server-artifact  copy  pre-integration-test    ${project.groupId} SpringBoot2App ${project.version} jar ${project.build.directory} app.jar        com.bazaarvoice.maven.plugins process-exec-maven-plugin 0.7   start-server pre-integration-test  start   run-server false http://localhost:8080  java -jar ${project.build.directory}/app.jar     stop-server post-integration-test  stop-all     

选项2

只需从测试工件声明服务器工件的正常Maven依赖关系,然后在挂钩之前在JUnit中运行服务器的@SpringBootApplication类,或者你拥有什么,例如

 private static ConfigurableApplicationContext context; @BeforeClass public static void setUp() { context = new SpringApplicationBuilder(SimpleServiceApplication.class).run(); } @AfterClass public static void tearDown() { if (context != null) { context.close(); } } 

这可能足以满足您的需求。

RyanP的解决方案完全符合需求。

但是,我确实设法让它工作,运气不好我猜:)

  1. 它需要在TEST模块中重新添加运行应用程序所需的依赖项。 (在这种情况下,spring-boot.starter-web)

  2. 添加测试类,需要一个非常有趣的语法

到目前为止,优点是我可以在正在运行的服务器上运行测试,但仍然使用配置文件和服务的测试类来模拟一些服务。

老实说,我仍然会尝试上面的两个解决方案,但仅仅是为了节目,这就是我最终的工作

   org.springframework.boot spring-boot-maven-plugin   start-mocked-app   --server.port=${tomcat.http.port}  xxx.TestApplication ../${api-module}/target/classes   ./${api-module}/target/test-classes   MOCKED    start  pre-integration-test   stop  stop  post-integration-test    

和…

   org.springframework.boot spring-boot-starter-web provided