44

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.

Czyzby
  • 2,999
  • 1
  • 22
  • 40

5 Answers5

48

You should use standard way of configuring Java compile plugin:

apply plugin: 'java'

compileJava {
    options.compilerArgs << '-parameters'
}
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
  • 5
    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. ) – SoBeRich Oct 28 '19 at 18:38
  • 2
    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? – mjaggard Oct 17 '22 at 11:36
12

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"]
}
jayeffkay
  • 1,229
  • 1
  • 16
  • 22
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
}
Goldorak84
  • 3,714
  • 3
  • 38
  • 62
  • Close, but not exactly. Full configuration in gradle kotlin is `tasks.compileKotlin { kotlinOptions { javaParameters = true } }` – Miguel Gamboa Feb 14 '23 at 13:30
  • @MiguelGamboa you're right, your snippet works for all compile tasks. My snippet was specific to an android project – Goldorak84 Feb 16 '23 at 21:07
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

ulk200
  • 369
  • 3
  • 5
-3
compileJava {
  options.compilerArgs.addAll(['--release', javaVersion])
}
Maayan Hope
  • 1,482
  • 17
  • 32