Java Maven Spring Boot插件:如何从另一个项目运行Spring Boot
我有一个项目,有两个模块Java Maven Spring Boot插件:如何从另一个项目运行Spring Boot,java,maven,spring-boot,spring-boot-maven-plugin,Java,Maven,Spring Boot,Spring Boot Maven Plugin,我有一个项目,有两个模块 [Parent] |-pom.xml | [SpringBoot2App] | |-pom.xml | [test] | |-pom.xml (start goal here!) 我想在另一个模块的单独项目中运行集成测试(maven failsafe插件) 在父模块的集成测试期间,是否可以配置SpringBootMaven插件来启动/停止子模块 我尝试过这样的事情,但没有成功 <plugin>
[Parent]
|-pom.xml
| [SpringBoot2App]
| |-pom.xml
| [test]
| |-pom.xml (start goal here!)
我想在另一个模块的单独项目中运行集成测试(maven failsafe插件)
在父模块的集成测试期间,是否可以配置SpringBootMaven插件来启动/停止子模块
我尝试过这样的事情,但没有成功
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.SimpleServiceApplication</mainClass>
<classesDirectory>../SpringBoot2App/target/classes</classesDirectory>
<folders>
<param>../SpringBoot2App/target/test-classes</param>
</folders>
</configuration>
<executions>
<execution>
<goals>
<goal>start</goal>
</goals>
<phase>pre-integration-test</phase>
</execution>
</executions>
</plugin>
org.springframework.boot
com.simpleservice应用程序
${project.parent.collectedProjects[0]}
如调试所示,这引用了正确的项目,但也不起作用
请不要对[0]发表评论,我知道[0]不干净,是一个耦合,需要直接了解父pom模块顺序
我在org/springframework/boot/SpringApplication上得到一个java.lang.NoClassDefFoundError
我将starter web项目添加到test pom.xml中,同样的结果我认为使用spring boot maven plugin
对另一个模块运行集成测试是不可能的,因为start
目标似乎没有提供从本地存储库或maven reactor解析应用程序的方法,这可能就是你想要的。您尝试的project
configuration属性不是设计为以这种方式重写的。插件执行应仅使用插件目标中列出的属性进行配置
相反,我认为你至少有两种可能的方法:
使用不同的插件控制服务器;或
直接从代码中的测试运行服务器
选择1
为此,我认为您需要一种在您想要运行的服务器工件中复制的方法,以及一些更通用的启动和停止方法,如or
只需在服务器工件构建中配置重新打包
目标:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>true</excludeDevtools>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
这可能足以满足您的需求。RyanP的解决方案完全满足您的需求
但是,我确实成功了,我想是运气使然:)
它需要在测试模块中重新添加运行应用程序所需的依赖项。(本例中为spring-boot.starter-web)
添加测试类需要非常有趣的语法
到目前为止,优点是我可以在运行的服务器上运行测试,但仍然可以使用概要文件和服务的测试类模拟一些服务
老实说,我仍然会尝试以上两种解决方案,但只是为了展示,以下是我最终得到的效果
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>start-mocked-app</id>
<configuration>
<arguments>
<argument>--server.port=${tomcat.http.port}</argument>
</arguments>
<mainClass>xxx.TestApplication</mainClass>
<classesDirectory>../${api-module}/target/classes</classesDirectory>
<folders>
<!-- wow! notice the weird "./" rather than the expected "../" -->
<folder>./${api-module}/target/test-classes</folder>
</folders>
<profiles>
<profile>MOCKED</profile>
</profiles>
</configuration>
<goals>
<goal>start</goal>
</goals>
<phase>pre-integration-test</phase>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
org.springframework.boot
springbootmaven插件
启动模拟应用程序
--server.port=${tomcat.http.port}
xxx.测试应用
../${api模块}/target/classes
./${api模块}/target/test类
嘲弄
开始
预集成测试
停止
停止
整合后测试
而且
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
</dependencies>
org.springframework.boot
SpringBootStarterWeb
假如
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();
}
}
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>start-mocked-app</id>
<configuration>
<arguments>
<argument>--server.port=${tomcat.http.port}</argument>
</arguments>
<mainClass>xxx.TestApplication</mainClass>
<classesDirectory>../${api-module}/target/classes</classesDirectory>
<folders>
<!-- wow! notice the weird "./" rather than the expected "../" -->
<folder>./${api-module}/target/test-classes</folder>
</folders>
<profiles>
<profile>MOCKED</profile>
</profiles>
</configuration>
<goals>
<goal>start</goal>
</goals>
<phase>pre-integration-test</phase>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
</dependencies>