简体   繁体   English

如何在Gradle中将自定义Antlr输出路径添加到主要sourceset?

[英]How to add custom Antlr output path to main sourceset in Gradle?

So, I'm new to Gradle and Java in general and having quite a few problems. 因此,我一般来说对Gradle和Java都是陌生的,并且有很多问题。 Because of some other weird difficulties with IntelliJ, I want to change path that Antlr outputs the generated code to. 由于IntelliJ还有其他一些怪异的困难,我想更改Antlr将生成的代码输出到的路径。 This was easy to change: 这很容易更改:

generateGrammarSource {
    outputDirectory = file("src/temp/generated-code")
}

However, now I'm having great difficulty actually getting it to compile into my "main" and "test" source sets. 但是,现在要真正将其编译到我的“主”和“测试”源集中非常困难。 I basically just want to extend the main and test source sets to include these files. 我基本上只想扩展主要和测试源集以包括这些文件。 I tried doing that with the something like: 我尝试用类似的方法做到这一点:

sourceSets {
    generated{
        java {
            srcDir 'src/temp/generated-code'
        }
    }
    main {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }
    test {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }
}

However, doing this doesn't allow the generated code compilation to have access to the dependencies. 但是,这样做不允许生成的代码编译访问依赖项。 So, compilation fails because it can't use all of the stuff in the antlr packages. 因此,编译失败,因为它无法使用antlr软件包中的所有内容。

Is there any easy way to add these dependencies, OR, just force the main and test source sets to somehow include the generated code? 是否有任何简单的方法来添加这些依赖关系,或者只是强制将主要和测试源集以某种方式包括生成的代码?

I ended up figuring this out in a deceptively easy way: 我最终以一种看似简单的方式弄清楚了这一点:

sourceSets {
    main {
        java {
            srcDirs = ["src/main/java", "src/temp/generated-code"]
        }
    }
}

Though I did have to add this for proper clean up: 虽然我确实必须添加此代码以进行适当的清理:

clean.doFirst {
    delete "src/temp"
}

I feel like there is probably a better way to do it than passing these path names around everywhere, but it seems to work fine 我觉得可能有比在任何地方传递这些路径名更好的方法,但它似乎可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM