java gradle 目录,分割Gradle中的所有输出目录

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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值