50

I have a Gradle-managed multi-project setup that relies on the new Java 8 -parameters compiler flag. I need 2 ways of including the compiler flag:

  • To test classes only (the main project should compile without parameter names attached).
  • To all compiled sources.

I've tried this:

  tasks.withType(JavaCompile) {
    options.compilerArgs << '-parameters'
    options.fork = true
    options.forkOptions.executable = 'javac'
  }

...but it does not seem to be working properly.

6 Answers 6

52

You should use standard way of configuring Java compile plugin:

apply plugin: 'java'

compileJava {
    options.compilerArgs << '-parameters'
}
Sign up to request clarification or add additional context in comments.

2 Comments

The code snippet works (of course), but the comment supplied is somewhat... So, the "standard" is actually the totally opposite to what Gradle guys recommend. The requester is using much closer to using configuration avoidance, and configuring task for all sourceSets, etc. (compileJava affect only main sourceSet). The "accepted" answer is nothing of that, the answer below is much much much better. @JustACluelessNewbie reconsider. ) Gradle experts make some up/down votes. Sorry for criticising comment. )
But the answers below don't seem relevant to the question of how to pass -parameters they're referencing Spring, Dagger or a release version instead?
13

For Android projects, one can add e.g. the below in the gradle android scope.

// Used to get more info from dagger regarding binding compile errors
// see https://github.com/google/dagger/wiki/Dagger-2.17-@Binds-bugs
tasks.withType(JavaCompile) {
    options.compilerArgs += ["-Adagger.floatingBindsMethods=enabled"]
}

Comments

2

I had to add support for parameters in a kotlin project. The way to do it was to set the javaParameters kotlin option

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_11.toString()

    javaParameters = true
}

2 Comments

Close, but not exactly. Full configuration in gradle kotlin is tasks.compileKotlin { kotlinOptions { javaParameters = true } }
@MiguelGamboa you're right, your snippet works for all compile tasks. My snippet was specific to an android project
2

When using a Kotlin config file for a Java project, with a build.gradle.kts file, the above suggestions did not work. This did:

tasks.withType<JavaCompile> {
  // Support Beam's @JavaBeanSchema with @SchemaCreate all-args constructors
  options.compilerArgs.add("-parameters")
}

Comments

1

Answer from @Crazyjavahacking is correct

Also, check that you doesn't redifine it, like me, in a subproject that uses others args (mapstruct in my case) :

options.compilerArgs = ['-Amapstruct.defaultComponentModel=spring'] // do not do this

Always append the args options.compilerArgs << '-Amapstruct.defaultComponentModel=spring'

Hope it can save some time to someone else, i personnaly lost 2 hours since i totally forgot this line in the subproject and was concentrating on the main build.gradle

Comments

-3
compileJava {
  options.compilerArgs.addAll(['--release', javaVersion])
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.