Generating Dokka Javadoc from CLI

It took a while but I was able to use gradle to generate javadoc documentation for my kotlin source. This is probably not an optimal solution but it works. What I would up doing was taking build.gradle.kts and settings.gradle.kts from the Kotlin example project and commenting out the sourceLink section since I wasn’t building documentation from code hosted online:

// settings.gradle.kts

pluginManagement {
    repositories {
        gradlePluginPortal()
        jcenter()
        mavenCentral()
        maven("https://dl.bintray.com/kotlin/kotlin-eap")
        maven("https://maven.pkg.jetbrains.space/kotlin/p/dokka/dev")
    }
}

rootProject.name = "dokka-gradle-example"
// build.gradle.kts

import org.jetbrains.dokka.gradle.DokkaTask
import java.net.URL

plugins {
    kotlin("jvm") version "1.4.0"
    id("org.jetbrains.dokka") version ("1.4.0")
}

repositories {
    mavenCentral()
    jcenter()
    maven("https://dl.bintray.com/kotlin/kotlin-eap")
    maven("https://maven.pkg.jetbrains.space/kotlin/p/dokka/dev")
}

dependencies {
    implementation(kotlin("stdlib"))
    testImplementation(kotlin("test-junit"))
}

tasks.withType<DokkaTask>().configureEach {
    dokkaSourceSets {
        named("main") {
            moduleDisplayName.set("Dokka Gradle Example")
            includes.from("Module.md")
            // sourceLink {
            //     localDirectory.set(file("src/main/kotlin"))
            //     remoteUrl.set(URL("https://github.com/Kotlin/kotlin-examples/tree/master/" +
            //             "gradle/dokka/dokka-gradle-example/src/main/kotlin"
            //     ))
            //     remoteLineSuffix.set("#L")
            // }
        }
    }
}

I was then able to create a Dockerfile to containerize the build:

# dokka.dockerfile
FROM ubuntu:18.04

# base dependencies
RUN apt-get update \ 
    && apt-get install -y default-jdk-headless gradle \
    && rm -rf /var/lib/apt/lists/*

# gradle wrapper
COPY settings.gradle.kts /root/dokka/settings.gradle.kts
RUN cd /root/dokka \
    && gradle wrapper --gradle-version 6.5.1 \
    && ./gradlew

# dokka javadoc dependencies
COPY build.gradle.kts /root/dokka/build.gradle.kts
RUN cd /root/dokka \
    && ./gradlew dokkaJavadoc \
    && ./gradlew clean

and build with:

docker build -t dokka -f dokka.dockerfile .

From a directory containing my source in src/com… I was able to generate the javadoc:

docker run -it --rm \
    -v $(pwd)/src/com:/root/dokka/src/main/kotlin/com:ro \
    -v $(pwd)/javadoc:/root/dokka/build/dokka/javadoc \
    -w /root/dokka \
    dokka ./gradlew dokkaJavadoc