简体   繁体   English

Gradle - 源代码和测试的 JDK 版本不同

[英]Gradle - Different JDK Version for Source and Test

How can I configure Gradle to compile test classes for a different Java version than source classes?如何配置 Gradle 为与源类不同的 Java 版本编译测试类?

I'm writing a project for Java 1.7, and I want to use Java 1.8 in my tests so I can use Lambda Behave.我正在为 Java 1.7 编写一个项目,我想在测试中使用 Java 1.8,这样我就可以使用 Lambda Behave。 In Maven this is pretty straightforward , but I can't see how to achieve the same result in Gradle. 在 Maven 中,这非常简单,但我看不出如何在 Gradle 中实现相同的结果。

Of course, immediately after posting the question I stumble across the answer! 当然,在发布问题后我立刻偶然发现了答案!

apply plugin: "java"

compileJava {
    sourceCompatibility = 1.7
}

compileTestJava {
    sourceCompatibility = 1.8
}

If you are using Kotlin DSL :如果您使用的是Kotlin DSL

plugins {
    `java`
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

tasks {
    compileTestJava {
        sourceCompatibility = JavaVersion.VERSION_1_8.toString()
        targetCompatibility = JavaVersion.VERSION_1_8.toString()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM