ngranin
ngranin

Reputation: 301

Gradle create javadoc including subproject sources

I have the following project structure:

API/
└── client/
    ├── subA/
    │   └── build.gradle
    ├── subB/
    │   └── build.gradle
    └── build.gradle

And the following build.gradle inside client subproject:

task clientApiDocs(type: Javadoc) {
    source = sourceSets.main.allJava
    destinationDir = reporting.file("javadoc")
    classpath = configurations.compile
    options.tags = ["stereotype"]
    options.addStringOption('Xdoclint:none', '-quiet')
}

assemble.dependsOn(clientApiDocs)

distributions {
    main {
        contents{
            from (clientApiDocs.outputs) {
                into 'javadoc/ClientJava'
            }
        }
    }
    createStartScripts {
        applicationName = 'ClientJava'
    }
}

My goal is to include javadoc from subprojects subA and subB inside clientApiDocs. How can I do this?

Upvotes: 1

Views: 1130

Answers (1)

Lukas Körfer
Lukas Körfer

Reputation: 14493

The awesome io.freefair plugin collection provides a plugin called io.freefair.aggregate-javadoc that should suit your needs:

This plugin adds a aggregateJavadoc task to the project which will generate the aggregated javadoc for the project itself and all of its subprojects (which have the java plugin applied).

Just add the following code to the build.gradle file of your root project. Choose a version based on the compatibility matrix in the project README.

plugins {
    id 'io.freefair.aggregate-javadoc' version <version>
}

Internally, the plugin uses a regular task of type Javadoc. It gets configured using the following code:

project.allprojects(p ->
    p.getPlugins().withType(JavaPlugin.class, jp ->
        aggregateJavadoc.configure(aj -> {
            TaskProvider<Javadoc> javadoc = p.getTasks().named(JavaPlugin.JAVADOC_TASK_NAME, Javadoc.class);

            aj.source(javadoc.map(Javadoc::getSource));

            if (aj.getClasspath() instanceof ConfigurableFileCollection) {
                ((ConfigurableFileCollection) aj.getClasspath()).from(javadoc.map(Javadoc::getClasspath));
            }
            else {
                ConfigurableFileCollection classpath = project.files();
                classpath.from(aj.getClasspath());
                classpath.from(javadoc.map(Javadoc::getClasspath));
                aj.setClasspath(classpath);
            }

            StandardJavadocDocletOptions options = (StandardJavadocDocletOptions) javadoc.get().getOptions();
            StandardJavadocDocletOptions aggregateOptions = (StandardJavadocDocletOptions) aj.getOptions();

            options.getLinks().forEach(link -> {
                if (!aggregateOptions.getLinks().contains(link)) {
                    aggregateOptions.getLinks().add(link);
                }
            });
            options.getLinksOffline().forEach(link -> {
                if (!aggregateOptions.getLinksOffline().contains(link)) {
                    aggregateOptions.getLinksOffline().add(link);
                }
            });
            options.getJFlags().forEach(jFlag -> {
                if (!aggregateOptions.getJFlags().contains(jFlag)) {
                    aggregateOptions.getJFlags().add(jFlag);
                }
            });
        })
    )
);

Upvotes: 2

Related Questions