2

I have a Kotlin JVM library, which I distribute using as a .jar artifact via maven repository. I would like to provide some Kdocs for it, so that it can be used via IntelliJ Idea's quick docs tool (ctrl+q). I use dokka gradle plugin to generate html docs and maven-publish plugin to distribute my artifact.

Here is my build.gradle.kts file's content:

import java.util.*

plugins {
    kotlin("jvm") version "1.8.0"
    id("org.jetbrains.dokka") version "1.8.10"
    `maven-publish`
}

group = "com.example"
version = "1.0"

fun RepositoryHandler.repsy(): MavenArtifactRepository{
    return maven(repoProperty("url")){
        credentials{
            username = repoProperty("username")
            password = repoProperty("password")
        }
    }
}

fun repoProperty(name: String): String = Properties().run {
    load(file("repo.properties").inputStream())
    get(name) as String
}

repositories {
    mavenCentral()
}

publishing{
    repositories{
        repsy()
    }
    publications{
        create("main", MavenPublication::class.java){
            artifactId = project.name
            version = project.version as String
            groupId = project.group as String
            artifact(file(".\\build\\libs\\${project.name}-${project.version}.jar"))
            artifact(file(".\\build\\dokka\\html\\index.html")){
                classifier = "javadoc"
            }
        }
    }
}

dependencies {
    testImplementation(kotlin("test"))
    implementation("com.google.code.gson:gson:2.10.1")
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(11)
}


And here are the contents of my repository: enter image description here

My artifact is resolved by gradle no problem. However, Idea is unable to resolve the docs. Also, html file cannot be navigated through correctly if downloaded separately from the repo. Dokka's generated files contain a lot of other stuff, but all the guides I found assure that I need to upload only the index.html file.

The question is, what am I doing wrong, and how to distribute dokka's generated kdocs correctly?

  • I'm not familiar with Dokka, does it actually generate Javadoc/KDoc-compatible output? IntelliJ probably won't be able to show just any HTML. – Jorn May 15 '23 at 12:57
  • @Jorn yes, it is an official Kotlin documentation engine, which should provide compatible html docs. However, I am not sure that it does not need any configuring. I was not able to find any info about configuring the plugin. – David Zhubrev May 15 '23 at 13:06

1 Answers1

1

Can you please try the following solution and let me know how it goes?

val javadocJar by tasks.register<Jar>("dokkaHtmlJar") {
    group = "documentation"
    dependsOn(tasks.dokkaHtml)
    from(tasks.dokkaHtml.flatMap { it.outputDirectory })
    archiveClassifier.set("javadoc")
}

val sourcesJar by tasks.named("sourcesJar")

publishing{
    repositories{
        repsy()
    }
    publications{
        create("main", MavenPublication::class.java){
            artifactId = project.name
            version = project.version as String
            groupId = project.group as String
            from(components["kotlin"])
            artifact(file(".\\build\\libs\\" +
                "${project.name}-${project.version}.jar"))          
            artifact(javadocJar)
            artifact(sourcesJar)
        }
    }
}

Have provided code for sourcesJar too, since that's also requirement if you wish to publish your library to Maven Central.

attila-fazekas
  • 128
  • 1
  • 1
  • 6
  • Thanks for your response! I found out that providing a sources jar alongside with an artifact jar will result in docs working properly, even if you do not distribute a javadoc jar. Although this is a solution, it also leads into distribution of the source code, which is not always wanted. – David Zhubrev Jul 26 '23 at 11:39