Spring boot maven插件:包含测试资源

     2023-03-30     277

关键词:

【中文标题】Spring boot maven插件:包含测试资源【英文标题】:Spring boot maven plugin: include test resources 【发布时间】:2019-04-25 22:39:18 【问题描述】:

我的目录结构如下:

src
|
|___ main
|
|___ test
      |
      |___resources

在 pre-integration-test 阶段运行启动目标(spring-boot:start,不同于 spring-boot:run)时,我想将来自测试资源的所有文件包含在类路径中。

我的 pom.xml 是:

            <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <wait>1000</wait>
                        <maxAttempts>30</maxAttempts>
                    </configuration>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我尝试了包括标志 addResourcesuseTestClasspath 在内的成功尝试,即使阅读文档 (https://docs.spring.io/spring-boot/docs/current/maven-plugin/start-mojo.html),我也真的无法弄清楚它们应该如何工作。

提前致谢。

【问题讨论】:

使用 Maven 配置文件并创建一个测试配置文件不是更好的主意吗?我认为这就是使用 Maven 的方式。 【参考方案1】:

这是一个有效的 Maven 配置:

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.2.2.RELEASE</version>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
            <execution>
                <id>pre-integration-test</id>
                <goals>
                    <goal>start</goal>
                </goals>
                <configuration>
                    <classesDirectory>$project.build.testOutputDirectory:$project.build.outputDirectory</classesDirectory>
                    <wait>1000</wait>
                    <maxAttempts>30</maxAttempts>
                </configuration>
            </execution>
            <execution>
                <id>post-integration-test</id>
                <goals>
                    <goal>stop</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

【讨论】:

如何使用 Spring Boot 测试 Maven 模块项目

】如何使用SpringBoot测试Maven模块项目【英文标题】:HowtotestMavenmoduleprojectwithSpringBoot【发布时间】:2016-10-0314:39:29【问题描述】:我已经将一个基于SpringBoot的项目拆分为几个Maven模块。现在只有war-project包含一个starter类(有一个m... 查看详情

无法理解 Spring Boot 如何使用 maven 管理集成测试。它是不是使用故障安全插件?

】无法理解SpringBoot如何使用maven管理集成测试。它是不是使用故障安全插件?【英文标题】:Can\'tunderstandhowSpringBootmanageintegrationtestswithmaven.Isitusesfailsafepluginornot?无法理解SpringBoot如何使用maven管理集成测试。它是否使用故障安全... 查看详情

使用 maven 插件设置时 Spring Boot 配置文件不活动

】使用maven插件设置时SpringBoot配置文件不活动【英文标题】:SpringBootprofilesnotactivewhensetwithmavenplugin【发布时间】:2016-08-0911:29:06【问题描述】:我正在尝试在启动Tomcat以在maven中进行集成测试时设置spring配置文件,如下所示:...... 查看详情

如何在我的 Maven Spring Boot 多模块项目的测试中引用兄弟模块?

】如何在我的MavenSpringBoot多模块项目的测试中引用兄弟模块?【英文标题】:Howtoreferencesiblingmoduleinatestformymavenspringbootmulti-moduleproject?【发布时间】:2020-03-2023:02:05【问题描述】:如何在Maven中包含兄弟模块作为我的SpringBoot测试... 查看详情

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

】MavenSpringBoot插件:如何从另一个项目运行SpringBoot【英文标题】:MavenSpringBootPlugin:howtorunspringbootfromanotherproject【发布时间】:2018-11-0714:02:26【问题描述】:https://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html我有一个项目,... 查看详情

Spring Boot Maven 插件 - spring-boot:run 和优雅关闭

】SpringBootMaven插件-spring-boot:run和优雅关闭【英文标题】:SpringBootMavenplugin-spring-boot:runandgracefulshutdown【发布时间】:2018-01-0714:00:27【问题描述】:我可以使用以下Maven命令运行SpringBoot应用程序:mvnspring-boot:run但我不知道如何优雅... 查看详情

maven插件系列之spring-boot-maven-plugin

Maven插件系列之spring-boot-maven-pluginSpringBoot的Maven插件(SpringBootMavenplugin)能够以Maven的方式为应用提供SpringBoot的支持,即为SpringBoot应用提供了执行Maven操作的可能。SpringBootMavenplugin能够将SpringBoot应用打包为可执行的jar或war文件&#... 查看详情

Maven 没有运行 Spring Boot 测试

】Maven没有运行SpringBoot测试【英文标题】:MavennotrunningSpringBoottests【发布时间】:2015-10-1908:04:28【问题描述】:我有一个想要测试的剩余SpringBootRESTAPI。我可以在Eclipse中手动运行测试(无需maven,并将应用程序作为JUnit测试运行... 查看详情

使用 Spring Boot BOM 管理 Maven 插件版本

】使用SpringBootBOM管理Maven插件版本【英文标题】:ManagingMavenpluginversionswithSpringBootBOM【发布时间】:2021-08-0213:35:58【问题描述】:我有一个用于SpringBoot项目的MavenPOM。POM导入SpringBootBOM。现在我想自定义一个Maven插件的配置,比如m... 查看详情

带有 JDK 17 的 Spring Boot 2.5+ - maven surefire 不执行现有测试

】带有JDK17的SpringBoot2.5+-mavensurefire不执行现有测试【英文标题】:SpringBoot2.5+withJDK17-mavensurefiredoesn\'texecuteexistingtests【发布时间】:2022-01-1523:40:33【问题描述】:使用JDK17将SpringBoot2.2应用程序更改为2.5.5时,Surefire测试插件不会启... 查看详情

Spring Boot Maven 插件 - 没有 BOOT-INF 目录

】SpringBootMaven插件-没有BOOT-INF目录【英文标题】:SpringBootMavenPlugin-NoBOOT-INFdirectory【发布时间】:2017-08-2418:59:13【问题描述】:在spring-boot-maven-plugin的1.3.8.RELEASE版本和1.4.0.RELEASE版本之间-生成的包结构发生了变化(如果您提取uber... 查看详情

使用 AttributeConverter 的 Spring Boot 测试不适用于 Maven 测试

】使用AttributeConverter的SpringBoot测试不适用于Maven测试【英文标题】:SpringboottestwithAttributeConverternotworkingwithMaventest【发布时间】:2021-11-0213:33:14【问题描述】:我有Spring引导单元测试(使用@SpringBootTest注释的类)在使用我的IDE时... 查看详情

Maven-在当前项目和插件组中找不到前缀“spring-boot”的插件

】Maven-在当前项目和插件组中找不到前缀“spring-boot”的插件【英文标题】:Maven-Nopluginfoundforprefix\'spring-boot\'inthecurrentprojectandintheplugingroups【发布时间】:2015-08-3113:55:14【问题描述】:我正在尝试构建一个使用SpringToolsSuite构建... 查看详情

由于 maven 插件错误,spring boot maven 项目未编译

】由于maven插件错误,springbootmaven项目未编译【英文标题】:springbootmavenprojectisnotcompilingduetomavenpluginerror【发布时间】:2018-03-2000:53:42【问题描述】:我正在尝试在netbeans中运行一个简单的SpringBoot项目,但代码未编译。这是我得... 查看详情

Spring Boot maven 多模块项目——单元测试(应用上下文)

】SpringBootmaven多模块项目——单元测试(应用上下文)【英文标题】:SpringBootmavenmultimoduleproject-unittesting(applicationcontext)【发布时间】:2017-12-0614:45:39【问题描述】:我是Spring框架的初学者。我在springboot中配置单元测试时遇到问... 查看详情

了解多模块项目中的 Spring Boot Maven 插件

】了解多模块项目中的SpringBootMaven插件【英文标题】:understandingSpringBootMavenPlugininMulti-Moduleprojects【发布时间】:2019-01-3019:38:42【问题描述】:我正在学习SpringBoot多模块项目,并尝试了解SpringBootmaven插件到底是什么。例如,我有... 查看详情

Spring Boot Maven 插件 - “附加”设置有啥作用?

】SpringBootMaven插件-“附加”设置有啥作用?【英文标题】:SpringBootMavenPlugin-Whatdoesthe"attach"settingdo?SpringBootMaven插件-“附加”设置有什么作用?【发布时间】:2020-03-1016:23:56【问题描述】:SpringBootMaven插件有一个名为“att... 查看详情

spring-boot-maven插件repackage(goal)的那些事

前言:在打包Springboot项目成jar包时需要在pom.xml使用spring-boot-maven-plugin来增加Maven功能,在我的上一篇博客<<Maven生命周期和插件的那些事(2021版)>>中已经介绍过Maven和插件的关系,在此不再赘述,... 查看详情