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

如何不修改依赖配置,实现Gradle生成无依赖WAR并运行OpenAPI任务

问题解决:Gradle下生成无依赖WAR同时确保OpenAPI文档生成成功

问题背景

我有一个Spring Boot应用,使用Gradle 7.6.x,Gradle配置如下:

buildscript {
    ext {
        springBootPlugin = 'org.springframework.boot:spring-boot-gradle-plugin'
        springBootVersion = '2.4.5'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.springdoc:springdoc-openapi-gradle-plugin:1.3.2"
        classpath "gradle.plugin.com.github.jengelman.gradle.plugins:gradle-processes:0.5.0"
    }
}

plugins {
    id 'war'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "org.springdoc.openapi-gradle-plugin"

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compileOnly group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '2.4.5'
    compileOnly('org.springframework.boot:spring-boot-starter')
    compileOnly('org.springframework.boot:spring-boot-starter-web')
    compileOnly 'org.springframework.boot:spring-boot-starter-data-jpa'
    compileOnly 'org.springdoc:springdoc-openapi-ui:1.6.15'
    //testRuntime('org.springframework.boot:spring-boot-starter-test')
    runtimeOnly 'com.h2database:h2'
}

springBoot {
    mainClassName = 'com.example.DemoApplication'
}


jar {
    enabled = false
}
war {
    classpath = configurations.providedRuntime
}

openApi {
    apiDocsUrl.set("http://localhost:8080/v3/api-docs")
    outputDir.set(file("$buildDir/api"))
    outputFileName.set("openapi.json")
    waitTimeInSeconds.set(100)
   //forkProperties.set("-Dlogging.level.root=DEBUG")
    //forkProperties.set("-Dspring.profiles.active=openapi")
}

需求:

  • 执行gradle clean build时生成不包含任何依赖库的WAR包
  • 执行gradle generateOpenApiDocs时,应用能正常启动以生成OpenAPI JSON文件
    要求不修改现有dependencies块的配置,确保OpenAPI文档生成任务成功。

解决方案

核心问题是generateOpenApiDocs任务需要启动应用,但当前核心依赖均为compileOnly,任务执行时类路径缺失导致启动失败。我们可以通过修改该任务的类路径来解决,不影响WAR包的生成逻辑:

1. 为generateOpenApiDocs任务补充类路径

在现有Gradle配置的末尾添加以下代码:

tasks.named('generateOpenApiDocs') {
    // 将compileOnly依赖加入任务运行类路径
    classpath += configurations.compileOnly
    // 确保runtimeOnly依赖也被包含
    classpath += configurations.runtimeOnly
}

说明:

  • 仅修改generateOpenApiDocs任务的类路径,不会改变build任务生成WAR的规则(WAR依然不会包含依赖)
  • compileOnly中的Spring Boot Starter、OpenAPI UI等依赖会被加载,满足应用启动需求
  • runtimeOnly的H2数据库依赖本来属于运行时配置,显式添加确保完整性

2. 可选:调整应用启动参数(按需)

如果应用启动需要特定配置(如指定环境、日志级别),可以启用openApi块中的forkProperties配置:

openApi {
    // 保留原有配置
    apiDocsUrl.set("http://localhost:8080/v3/api-docs")
    outputDir.set(file("$buildDir/api"))
    outputFileName.set("openapi.json")
    waitTimeInSeconds.set(100)
    // 添加启动参数
    forkProperties.set(["-Dspring.profiles.active=openapi", "-Dlogging.level.root=INFO"])
}

验证

  • 执行gradle generateOpenApiDocs,检查$buildDir/api/openapi.json是否成功生成
  • 执行gradle clean build,打开生成的WAR包,确认WEB-INF/lib目录为空(无依赖库)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 00:43:17