You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Maven Clean Install期间禁用Spring Boot定时任务?

解决SpringDoc Maven插件触发应用启动时禁用定时任务的问题

问题分析

执行mvn clean install时,springdoc-openapi-maven-plugin的generate目标会触发spring-boot-maven-plugin的start目标启动应用,但此时应用加载的是主环境配置(src/main/resources/application.yml),而非测试环境配置,所以你在src/test/resources/application.yml中设置的scheduling.enabled=false不会生效,导致定时任务依然启动。

解决方案

你可以通过两种方式直接在插件配置中指定属性或激活Spring Boot Profile,从而禁用定时任务:

方式1:修改spring-boot-maven-plugin的启动参数

在spring-boot-maven-plugin的<jvmArguments>中直接添加系统属性,覆盖主配置的scheduling.enabled值,或者激活测试Profile:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot-maven-plugin.version}</version>
    <configuration>
        <jvmArguments>
            -Dspring.application.admin.enabled=true
            <!-- 直接禁用定时任务 -->
            -Dscheduling.enabled=false
            <!-- 或者激活测试Profile,加载test目录下的配置 -->
            <!-- -Dspring.profiles.active=test -->
        </jvmArguments>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>start</goal>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

方式2:在springdoc-openapi-maven-plugin中配置参数

springdoc插件支持直接传递属性或JVM参数给启动的应用,在插件的<configuration>中添加对应配置即可:

<plugin>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!-- 直接设置禁用定时任务的属性 -->
                <properties>
                    <scheduling.enabled>false</scheduling.enabled>
                </properties>
                <!-- 或者通过JVM参数激活测试Profile -->
                <!-- <jvmArguments>-Dspring.profiles.active=test</jvmArguments> -->
            </configuration>
        </execution>
    </executions>
</plugin>

验证效果

修改配置后执行mvn clean install,控制台会输出Scheduling enabled: false,说明定时任务配置已被禁用,同时SpringDoc依然会正常生成OpenAPI yaml文件。

内容的提问来源于stack exchange,提问作者1Z10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:11:00