您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

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

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

为此,我认为您需要一种在您想要运行的服务器工件中复制的方法,以及一些用于启动和停止它的更常规的方法,例如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>

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

<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依赖关系,然后@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();
    }
}

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

Java 2022/1/1 18:20:39 有318人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶