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

Fil*_*lip 7 java maven spring-boot spring-boot-maven-plugin

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!)
Run Code Online (Sandbox Code Playgroud)

我想在一个单独的项目中运行集成测试(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>
Run Code Online (Sandbox Code Playgroud)

哪个不起作用.

我还尝试在读取插件超级类的源代码后添加"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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

rya*_*anp 8

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

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

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

选项1

为此,我认为您需要一种方法来复制要运行的服务器工件,以及一些更通用的方法来启动和停止它,例如cargo-maven2-pluginprocess-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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

选项 2

只需从测试工件中声明对服务器工件的正常 Maven 依赖,然后@SpringBootApplication在钩子或您拥有的之前在 JUnit 中运行服务器的类,例如

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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