分割Gradle中的所有输出目录 [英] Splitting all output directories in Gradle
问题描述
现在我收到以下警告:
Gradle现在为每种JVM语言使用单独的输出目录,但是
这个版本假设源
集合中的所有类都有一个单独的目录。此行为已被弃用,计划在Gradle 5.0中删除
。
我想使用单独的输出目录每种语言。我需要做些什么来改变这种情况?
我试过的东西:
-
gradle clean
后跟gradle build
- 删除build目录然后运行
gradle build
。 - 删除gradle和build目录,然后运行<$ c
$ b $相关GitHub 问题
Gradle插件:
- java
- eclipse
- 主意
- org.springframework.boot
解决方案Gradle 4.0为每个JVM语言引入了多个sourceSet以启用远程构建缓存。使用
java
插件,您的build / classes / main
应该变成build / classes / java / main
和build / classes / test
应该变成build / classes / java / test
,等等。
该警告的定义如下:
https://github.com/gradle/ gradle / blob / bb0ed850aa4061931a62133091d66c56551cf5e0 / subprojects / plugins / src / main / java / org / gradle / api / internal / tasks / DefaultSourceSetOutput.java#L79
<$ p (); {code> @Override
public File getClassesDir(){
if(isLegacyLayout()){
return fileResolver.resolve(classesDir);
SingleMessageLogger.nagUserOfDeprecatedBehaviour(Gradle现在为每个JVM语言使用单独的输出目录,但是这个版本假设源集中所有类的单个目录);
Object firstClassesDir = CollectionUtils.findFirst(classesDirs.getFrom(),Specs.SATISFIES_ALL);
if(firstClassesDir!= null){
return fileResolver.resolve(firstClassesDir);
}
返回null;
$ / code>因此,如果您的项目中有任何插件或
构建.gradle
调用DefaultSourceSetOutput.getClassesDir()
您会收到此警告。
正式推荐摆脱此警告的方法是:
sourceSets.main.output.classesDir = new File (buildDir,classes / main)
对应于:
@Override
public boolean isLegacyLayout(){
return classesDir!= null;
$ b @Override
public void setClassesDir(File classesDir){
setClassesDir((Object)classesDir);
}
@Override
public void setClassesDir(Object classesDir){
this.classesDir = classesDir;
this.classesDirs.setFrom(classesDir);
}
请注意 https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/SourceSetOutput .html#getClassesDir()将
另一个问题是测试文件。 如果您不喜欢有不同的层次结构布局(getClassesDir()
标记为已弃用。
<因此,在所有项目中使用的插件获得对Gradle 4.0的支持之前,您应该坚持解决方法,并在Gradle源代码构建脚本中忽略弃用。build / classes / main
和build / classes / java /测试
)您应该调整测试路径:
sourceSets.main.output.classesDir = new File (buildDir,classes / main)
sourceSets.test.output.classesDir = new File(buildDir,classes / test)
更新 IDEA的用户可能注意到IDE开始使用单独的
out
目录来构建Gradle 4。检测到x。如果您在IDEA之外运行应用程序,则无法热应用程序重新加载。要修复添加和重新导入:
子项目{
apply plugin:'idea'
//到期到Gradle 4.x更改(每个JVM语言单独输出目录)
// Idea开发人员拒绝重用Gradle类路径并使用自己的out /目录。
//恢复旧的行为以允许Spring Devtool使用快速的Idea编译器。
// https://youtrack.jetbrains.com/issue/IDEA-175172
//或者使用本机Gradle构建或bootRun.addResources = true
//要使用此功能,请按Ctrl + Shift + F9重新编译!
//请注意,Idea将资源放入类/目录中!
idea.module.inheritOutputDirs = false
idea.module.outputDir = sourceSets.main.output.classesDir
idea.module.testOutputDir = sourceSets.test.output.classesDir
}
请注意,IDEA会将资源放入
.class
文件,所以你的Gradle类路径将被破坏。只需为使用IDEA内置构建命令(Ctrl + Shift + F10等)的模块执行gradle clean
。Gradle 4.0 came out yesterday and I updated my project for it.
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
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
Related GitHub issue
Gradle Plugins:
- java
- eclipse
- idea
- org.springframework.boot
解决方案Gradle 4.0 introduces multiply sourceSet per JVM language in order to enable remote build cache. With
java
plugin yourbuild/classes/main
should becomebuild/classes/java/main
andbuild/classes/test
should becomebuild/classes/java/test
, etc.That warning is defined in:
@Override public File getClassesDir() { if (isLegacyLayout()) { return fileResolver.resolve(classesDir); } SingleMessageLogger.nagUserOfDeprecatedBehaviour("Gradle now uses separate output directories for each JVM language, but this build assumes a single directory for all classes from a source set"); Object firstClassesDir = CollectionUtils.findFirst(classesDirs.getFrom(), Specs.SATISFIES_ALL); if (firstClassesDir!=null) { return fileResolver.resolve(firstClassesDir); } return null; }
So if any plugin within your project or your
build.gradle
callDefaultSourceSetOutput.getClassesDir()
you get this warning.Officially recommended way to get rid of this warning is:
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 https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/SourceSetOutput.html#getClassesDir() marks
getClassesDir()
as deprecated.So until all used plugins in project get support for Gradle 4.0 you should stick to workaround and ignore deprecation in Gradle source code build script.
Another issue is test files. If you don't like to have different hierarchy layout (
build/classes/main
andbuild/classes/java/test
) you should adjust test path too:sourceSets.main.output.classesDir = new File(buildDir, "classes/main") sourceSets.test.output.classesDir = new File(buildDir, "classes/test")
UPDATE Users of IDEA may notice that IDE start using separate
out
directories for build it Gradle 4.x detected. That makes impossible hot app reloading if you run app outside of IDEA. To fix that add and reimport: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 put resources into same directory as
.class
files so your Gradle classpath would be corrupted. Just dogradle clean
for modules on which you use IDEA built-in build commands (Ctrl+Shift+F10, etc).这篇关于分割Gradle中的所有输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!