Java › Swing

Generating and publishing Swing API docs with Dokka

Generate Kotlin and Java API docs with Dokka, add KDoc and module descriptions, and publish the output through GitHub Actions and Pages.

Generating and publishing Swing API docs with Dokka

I added API documentation to my mixed Java and Kotlin Swing project and published it with GitHub Pages. Dokka turned source comments into a browsable reference for the project.

This is the June 2024 setup, using Dokka 1.9.10 and Java 17. Newer Dokka versions have different Gradle conventions.

Installing the Dokka plugin

Javadoc documents Java APIs and KDoc documents Kotlin APIs. Dokka reads Kotlin declarations and source documentation and can build an API reference for a codebase that includes both languages. It can generate formats such as HTML and Markdown through the relevant Gradle tasks and format support.

1
2
3
plugins {
    id "org.jetbrains.dokka" version "1.9.10"
}

This project encountered a version conflict in the Dokka classpath at the time. The workaround pinned Jackson only for the Dokka configurations. That scope matters because it avoids changing the versions used by the Swing application’s regular runtime.

1
2
3
4
5
6
7
configurations.matching { it.name.startsWith("dokka") }.configureEach {
    resolutionStrategy.eachDependency {
        if (requested.group.startsWith("com.fasterxml.jackson")) {
            useVersion("2.15.3")
        }
    }
}

Generating and previewing output

Run the HTML task and inspect the output under build/dokka/html in this setup.

1
./gradlew dokkaHtml

The Markdown task is another option when the corresponding format support is configured.

1
./gradlew dokkaMarkdown

Generated Dokka API documentation Figure 1. The generated API reference in the Swing project.

Opening a page directly through file:// can interfere with features that load assets. Preview the generated directory through a local web server or through the deployed Pages site to check the navigation.

Documenting a class, package, and module

Use KDoc to explain what a type represents and to describe its public parameters and properties. That gives users context that is hard to infer from the declaration alone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * Represents a person.
 * @property name The person's name
 * @property age The person's age
 */
class Person(
    val name: String,
    val age: Int
) {
    /**
     * Prints the person's name.
     */
    fun printName() {
        println(name)
    }
}

Module and package documentation can group related declarations and explain conventions. The source text goes in a Markdown file configured as an include for Dokka.

1
2
3
# Package name

Describe the package and its responsibilities.
1
2
3
4
5
6
7
8
9
10
11
# Module TODO Swing Application

This module documents a desktop application written with Swing, Java, and Kotlin.

# Package org.example

Contains the application's main class.

# Package org.example.common

Contains shared classes, including font, color, and dimension constants and helper methods.

Building and publishing with GitHub Actions

The example workflow runs when the refactor branch changes. It checks out the source, installs Java, generates the HTML, and publishes the generated directory to a dedicated docs branch. The action needs write access to that branch.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
name: Docs CI
# Run when a commit is pushed to the refactor branch
on:
  push:
    branches: ["refactor"]

jobs:
  docs:
    runs-on: ubuntu-latest

    steps:
        # Set up Java 17
      - uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: "17"
          distribution: "temurin"

      - name: permissions
        run: chmod +x gradlew

        # Build the Dokka HTML reference
      - name: Build Documentation
        run: ./gradlew dokkaHtml

        # Publish the generated files to the docs branch
      - name: Deploy Documentation to GitHub Pages
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          BRANCH: docs
          FOLDER: build/dokka/html

In the repository’s Pages settings, pick branch deployment, select docs, and choose the root directory. Then open the published site and check that the sidebar works. The workflow’s branch and directory have to match the settings.

GitHub Pages settings selecting the docs branch Figure 2. The repository publishes the root of the generated docs branch.

Successful Dokka build and deployment in GitHub Actions Figure 3. GitHub Actions records the documentation build and branch update.

Verifying the published documentation

Open a package and a type through the generated sidebar. Confirm that their links and assets load on the deployed path, then put the documentation address in the README so users can find it. Published Swing API reference

Published Dokka reference with its navigation sidebar Figure 4. The published API reference shows the package navigation.

The existing README example uses a linked badge. The documentation URL can also work as a plain text link.

1
[![Dokka](https://img.shields.io/badge/Kdoc-7F52FF.svg?style=for-the-badge&logo=kotlin&logoColor=white)](https://cmsong111.github.io/Restaurant-information-system/)
This post is licensed under CC BY 4.0 by the author.