Search code examples
kotlingradlegradle-kotlin-dsl

Gradle Kotlin DSL equivalent for Groovy DSL compilerArgs for compileJava?


For the Gradle Java plugin, what is the Kotlin DSL equivalent for the following Groovy DSL?

compileJava { 
  options.compilerArgs += [ 
    '-Amapstruct.suppressGeneratorTimestamp=true', 
    '-Amapstruct.suppressGeneratorVersionInfoComment=true', 
    '-Amapstruct.verbose=true'
  ]
}

Solution

  • compilerArgs is a Java List<String>, which in Kotlin is mapped to a MutableList<String>.

    There are a few different ways to add elements to a mutable list in Kotlin. One option is to use addAll()

    tasks.compileJava {
      options.compilerArgs.addAll(
        listOf(
          "-Amapstruct.suppressGeneratorTimestamp=true",
          "-Amapstruct.suppressGeneratorVersionInfoComment=true",
          "-Amapstruct.verbose=true",
        )
      )
    }