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 故障安全插件)。
在父模块的集成测试期间,是否可以配置 spring boot maven 插件来启动/停止子模块?
我尝试过类似的方法,但没有成功
<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>
这是行不通的。
我在阅读插件父类(super class)的源码后也尝试添加一个“project”参数 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java
<configuration>
<mainClass>com.SimpleServiceApplication</mainClass>
<project>${project.parent.collectedProjects[0]}</project>
</configuration>
这指的是正确的项目,如调试所示,但也不起作用。
请不要对 [0] 发表评论,我知道 [0] 不干净并且是一个需要直接了解父 pom 模块排序的耦合。
我在 org/springframework/boot/SpringApplication 上得到一个 java.lang.NoClassDefFoundError
我将starter-web项目添加到测试pom.xml中,结果相同
最佳答案
我不认为可以使用 spring-boot-maven-plugin
对另一个模块运行集成测试,因为 start
目标似乎没有给出您可以从本地存储库或 Maven react 器解析应用程序,这可能是您想要的。您尝试过的 project
配置属性并非旨在以这种方式覆盖。插件执行应仅使用插件目标中列出的属性进行配置 docs .
相反,我认为您至少有两种可能的方法:
- 使用不同的插件来控制服务器;或
- 直接从代码中的测试运行服务器。
选项1
为此,我认为您需要一种方法来复制您要运行的服务器 Artifact ,以及一些更通用的启动和停止它的方法,例如 cargo-maven2-plugin或 process-exec-maven-plugin .
只需在服务器构件构建中配置repackage
目标即可:
<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>
然后在集成测试模块中,您可以执行如下操作:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-server-artifact</id>
<goals>
<goal>copy</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>SpringBoot2App</artifactId>
<version>${project.version}</version>
<classifier>jar</classifier>
<outputDirectory>${project.build.directory}</outputDirectory>
<destFileName>app.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.bazaarvoice.maven.plugins</groupId>
<artifactId>process-exec-maven-plugin</artifactId>
<version>0.7</version>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<name>run-server</name>
<waitForInterrupt>false</waitForInterrupt>
<healthcheckUrl>http://localhost:8080</healthcheckUrl>
<arguments>
<argument>java</argument>
<argument>-jar</argument>
<argument>${project.build.directory}/app.jar</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-all</goal>
</goals>
</execution>
</executions>
</plugin>
选项 2
只需从测试 Artifact 中声明对服务器 Artifact 的正常 Maven 依赖性,然后在 JUnit 中运行服务器的 @SpringBootApplication
类,然后再 Hook 或您有什么,例如
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();
}
}
这可能足以满足您的需求。
关于java - Maven Spring Boot 插件 : how to run spring boot from another project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50575478/