Why can't I use different sourceCompatibility/targetCompatibility with Hello World?

I want Gradle to use the Java 1.8 compiler for Hello World, but create bytecode targeting the Java 1.7 JVM. When I specify

sourceCompatibility = '1.8'
targetCompatibility = '1.7'

gradle build fails with

:compileJavajavacTask: target release 1.7 conflicts with default source release 1.8
 FAILED

The compiler doesn’t allow you to compile new language features back into an older target bytecode.

Usually, your source compatibility will be equal to or lower than the target compatibility (imagine setting the source compatibility to 1.5, but targeting 1.7) because you’ll build multiple jars or share source in some way.

If you’re running Gradle with JDK8, it’ll use the JDK8 compiler.

Per AbrarSyed’s suggestion on #gradle, I’m able to accomplish my goals with

.java-version:
1.8

build.gradle:

sourceCompatibility = ‘1.2’