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 by gradle 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 your build/classes/main should become build/classes/java/main and build/classes/test should become build/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 call DefaultSourceSetOutput.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);
}
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 and build/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 do gradle clean for modules on which you use IDEA built-in build commands (Ctrl+Shift+F10, etc).