In a Gradle Java project, the production code (Java code under the src / main / java
directory) is equivalent to Java 7 equivalent compilation (when you specify the -source 1.7 -target 1.7
option with the javac
command. ), But I want to compile the test code (Java code under src / test / java
) equivalent to Java 8! In such a case, I didn't know how to specify sourceCompatibility
or targetCompatibility
in build.gradle, so I tried to verify it.
I didn't have to specify different source / target versions for production and test code, but JUnit 5 requires Java 8 at runtime It seems to be current / user-guide / # overview-java-versions), so it is necessary to compile with Java 7 on the production code side, but I want to use JUnit 5 for the test code! I think that you can refer to this setting in such cases.
As you can see in the build.gradle example below
--Production code settings
--Specify normally
--Test code settings
--Specify in the properties of the compileTestJava
task
If so, it's OK.
apply plugin: 'java'
//⭐️ source applied to production code/Target compatibility settings
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
//⭐️ source that applies only to test code/Target compatibility settings
compileTestJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
//I tried using the sourceSets property as below, but this didn't work
//sourceSets {
// main {
// sourceCompatibility = JavaVersion.VERSION_1_7
// targetCompatibility = JavaVersion.VERSION_1_7
// }
// test {
// sourceCompatibility = JavaVersion.VERSION_1_8
// targetCompatibility = JavaVersion.VERSION_1_8
// }
//}
repositories {
mavenCentral()
}
dependencies {
// ...
}
Recommended Posts