Reputation: 1750
Here is my situation, I have a java project and some unit tests to go with it.
My unit tests requests java 1.8 because they are using Stream and Arguments.of
My java sources needs to be built with java 1.7 because they are to be imported in Matlab - and unfortunately Matlab does not understand java 1.8
Do I have a solution to make the two work together ?
Upvotes: 3
Views: 2178
Reputation: 1
Since Gradle 6.7 there is possibility to set a java version using Java toolchains - https://blog.gradle.org/java-toolchains
Upvotes: 0
Reputation: 159096
You need to specify the targetCompatibility
when you compile. This is normally specified at the root level of your Gradle build file, but since you want the test code to be a different version, you need to specify it explicitly for the compileJava
task.
You should also specify a Java 7 Runtime library, so assuming you run Gradle with Java 8, your build file would be something like this:
apply plugin: 'java'
// options used by all compile tasks unless overridden, e.g. used by compileTestJava
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava {
// override global options just for this task
sourceCompatibility = 1.7
targetCompatibility = 1.7
options.bootClasspath = 'path/to/java7/lib/rt.jar'
}
// and all your other stuff
Upvotes: 1
Reputation: 44952
You should be able to build the project with JDK 8 and set different compatibility levels. As per The Java Plugin > Other convention properties you could try adding below to build.gradle
:
plugins {
id 'java'
}
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
compileTestJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Java strives to maintain backwards compatibility. As long as you won't hit a bug that was fixed in JDK 8 but is still present in JDK 7 you should be ok.
Upvotes: 2