Javadoc and --enable-preview

960 Views Asked by At

I am using Gradle 6.0.1 and JDK12 with preview features. Having the following configuration to be able to compile, run and test:

tasks.withType(JavaCompile) {
    options.compilerArgs += "--enable-preview"
}

tasks.withType(Test) {
    jvmArgs += "--enable-preview"
}

tasks.withType(JavaExec) {
    jvmArgs += '--enable-preview'
}

That works fine for all except javadoc generation that I defined as follows:

task generateJavadocs(type: Javadoc) {
    source = sourceSets.main.allJava
    options.jFlags("--enable-preview")
}

When running gradle generateJavadocs I receive compilation errors for the new switch expressions. Has anybody made it work with Javadoc?

2

There are 2 best solutions below

2
On BEST ANSWER

I had faced the same problem with preview features of JDK 14 (and Gradle 6.3). This has worked for me:

javadoc.options {
    addBooleanOption('-enable-preview', true)
    addStringOption('-release', '14')
}

In your case, try:

task generateJavadocs(type: Javadoc) {
    options {
        addBooleanOption('-enable-preview', true)
        addStringOption('-release', '12')
    }
}
1
On

This answer enhances Manfred's answer and is for anybody who is trying to set the javadoc args using gradle's kotlin api. It took me awhile to figure out that access to addStringOption and addBooleanOption requires a cast.

tasks.withType<Javadoc> {
    val javadocOptions = options as CoreJavadocOptions

    javadocOptions.addStringOption("source", "14")
    javadocOptions.addBooleanOption("-enable-preview", true)
}