Generate Javadoc Jar

Hello, World!

How would I go about making a project generate javadocs with a .jar output? With maven all I need to do is add the maven-javadoc-plugin and run “mvn javadoc:jar” and it is placed in the target/ folder.

What is the equivalent in Maven Gradle?

Thanks!

FTFY :wink:

Are you asking which task to run to generate javadoc?

Whenever I run the javadoc task or any of the publishXXX tasks (when the maven-publish plugin is applied), the docs are always bundled in a .jar file.

I think assemble generates javadoc too.

I don’t know much about Gradle so I’m unsure what exactly you’re talking about. On the command line when I run “gradle javadoc” it generates all of the documentation in .html, .js, and .css form in the directory: project_name/subproject_name/build/docs/javadoc/.

What it does not include is the .jar version of that same documentation. How do I make it so it does that? If it makes a difference, the project has subprojects that need to have their documentation generated.

Sorry my mistake. You’re right. javadoc on its own only produces the actual html.

Have you tried applying the 'maven-publish` plugin?

plugins{
    ...
    id 'maven-publish'
    ...
}
...
group = 'com.example'
version = '0.0.0'
...
task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    classifier = 'sources'
}

task javadocJar(type: Jar) {
    from javadoc
    classifier = 'javadoc'
}
...
publishing {
    publications {
        mavenJava(MavenPublication) {           
            artifactId = 'ziluckmichael'
            from components.java
            artifact sourcesJar
            artifact javadocJar
        }
    }
    repositories {
        maven {
            url 'repo'
        }
    }
}

…then gradle publish will get you your code, javadoc and sources jars in a repo directory as specified in the repositories{} stanza.

If you only want the javadoc, try removing the artifact sourcesJar and from components.java. I’ve never run that task without all three of them myself though. But it might work.

Thinking about the script I just posted above, I believe you can just run the javadocJar task on its own if you only want the javadoc and not the other two jars.

That will get you a jar in your project’s build/libs/ folder.

The docs explain it all better than I ever could.

@lingocoder
I went to go do this same thing again and Googled my problem and here I am!

For the more recent versions of Gradle, should I instead do this:
archiveClassifier.set("javadoc") rather than this: classifier = "javadoc"?

According to this

…and this, I believe you’ve absolutley nailed it, @ziluckmichael.

Although @ljacomet mentions „the Kotlin DSL“, it’s the same method in the Groovy DSL. I assume by him saying „DSL sugar“, that he’s also implying that it would be possible in the Groovy DSL to also do archiveClassifier = 'javadoc'.

Now then. Can you help me out on that request for functionality review. You know? I scratch your back? You scratch mine? :wink:

build.gradle.kts

plugins {
    id("java")
    id("maven-publish")
}

group = "io.github.goldfish07"
version = "0.1.0-rc1"

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

val sourcesJar by tasks.registering(Jar::class) {
    from(sourceSets["main"].allJava)
    archiveClassifier.set("sources")
}

val javadocJar by tasks.registering(Jar::class) {
    from(tasks.javadoc)
    archiveClassifier.set("javadoc")
}

repositories {
    mavenCentral()
    google()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")

}


publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            groupId = rootProject.group.toString()
            version = rootProject.version.toString()
            artifactId = "plugin"
            description = "your  description"
            from(components["java"])
            artifact(sourcesJar)
            artifact(javadocJar)

            pom {
            }
        }
    }

    repositories {
        mavenLocal()
        }
    }
}

This worked for me :kiss:

But that’s very unnecessary since long.
Just do

java {
    withSourcesJar()
    withJavadocJar()
}

Then you do not need to create the tasks manually and not need to add the artifacts manually to the publication which is bad practice anyway. :slight_smile:
You also don’t need to configure mavenLocal as publishing repository, you automatically get tasks to publish to maven local without any additional configuration necessary.