Passing argument "-target 1.7" or "-source 1.7" to javac via compilerArgs results in "invalid flag" error from javac

I’m trying to pass compiler arguments to javac in order to build Java 7 binaries on a machine running Java 8 (OSX 10.10). The javac call I want to achieve is this:

javac -source 1.7 -target 1.7 my_code.java

This works fine when calling javac by hand. I found a piece of gradle configuration on the internet™ that I used in this minimal build script:

apply plugin: 'java'
  allprojects {
 tasks.withType(JavaCompile) {
  options.compilerArgs << "-target 1.7" << "-source 1.7"
  //options.compilerArgs << "-version"
  options.fork = true
 }
}
  sourceSets.main {
    java {
        srcDirs project.projectDir.getPath() exclude ("bin/**", "build*")
    }
}

[I’ve tested this in a minimal example. The original project consists of multiple gradle projects, so the “allprojects” block is necessary as I see things.]

The result is this:

:compileJava FAILED

FAILURE: Build failed with an exception.

  • What went wrong:

Execution failed for task ‘:compileJava’.

invalid flag: -target 1.7

  • Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 4.252 secs

Even just passing “-version” to javac results in the same error. How can I pass those arguments to javac - or, assuming this is the correct way, what is going wrong here?


Gradle 2.2.1


Build time:

2014-11-24 09:45:35 UTC

Build number: none

Revision:

6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a

Groovy:

2.3.6

Ant:

Apache Ant™ version 1.9.3 compiled on December 23 2013

JVM:

1.8.0_25 (Oracle Corporation 25.25-b02)

OS:

Mac OS X 10.10.1 x86_64

To set java target and source compatibility when compiling you can use ‘targetCompatibility’ and ‘sourceCompatibility’ project properties added by the ‘java’ plugin:

apply plugin: “java”

targetCompatibility = “1.7”

sourceCompatibility = “1.7”

This works, thank you! :slight_smile: