Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I attach Javadocs for a Gradle dependency in Eclipse?

Tags:

eclipse

gradle

My Gradle project is pulling in some jar dependencies from file like this:

dependencies {
    compile fileTree(dir: 'lib', include: '*.jar')
}

I have downloaded the Javadocs for one of the dependencies as a zip file, how do I add the Javadocs in Eclipse?

When I right click on the Gradle dependency and try to add Javadocs I see this:

The current class path entry belongs to container 'Gradle Dependencies (persisted)' which does not allow user modifications to Javadoc locations on its entries.

like image 429
Chris K Avatar asked Sep 28 '22 00:09

Chris K


2 Answers

It may be a bit complicated for a file jar. But if you are able to get the dependency from repo, then this is the way to go:

repositories {
  mavenCentral()
}

dependencies {
  compile 'javax.servlet:javax.servlet-api:3.1.0'
}

eclipse.classpath.downloadJavadoc = true
eclipse.classpath.downloadSources = false

After I do gradle cleanEclipse eclipse I get something like that in .classpath:

<classpathentry kind="lib" path="C:/.../javax.servlet-api-3.1.0.jar">
  <attributes>
    <attribute name="javadoc_location" value="jar:file:/C:/.../javax.servlet-api-3.1.0-javadoc.jar!/"/>
    <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
  </attributes>
</classpathentry>
like image 50
Jarekczek Avatar answered Oct 06 '22 19:10

Jarekczek


You have to add the javadoc zip via eclipse closure. It could be achieved using the following code by

eclipse {
    classpath {
        file {
            whenMerged { cp ->
                // Add sources to a classpath entry
                def fileReferenceFactory = new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory()

                def entry = cp.entries.find{ entry -> entry.path.endsWith('$YOUR_JAR.jar') }
                // add javadoc jar
                entry.javadocPath = fileReferenceFactory.fromPath(file('$JAVADOC_FOLDER/$JAVADOC_FILE.zip'))
            }
        }
    }
}

Btw. I would suggest to add the a *-sources.jar as this also makes the javadoc hints available if it is possible. This can be done using the above code and

// add sources jar
entry.sourcePath = fileReferenceFactory.fromPath(file('$SOURCES_FOLDER/$SOURCE_FILE.jar')

instead of entry.javadocPath.

like image 24
Andreas Schmid Avatar answered Oct 06 '22 18:10

Andreas Schmid