【问题标题】:Maven Spring Boot Plugin : how to run spring boot from another projectMaven Spring Boot 插件:如何从另一个项目运行 Spring Boot
【发布时间】:2018-11-07 14:02:26
【问题描述】:

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>

这不起作用。

我也试过在阅读插件超类的源码后添加一个“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

我在测试pom.xml中添加了starter-web项目,结果一样

【问题讨论】:

    标签: java maven spring-boot spring-boot-maven-plugin


    【解决方案1】:

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

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

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

    选项 1

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

    只需从测试工件声明对服务器工件的正常 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();
        }
    }
    

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

    【讨论】:

      【解决方案2】:

      RyanP 的解决方案完全符合需求。

      但是,我确实设法让它工作,我猜是不走运:)

      1. 它需要在 TEST 模块中重新添加运行应用程序所需的依赖项。 (本例中为spring-boot.starter-web)

      2. 添加测试类,需要非常有趣的语法

      到目前为止,优点是我可以在正在运行的服务器上运行测试,但仍然可以使用配置文件和服务的测试类来模拟一些服务。

      老实说,我仍然会尝试上述两种解决方案,但只是为了展示,这就是我最终得到的工作

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        • 2014-06-20
        • 2021-09-09
        • 2017-08-24
        • 1970-01-01
        • 2017-03-26
        相关资源
        最近更新 更多