Maven Spring Boot插件:如何从另一个项目运行spring boot [英] Maven Spring Boot Plugin : how to run spring boot from another project

查看:210
本文介绍了Maven Spring Boot插件:如何从另一个项目运行spring boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://docs.spring.io /spring-boot/docs/current/maven-plugin/usage.html

我有一个项目,有2个模块。

I have a project, with 2 modules.

[Parent]  
|-pom.xml
|  [SpringBoot2App]  
|  |-pom.xml  
|  [test]  
|  |-pom.xml  (start goal here!)

我想运行集成测试(maven failsafe插件) )在一个单独的项目中,这是另一个模块。

I want to run integration tests (maven failsafe plugin) in a separate project which is another module.

在父项的集成测试期间,是否可以配置spring boot maven插件来启动/停止子模块模块?

Is is possible to configure the spring boot maven plugin to start/stop the child module, during the integration tests of the parent module?

我尝试了这样的事情没有成功

I tried something like this without success

        <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>

哪个不起作用。

I还尝试在读取插件的超类源后添加project参数
https:// github。 COM /弹簧项目/弹簧启动/ BLOB /主/弹簧启动项目/弹簧引导工具/弹簧引导Maven的插件/ src目录/主/ JAVA /组织/ springframework的的/ boot /行家/ AbstractRunMojo。 java

I also tried to add a "project" parameter after reading the source of the super class of the plugin 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>

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

This refers to the right project, as debug shows, but does not work either.

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

Please don't comment on the [0], I know the [0] is not clean and is a coupling that requires direct knowledge of parent pom module ordering.

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

I get a java.lang.NoClassDefFoundError on org/springframework/boot/SpringApplication

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

I added the starter-web project to the test pom.xml, same result

推荐答案

我不认为可以使用<$ c对另一个模块运行集成测试$ c> spring-boot-maven-plugin ,因为 start 目标似乎没有给你一个解决方案来解决本地存储库或Maven反应堆,这可能是你想要的。您尝试过的项目配置属性并非旨在以这种方式覆盖。应仅使用插件目标中列出的属性配置插件执行 docs

I don't think it's possible to run integration tests against another module using spring-boot-maven-plugin, because the start goal doesn't seem to give you a means to resolve the application from the local repository or the Maven reactor, which is probably what you want. The project configuration property you've tried isn't designed to be overriden that way. Plugin executions should be configured using only the properties listed in the plugin goal docs.

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

Instead I think you've got at least two possible approaches:


  1. 使用其他插件来控制服务器;或

  2. 直接从代码中的测试运行服务器。



选项1



为此,我认为你需要一种方法来复制你想要运行的服务器工件,以及一些更通用的启动和停止它的方法,比如 cargo-maven2-plugin process-exec-maven-plugin

只需配置 repackage 服务器工件构建中的目标:

Just configure the repackage goal in the server artifact build:

<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>

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

Then from the integration tests module, you could do something like:

<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



只需申报正常Maven依赖于测试工件中的服务器工件,并在挂钩之前在JUnit中运行服务器的 @SpringBootApplication 类,或者你有什么,例如

Option 2

Just declare a normal Maven dependency on the server artifact from the test artifact, and run the server's @SpringBootApplication class in a JUnit before hook or what have you, e.g.

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();
    }
}

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

This may be sufficient for your needs.

这篇关于Maven Spring Boot插件:如何从另一个项目运行spring boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆