How to distribute javadoc along with my jar library artifact using maven repository?

245 views Asked by At

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?

1

There are 1 answers

1
attila-fazekas On

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.