How to create JavaDoc twice (but with different options)?

For consistence I’d like to create JavaDoc twice but with different options and at different points in the lifecycle.

  • Once (temporarily) with JavadocMemberLevel.PRIVATE as a quality check that documentation of internals doesn’t get outdated. Ideally this step should fail early, eg. after compilation.
  • The second and final run would be to create JavadocMemberLevel.PROTECTED JavaDoc whcih can be considered as API documentation.

Any hints, how this could be done?

Got it running. That’s the code I came up with:

ext {
    // not all our proejcts are java projects
    javaProjects = subprojects.findAll { ... }

    javadocLink = "https://docs.oracle.com/javase/8/docs/api/"
}

configure(javaProjects) { project ->
    javadoc {
        source = sourceSets.main.allJava
        classpath = configurations.compileClasspath

        options {
            setMemberLevel JavadocMemberLevel.PROTECTED
            author = true
            charSet = 'UTF-8'
            encoding = 'UTF-8'
            docEncoding = 'UTF-8'
            use = true
            splitIndex = true
            noIndex = false
            noNavBar = false
            noTree = false

            links javadocLink
        }
    }

    task javadocCheck(type: Javadoc) {
        source = sourceSets.main.allJava
        source += sourceSets.test.allJava
        classpath = sourceSets.main.compileClasspath
        classpath += sourceSets.test.compileClasspath
        // if destinationDir is not changed, we might interfere with regular javadoc
        destinationDir = file("${buildDir}/tmp/checks/javadoc")

        // copy most options from regular javadoc task
        options {
            setMemberLevel JavadocMemberLevel.PRIVATE
            author = javadoc.options.author
            charSet = javadoc.options.charSet
            encoding = javadoc.options.encoding
            docEncoding = javadoc.options.docEncoding
            use = javadoc.options.use
            splitIndex = javadoc.options.splitIndex
            noIndex = javadoc.options.noIndex
            noNavBar = javadoc.options.noNavBar
            noTree = javadoc.options.noTree

            links javadocLink
        }
    }
    // the javadoc check should be done early on
    classes { finalizedBy javadocCheck }
}