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

如何配置Gradle使调试资源仅在本地IDE生效不随Jar部署

解决Gradle配置:本地调试类路径包含自定义目录但不打包进Jar

直接在项目的build.gradle中添加以下配置即可满足需求:

1. 核心配置:资源目录与打包排除

sourceSets {
    main {
        resources {
            // 将自定义目录加入资源路径,本地运行时可读取
            srcDir 'src/env-properties'
            // 打包Jar时排除该目录下所有文件
            exclude '**/*'
        }
    }
}

这段配置的作用是:把src/env-properties纳入项目的资源目录范畴,本地调试时类路径会包含该目录;同时明确排除该目录下所有文件,避免被打包进最终的Jar包。

2. 适配IDEA:避免Gradle刷新后配置失效

idea {
    module {
        // 强制将目录标记为IDEA的资源根目录
        resourceDirs += file('src/env-properties')
    }
}

添加后,执行gradlew idea或在IDEA中刷新Gradle项目,src/env-properties会被自动识别为资源目录,无需手动配置。

3. 适配Eclipse:执行gradlew eclipse后自动加入类路径

eclipse {
    classpath {
        file {
            whenMerged { classpath ->
                // 创建源文件夹条目并添加到Eclipse类路径
                def envPropsFolder = new org.gradle.plugins.ide.eclipse.model.SourceFolder('src/env-properties', null)
                envPropsFolder.entryAttributes['kind'] = 'src'
                classpath.entries.add(envPropsFolder)
            }
        }
    }
}

运行gradlew eclipse生成Eclipse项目文件后,src/env-properties会被自动加入类路径,团队成员直接导入项目即可使用。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 19:35:25