如何在 Gradle 中定义编译时 *only* 类路径? [英] How do I define a compile-time *only* classpath in Gradle?
问题描述
谁能给我一个简单的 build.gradle 示例,说明如何指定不包含在运行时部署 (war) 中的仅编译时类.
Can someone please give me a simple build.gradle example of how I can specify compile-time-only classes that are not included in the runtime deployment (war).
Gradle 似乎以错误的方式解决了这个问题,因为 'runtime' 继承自 'compile'.我无法想象在运行时需要类而在编译时不需要的情况.但是,在很多情况下,我需要在编译时生成代码的类,而我不想在运行时部署!
Gradle seems to have gotten this the wrong way around since 'runtime' inherits from 'compile'. I can't imagine a situation where I would want classes at runtime that I wouldn't want at compile time. However, there are many circumstances where I need classes to generate code at compile time that I do not wish to deploy at runtime!
我翻遍了臃肿的 gradle 文档,但找不到任何明确的说明或示例.我怀疑这可能是通过定义配置"并将其设置为 CompileJava 插件的类路径来实现的 - 但文档没有解释如何实现这一点.
I've ploughed through the bloated gradle documentation but cannot find any clear instructions or examples. I suspect this might be achieved by defining a 'configuration' and setting it as the classpath of the CompileJava plugin - but the documentation falls short on explaining how to achieve this.
推荐答案
关于这个话题已经有很多讨论了,主要是此处,但尚无明确结论.
There has been a lot of discussion regarding this topic, mainly here, but not clear conclusion.
您走在正确的轨道上:目前最好的解决方案是声明您自己的provided
配置,该配置将包含仅编译依赖项并添加到您的编译类路径中:
You are on the right track: currently the best solution is to declare your own provided
configuration, that will included compile-only dependencies and add to to your compile classpath:
configurations{
provided
}
dependencies{
//Add libraries like lombok, findbugs etc
provided '...'
}
//Include provided for compilation
sourceSets.main.compileClasspath += [configurations.provided]
// optional: if using 'idea' plugin
idea {
module{
scopes.PROVIDED.plus += [configurations.provided]
}
}
// optional: if using 'eclipse' plugin
eclipse {
classpath {
plusConfigurations += [configurations.provided]
}
}
通常这很有效.
这篇关于如何在 Gradle 中定义编译时 *only* 类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!