How reuse options.links for two javadocs tasks?


(Manuel Jordan) #1

In Gradle about javadoc I have two tasks masterMainJavadoc and masterTextJavadoc.
All are practically the same. Just one for main and the other for test. For the masterMainJavadoc the code is as follows:

task masterMainJavadoc(type: Javadoc, group: "Documentation") {
	description = 'Generates a master javadoc from all the modules (main)'
    source exportedProjects.collect { project(it).sourceSets.main.allJava }
    classpath = files(exportedProjects.collect { project(it).sourceSets.main.compileClasspath })
    options.memberLevel = JavadocMemberLevel.PRIVATE
    options.addStringOption("charset", "UTF-8")
    options.links 'https://docs.oracle.com/javase/8/docs/api/'
    options.links 'https://docs.oracle.com/javaee/7/api'
   ... more links
    destinationDir = file("${buildDir}/docs/javadoc/main")
}

Because both tasks have the same options.links. I have tried the following:

ext.exportedUrlsJavadoc = [
	
	"http://docs.oracle.com/javase/8/docs/api/",
    "http://docs.oracle.com/javaee/7/api"
	
]

task masterTestJavadoc(type: Javadoc, group: "Documentation") {
	description = 'Generates a master javadoc from all the modules (test)'
    source exportedProjects.collect { project(it).sourceSets.test.allJava }
    classpath = files(exportedProjects.collect { project(it).sourceSets.test.compileClasspath})
    options.memberLevel = JavadocMemberLevel.PRIVATE
    options.addStringOption("charset", "UTF-8")
    options.links exportedUrlsJavadoc.collect {it}
    destinationDir = file("${buildDir}/docs/javadoc/test")
}

Observe: options.links exportedUrlsJavadoc.collect {it}
But it throws

What went wrong:
A problem occurred evaluating root project web.
> [Ljava.lang.String; cannot be cast to java.util.List

If I use now {} instead of []

ext.exportedUrlsJavadoc = {
	
	"http://docs.oracle.com/javase/8/docs/api/",
        "http://docs.oracle.com/javaee/7/api"
	
}

The error is:

build.gradle': 127: unexpected token: http://docs.oracle.com/javase/8/docs/api/ @ line 127, column 2.
        "http://docs.oracle.com/javase/8/docs/api/",
      ^

  1 error

Thus, what is the correct configuration?


(James Justinic) #2

Your original declaration of ext.exportedUrlsJavadoc was correct. Using [] in groovy gives you a List.

ext.exportedUrlsJavadoc = [
    'http://docs.oracle.com/javase/8/docs/api/',
    'http://docs.oracle.com/javaee/7/api'
]

You can either use the setter / property access to set the List you already have:

task masterTestJavadoc(type: Javadoc, group: 'Documentation') {
    // ...
    options.links = exportedUrlsJavadoc
    // ...
}

or use the links(String...) method, which requires you to use the spread operator (*) to convert from the List to varargs. The parenthesis around the argument are not optional in this case:

task masterTestJavadoc(type: Javadoc, group: 'Documentation') {
    // ...
    options.links(*exportedUrlsJavadoc)
    // ...
}

(Manuel Jordan) #3

Thank you. I have tested both approaches.