Search code examples
gradlegradle-kotlin-dsl

How to pass compiler arguments to java Compiler with kotlin dsl Gradle


I want to add arguments --add-exports java.rmi/sun.rmi.server=ALL-UNNAMED to java compiler for my gradle build task. I'm using gradle kotlin dsl.

Below is the updated JavaCompiler task.

tasks.withType<JavaCompile> {
    val compilerArgs = options.compilerArgs
    compilerArgs.add("--add-exports java.rmi/sun.rmi.server=ALL-UNNAMED ")
}

When I run gradle compileJava task I get below error.

  • What went wrong: Execution failed for task ':compileJava'.

error: invalid flag: --add-exports java.rmi/sun.rmi.server=ALL-UNNAMED

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

How can I fix this?


Solution

  • According to the javadoc, options.compilerArgs returns List<String>. So you can write like this instead:

    tasks.withType<JavaCompile> {
        val compilerArgs = options.compilerArgs
        compilerArgs.addAll(listOf("--add-exports", "java.rmi/sun.rmi.server=ALL-UNNAMED"))
    }
    

    Also make sure you have Java 9+.