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

为何sourceSets.generated.java.srcDirs路径为"generated-src/antlr/main"而非"generated-src/antlr"?

Why generated-src/antlr/main instead of generated-src/antlr for ANTLR-generated sources in Gradle?

Great question—this boils down to how Gradle's ANTLR plugin structures generated files compared to standard source set conventions. Let’s break it down clearly:

  • Standard source set paths rely on context
    The example you referenced from the docs:

    sourceSets {
      main {
        java { srcDirs = ['src/java'] }
        resources { srcDirs = ['src/resources'] }
      }
    }
    

    Here, we’re configuring the main source set directly inside its block. The path src/java works because the block scope already tells Gradle we’re targeting the main source set—we don’t need to append /main to the path because the context defines which source set we’re working with.

  • ANTLR plugin enforces source-set-specific isolation
    The ANTLR plugin is built to keep generated code separate per source set (like main vs test). By default, it outputs generated code to generated-src/antlr/<sourceSetName>—so for the main source set, that becomes generated-src/antlr/main. This separation ensures application code generated for production doesn’t get mixed with test-specific generated code, avoiding conflicts or unintended dependencies.

  • You need to target the exact subdirectory for your use case
    When you set sourceSets.generated.java.srcDirs = ["generated-src/antlr/main"], you’re telling Gradle to only include ANTLR code generated for the main source set. If you used just generated-src/antlr, you’d pull in all ANTLR-generated files (including those for test or custom source sets), which could lead to compile errors or include unwanted code in your final build.

In short: Standard source set config uses block scope to avoid repeating the source set name in paths, while the ANTLR plugin embeds the source set name in its output path to enforce isolation. Matching that exact subdirectory ensures you’re only including the generated code you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:10:43