当前位置: 首页 > 知识库问答 >
问题:

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

和选
2023-03-14

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

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

在父模块的集成测试期间,是否可以配置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>

这是不起作用的。

            <configuration> 
                <mainClass>com.SimpleServiceApplication</mainClass>
                <project>${project.parent.collectedProjects[0]}</project>
            </configuration>

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

共有1个答案

敖毅
2023-03-14

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

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

  1. 使用不同的插件来控制服务器;或
  2. 直接从代码中的测试运行服务器。
<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();
    }
}

这可能足以满足你的需要。

 类似资料:
  • 我想做这样的事情: 项目tests_thing1.sikuli: 并且有类似的类似Tests_Thing2和Tests_Thing3项目

  • 问题内容: 假设我的项目位于并且我当前的位置是如何在不更改项目位置的情况下运行? 问题答案: 您可以使用参数(或)并指定文件的路径,例如 这会像在工作目录中一样“运行” 。

  • 我有两个Firebase项目使用存储来存储文件和Firestore的元数据。一个项目用于试运行,另一个用于生产。我有一个简单的react应用程序设置,用于将我的文件和元数据上传到暂存项目。 我试图找出如何将暂存存储和Firestores同步到生产项目。我在google搜索和firebase文档方面运气不佳(除了看到可以使用node连接到多个Firebase项目之外)。 任何帮助将不胜感激。

  • 我有两个包com.a.b.c和com.x.y.z。在com.a.b2.c中,我定义了这样一个组件: 在com.x.y.z中,我有一个类,我想像这样注入我的ClassA: 我需要做什么样的配置更改才能将我的类注入到我的其他类中?现在我得到了构建错误 org.springframework.beans.factory。NoSuchBeanDefinitionException:未找到依赖项的[com.

  • 但是,在编译ApplicationA时,SBT抱怨依赖项只能是子目录!!: 这看起来很简单,拥有这个项目依赖项的正确方法是什么?

  • 问题内容: 我正在做一个多模块项目。我们正在其他几个模块中的一个模块中使用appCtx.xml。 当前的问题是它们并不总是彼此同步。 当有人修改文件并构建项目时,就会发生这种情况,这样做的人可能会忘记将其复制到另一个模块,从而导致问题。 如何将src / main / resources中的appCtx.xml从项目A复制到项目B中的src / main / resources? 问题答案: 您可