How to compile a Java 9 project with Lombok in Gradle?

Asked

Viewed 214 times

8

TL;DR

How to pass multiple parameters -J--add-opens=<pacote>=ALL-UNNAMED so that Gradle uses them when calling javac?

Details

I have a code Helloworld.java using the Lombok in Java 9:

import lombok.NonNull;

public class HelloWorld {
    public static void main(@NonNull String[] args) {
        System.out.println("Hello World");
    }
}

The Lombok version is the unstable version edge (this link may change in the future, it is a version 1.16.19). I put the Lombok-edge.jar in the same folder as Helloworld.java above.

Lombok is not yet mature enough to be used in Java 9 due to the numerous difficulties that the modularization of Java 9 introduced. However, you can compile and run it anyway with due abuses on the command line:

javac -parameters -Xlint:all,-processing --add-modules=java.xml.ws.annotation -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED -cp lombok-edge.jar HelloWorld.java

With that, no Warning appears and the file Helloworld.class is generated. And then:

java HelloWorld

Produces that output:

Hello World

So far, so good, but now I wanted to do it at Gradle.

I tried with this file:

apply plugin: 'java'

sourceCompatibility = '1.9'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'HelloWorld'
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compileOnly files('libs/lombok-edge.jar')
    testCompileOnly files('libs/lombok-edge.jar')
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
    options.compilerArgs << "-parameters" << "-Xlint:all,-processing"
    options.compilerArgs << "--add-modules=java.xml.ws.annotation"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED"
    options.compilerArgs << "-J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
}

Obviously, I put the Lombok-edge.jar inside a briefcase libs.

While trying to give the build with Gradle 4.2:

gradle build

The result was this:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> invalid flag: -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED

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

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
1 actionable task: 1 executed

Once the command line compilation works, this shouldn’t be a problem in Lombok (the problem with it is needing this absurd and esoteric amount of command line parameters to work in Java 9). I believe the problem lies simply in the fact that I don’t know how to use Gradle properly, a problem that simply boils down to:

How to pass multiple parameters -J--add-opens=<pacote>=ALL-UNNAMED so that Gradle uses them when calling javac?

Edited: I also tried Gradle 4.3.1, and the result was even worse than Gradle 4.2:

> Task :compileJava FAILED
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.gradle.api.internal.tasks.compile.reflect.SourcepathIgnoringInvocationHandler (file:/C:/Users/d841156/.gradle/wrapper/dists/gradle-4.3.1-bin/7yzdu24db77gi3zukl2a7o0xx/gradle-4.3.1/lib/plugins/gradle-language-java-4.3.1.jar) to method com.sun.tools.javac.file.BaseFileManager.handleOption(java.lang.String,java.util.Iterator)
WARNING: Please consider reporting this to the maintainers of org.gradle.api.internal.tasks.compile.reflect.SourcepathIgnoringInvocationHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> invalid flag: -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED

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

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
1 actionable task: 1 executed

1 answer

3


Try the following:

apply plugin: 'java-library'

dependencies {
    compileOnly files("libs/lombok-edge.jar")
}

tasks.withType(JavaCompile) { 
    options.fork = true
    options.forkOptions.jvmArgs += [
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
          '--add-opens', 'jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED'
        ]
}

In accordance with that comment on Github this combination is working with lombok-edge, Gradle 4.2.1 and Java 9. The author of the commentary even claims to have achieved a build clean with the plugin java-library even after commenting on the forkOptions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.