java 在 Gradle 中拆分所有输出目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44578302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting all output directories in Gradle
提问by shammancer
Gradle 4.0 came out yesterday and I updated my project for it.
Gradle 4.0 昨天发布了,我为它更新了我的项目。
Now I am getting the following warning:
现在我收到以下警告:
Gradle now uses separate output directories for each JVM language, but this build assumes a single directory for all classes from a source set. This behaviour has been deprecated and is scheduled to be removed in Gradle 5.0
Gradle 现在为每种 JVM 语言使用单独的输出目录,但此构建假定源集中的所有类都在一个目录中。此行为已被弃用,并计划在 Gradle 5.0 中删除
I would like to use separate output directories for each language. What do I need to change to make that happen?
我想为每种语言使用单独的输出目录。我需要改变什么才能做到这一点?
Things I tried:
我尝试过的事情:
gradle clean
followed bygradle build
- deleting the build directory then running
gradle build
. - deleting the gradle and build directory then running
gradle
gradle clean
其次是gradle build
- 删除构建目录然后运行
gradle build
. - 删除gradle和build目录然后运行
gradle
Related GitHub issue
相关的 GitHub问题
Gradle Plugins:
Gradle插件:
- java
- eclipse
- idea
- org.springframework.boot
- 爪哇
- 蚀
- 主意
- org.springframework.boot
采纳答案by gavenkoa
Gradle 4.0 introduces multiple sourceSet
s per JVM language in order to enable remote build caching. With the java
plugin your build/classes/main
should become build/classes/java/main
and build/classes/test
should become build/classes/java/test
, etc.
Gradle 4.0sourceSet
为每种 JVM 语言引入了多个s 以启用远程构建缓存。使用java
插件你build/classes/main
应该成为build/classes/java/main
,build/classes/test
应该成为build/classes/java/test
,等等。
The warning you're seeing is defined in DefaultSourceSets.java
您看到的警告在DefaultSourceSets.java 中定义
Therefore, if any plugin within your project or your build.gradle
calls DefaultSourceSetOutput.getClassesDir()
(or access classesDir
) you get this warning.
因此,如果您的项目或您的build.gradle
调用DefaultSourceSetOutput.getClassesDir()
(或访问classesDir
)中有任何插件,您会收到此警告。
Solution 1
方案一
Use
利用
sourceSets.main.output.classesDir = new File(buildDir, "classes/main")
which corresponds to:
对应于:
@Override
public boolean isLegacyLayout() {
return classesDir!=null;
}
@Override
public void setClassesDir(File classesDir) {
setClassesDir((Object)classesDir);
}
@Override
public void setClassesDir(Object classesDir) {
this.classesDir = classesDir;
this.classesDirs.setFrom(classesDir);
}
Note that SourceSetOutput.javamarks getClassesDir()
as deprecated.
请注意SourceSetOutput.java标记getClassesDir()
为已弃用。
So until all plugins in your project get support for Gradle 4.0 you should stick to the workaround and ignore the deprecation warnings.
因此,在您项目中的所有插件都获得对 Gradle 4.0 的支持之前,您应该坚持解决方法并忽略弃用警告。
Another issue is test files. If you don't want to have the new layout(build/classes/main
and build/classes/java/test
) you should adjust test path too:
另一个问题是测试文件。如果您不想拥有新的布局(build/classes/main
和build/classes/java/test
),您也应该调整测试路径:
sourceSets.main.output.classesDir = new File(buildDir, "classes/main")
sourceSets.test.output.classesDir = new File(buildDir, "classes/test")
UPDATEUsers of IDEA may notice that IDE starts using separate out
directories for build if Gradle 4.x is detected. That makes impossible hot app reloadingif you run app outside of IDEA. To fix that add and reimport:
更新IDEA 的用户可能会注意到,out
如果检测到 Gradle 4.x ,IDE 开始使用单独的目录进行构建。如果您在 IDEA 之外运行应用程序,则不可能重新加载热应用程序。要修复该添加和重新导入:
subprojects {
apply plugin: 'idea'
// Due to Gradle 4.x changes (separate output directories per JVM language)
// Idea developers refuse to reuse Gradle classpath and use own 'out/' directory.
// Revert to old behavior to allow Spring Devtool to work with using fast Idea compiler.
// https://youtrack.jetbrains.com/issue/IDEA-175172
// Alternatively use native Gradle builds or bootRun.addResources = true
// To use this feature push Ctrl+Shift+F9 to recompile!
// Be aware that Idea put resources into classes/ directory!!
idea.module.inheritOutputDirs = false
idea.module.outputDir = sourceSets.main.output.classesDir
idea.module.testOutputDir = sourceSets.test.output.classesDir
}
Please note that IDEA puts resources into the same directory as .class
files so your Gradle classpath could be corrupted. Just do gradle clean
for modules on which you use IDEA built-in build commands (Ctrl+Shift+F10, etc).
请注意,IDEA 将资源与.class
文件放在同一目录中,因此您的 Gradle 类路径可能会损坏。只需gradle clean
对使用 IDEA 内置构建命令(Ctrl+Shift+F10 等)的模块进行操作。
回答by Dordoka Maisu
This is due to the change introduced in Gradle 4.0: it now uses separate output directories if there are multiple language sources.
这是由于 Gradle 4.0 中引入的更改:如果有多种语言源,它现在使用单独的输出目录。
To return to the old behaviour and get rid of the warning, insert this into your build.gradle:
要返回旧行为并消除警告,请将其插入到您的 build.gradle 中:
// Change the output directory for the main source set back to the old path
sourceSets.main.output.classesDir = new File(buildDir, "classes/main")
// Change the output directory for the main source set back to the old path
sourceSets.main.output.classesDir = new File(buildDir, "classes/main")
Reference: https://docs.gradle.org/4.0/release-notes.html#multiple-class-directories-for-a-single-source-set
参考:https: //docs.gradle.org/4.0/release-notes.html#multiple-class-directories-for-a-single-source-set
回答by Tono Wiedermann
My case was a bit specific because output classes directories were used to construct classpath entry for command-line execution. But perhaps this will help someone.
我的情况有点特殊,因为输出类目录用于构造命令行执行的类路径条目。但也许这会帮助某人。
I decided to concatenate all output directories. The change I made was form
我决定连接所有输出目录。我所做的改变是形式
sourceSets.integrationTest.output.classesDir
to
到
ext {
classpathSeparator = System.properties['os.name'].toLowerCase().contains('windows')?";":":"
}
...
sourceSets.integrationTest.output.classesDirs.join(classpathSeparator)
回答by Andrii Abramov
For example, if you mix Java, Kotlin and Groovy project structure should be like the following:
例如,如果混合使用 Java,则 Kotlin 和 Groovy 项目结构应如下所示:
root/
src/
main/
java/
kotlin/
groovy/
test/
java/
kotlin/
groovy/
In you build.gradleyou have to specify plugins that are required for specific language.
在build.gradle 中,您必须指定特定语言所需的插件。
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'kotlin'