sparkonhdfs
sparkonhdfs

Reputation: 1343

Gradle does not generate Javadocs

I am writing a build file with Gradle to do Java build operations. However, Gradle does not generate Javadocs for my project. According to Gradle.org's documentation, to implement a Javadocs task in Gradle, the source and classpath have to be specified.

apply plugin: 'java'

javadoc {
source = sourceSets.main.allJava
classpath = configurations.compile
}

However, when I run the command gradle javadoc, or gradle build, the default folder for javadocs (build\docs) is never created, so no html files are generated for the project. What can I do to fix this?

Upvotes: 8

Views: 21473

Answers (5)

papigee
papigee

Reputation: 7408

Just to add a little more structure to the answers, these are the configs needed to generate Javadoc (using Gradle version 4 and 5).

1. Define your sourceSet (might be optional if the standard Maven directory structure is used). Assuming this directory structure:

├── src
│   └── main
│       ├── java
├── src
│   └── test
│       ├── java

Define sourceSet like this

sourceSets {
    main {
        java {
               srcDirs =['src/main/java']
    }
    test {
        java {
               srcDirs =['src/test/java']
    }
}

2. Configure classpath to be used when generating javadoc. If this is not done, it will result in errors similar to this: error: package org.xxxx does not exist.

Assuming your dependency is defined like this:

dependency {
   compile group: 'xxxx', name: 'yyyy', version: 'zzzz'
   testCompile group: 'aaaa', name: 'bbbb', version: 'cccc'
}

define javadoc task this way

tasks.withType(Javadoc){
source = sourceSet.main.java.srcDirs
classpath +=configuration.compile
destinationDir = reporting.file("myJavaDoc") //optional
}

If you want to generate Java doc for test, the task will look like this:

tasks.withType(Javadoc){
source = sourceSet.test.java.srcDirs
classpath +=configuration.compile
classpath +=configuration.testCompile
destinationDir = reporting.file("myJavaDoc") //optional
}

Upvotes: 0

Abhijit Maity
Abhijit Maity

Reputation: 467

If include closure is not working the add sourceSet closure with srcDir pointing to the module/java directory.

The below example is properly working for me. Here java directory is src/main/java , where I have all my packages.

sourceSets {
    build {
        java.srcDir file('src/integrationTest/java')
    }
}

javadoc {
    source = sourceSets.main.allJava
    classpath = configurations.compile
}

The run $gradle javadoc

Upvotes: 0

sver
sver

Reputation: 942

You can write a gradle task of type Javadoc to create javadocs like this :

 task createJavadocs (type: Javadoc)
      {
            source = project.android.sourceSets.main.java.srcDirs
            options.linkSource true
            classpath += project.files(project.android.getBootClasspath().join(File.pathSeparator))
            failOnError false
      }

To create Javadocs, simply run this task.

Upvotes: 1

tintin
tintin

Reputation: 5867

Seems like your directory structure is is not a standard src/main/java. If that is the case then you need to specify the include pattern as part of the include closure, something like this:

javadoc {
source = sourceSets.main.allJava
classpath = configurations.compile
}
include **/your/directory/structure/*

Upvotes: 2

centerback
centerback

Reputation: 629

For various reasons we created a custom classpath in gradle for our java project. Mainly as we wanted to split out dependancies into those provided at runtime from those that weren't.

So we set up build.gradle like this

configurations {
  providedCompile
}

dependencies {

  providedCompile  'provided1', 'provided2

  providedCompile  'provided3'

  compile 'compile1'
  compile ('compile2')   
  { 
        exclude group: 'unwanted part'
  }

}
sourceSets.main.compileClasspath += configurations.providedCompile
javadoc.classpath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile

We needed to add the final part in order for gradle to correctly pick up the classpath for complying, testing AND running javadoc.

Upvotes: 0

Related Questions