[英]Maven Spring Boot Plugin : how to run spring boot from another project
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插件來啟動/停止子模塊?
我嘗試過這樣的事情沒有成功
<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>
哪個不起作用。
我還嘗試在讀取插件超級類的源代碼后添加“project”參數https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot -tools /彈簧引導Maven的插件/ src目錄/主/ JAVA /組織/ springframework的的/ boot /行家/ 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反應器解析應用程序的方法,可能就是你想要的。 您嘗試過的project
配置屬性並非旨在以這種方式覆蓋。 應僅使用插件目標文檔中列出的屬性配置插件執行。
相反,我認為你至少有兩種可能的方法:
為此,我認為您需要一種方法可以復制您想要運行的服務器工件,以及一些更通用的啟動和停止它的方法,例如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>
只需從測試工件聲明服務器工件的正常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的解決方案完全符合需求。
但是,我確實設法讓它工作,運氣不好我猜:)
它需要在TEST模塊中重新添加運行應用程序所需的依賴項。 (在這種情況下,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>
和...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
</dependencies>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.