Include local javadoc with gradle

Hi there,
I am trying to figure out a way to show javadoc from a local .jar in my project, so I can see the documentation in intelliJ (and possible also NetBeans).

My build.gradle contains the following to include the local jars:

dependencies {
    compile fileTree(dir: "./src/libs/", includes: ['*.jar'])
}

and in the libs folder is three .jar files (compiled java, sources and javadoc) all for the same project.

What I cannot figure out it how get intelliJ to read the javadoc and show that. I made a small task with

task listJars {
    configurations.compile.each { File file -> println file.name }
}

to confirm that all the .jar files are included, which they are. The task outputs:

PickRandomStudent-1.0-javadoc.jar
PickRandomStudent-1.0-sources.jar
PickRandomStudent-1.0.jar

The -javadoc.jar was created in another project with the javadoc task.

I also have

apply plugin: 'idea'
idea {
    module {
        downloadJavadoc = true
    }
}

which is not really helping me in this case

Hope you guys can help!
/Kasper

The simplest way to achieve this is layout the jars in a maven style directory layout.

Eg:

mavenRepo/group/PickRandomStudent/1.0/PickRandomStudent-1.0-javadoc.jar
mavenRepo/group/PickRandomStudent/1.0/PickRandomStudent-1.0-sources.jar
mavenRepo/group/PickRandomStudent/1.0/PickRandomStudent-1.0.jar

You can then reference the dependency in your build

repositories {
   maven {
      url file('mavenRepo') 
    } 
} 
dependencies {
   compile 'group:PickRandomStudent:1.0'
}

This has the added benefit that the jars can now properly participate in Gradle’s dependency resolution process

1 Like

Ah yes! Thank you very much @Lance !

In IntelliJ 2018.3.3 with Gradle 5.1 this seems to be broken. I still have the three files locally and the jar file with the classes are correctly recognized and can be imported and used. The source and javadoc however are not.

In the ./idea/libraries folder that is created by IntelliJ i can find the file:
Gradle__dk_kaspersoerensen_PickRandomStudent_1_0.xml that looks like this:

<component name="libraryTable">
  <library name="Gradle: dk.kaspersoerensen:PickRandomStudent:1.0">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/lib/dk/kaspersoerensen/PickRandomStudent/1.0/PickRandomStudent-1.0.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
</component>

Clearly both <JAVADOC> and <SOURCES> are empty. Filling them out manually with the path to the -javadoc.jar and -sources.jar resolves the problem, but I am wondering if the idea plugin to Gradle should do this, when calling the idea task?

If I use external packages from the MavenCentral there is no problem downloading the sources and javadoc. These will both be listed in the ./idea/libraries folder.

EDIT
When using the ideaModule task, the .iml file does look correct, but the javadocs are still not available.

<?xml version="1.0" encoding="UTF-8"?>
<module relativePaths="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output/>
    <orderEntry type="inheritedJdk"/>
    <content url="file://$MODULE_DIR$/">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false"/>
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true"/>
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource"/>
      <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource"/>
      <excludeFolder url="file://$MODULE_DIR$/.gradle"/>
      <excludeFolder url="file://$MODULE_DIR$/build"/>
    </content>
    <orderEntry type="sourceFolder" forTests="false"/>
    <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="jar://$MODULE_DIR$/externalJars/dk/kaspersoerensen/PickRandomStudent/1.0/PickRandomStudent-1.0.jar!/"/>
        </CLASSES>
        <JAVADOC>
          <root url="jar://$MODULE_DIR$/externalJars/dk/kaspersoerensen/PickRandomStudent/1.0/PickRandomStudent-1.0-javadoc.jar!/"/>
        </JAVADOC>
        <SOURCES>
          <root url="jar://$MODULE_DIR$/externalJars/dk/kaspersoerensen/PickRandomStudent/1.0/PickRandomStudent-1.0-sources.jar!/"/>
        </SOURCES>
      </library>
    </orderEntry>
  </component>
  <component name="ModuleRootManager"/>
</module>

this burn me some hours: if you have an “aar”:

compile ‘group:PickRandomStudent:1.0@aar’

Since version 6.0, it’s necessary for the repositories block to look something like this:

repositories {
    maven {
        url file('mavenRepo) 
        metadataSources {
            artifact()
        }
    }
}

See here.

1 Like