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

Gradle中SourceSet的jarTaskName属性来源与语法映射问题咨询

jarTaskName生成规则说明

jarTaskName不是SourceSet接口定义的原生显式属性,是Gradle「约定优于配置」原则下的派生约定属性,生成逻辑如下:

  • Gradle的Base插件为所有SourceSet预设了固定命名规则:对于命名为<sourceSetName>的资源集,对应Jar打包任务的默认名称为<sourceSetName>Jar(如果sourceSet名称首字母大写会自动转小写,符合小驼峰命名规范)
    • 内置main SourceSet走简化规则,对应任务名为jar
    • 内置test SourceSet对应任务名为testJar
    • 你自定义的integrationTest SourceSet对应任务名为integrationTestJar
  • 代码中调用的sourceSets.integrationTest.jarTaskName是Gradle动态推导的属性,等价于手动拼接字符串${integrationTest.name}Jar,二者输出结果完全一致
  • 该规则未直接列在SourceSet接口文档中,因为它属于Base插件对SourceSet的扩展约定,不属于SourceSet本身的内置方法,相关规则说明可以在Gradle官方Base插件约定规则章节查询

你提供的代码也可以替换为手动拼接的写法,运行效果完全一致:

def integrationTest = sourceSets.create('integrationTest')
configurations[integrationTest.implementationConfigurationName].extendsFrom(configurations.testImplementation)
configurations[integrationTest.runtimeOnlyConfigurationName].extendsFrom(configurations.testRuntimeOnly)

// 手动拼接任务名写法
def integrationTestJarTask = tasks.register("${integrationTest.name}Jar", Jar) {        
      archiveClassifier = 'integration-tests'
      from integrationTest.output
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 12:15:08