Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Api Sources and Doc when writing Gradle Plugins

Tags:

eclipse

gradle

The question:

How do I get the sources and javadoc/groovydoc for the gradle-api code integrated into an Eclipse project?

Background:

I'm using Gradle to build a Gradle-plugin that I'm writing. I'm using Eclipse as an IDE for this project and my Gradle script for building this plugin is using the 'Eclipse' plugin to generate my Eclipse project. Also, I'm using Spring's Gradle plugin for Eclipse which grabs all my dependencies from my build.gradle file.

The dependencies block for this Gradle script has

dependencies {
    compile localGroovy()
    compile gradleApi()
    // I want something like: 'compile gradleApiSources()' here
    // I want something like: 'compile gradleApiDoc()' here as well
}

Justification:

As I'm learning to write Gradle plugins, it would be helpful to be able to see the documentation and even implementation for Gradle to help me learn what I'm doing.

like image 532
MrQBerrt Avatar asked Mar 27 '14 16:03

MrQBerrt


People also ask

How do I add plugins to Gradle?

To add a Gradle plugin with dependencies, you can use code similar to the following: Plugin DSL GA versions. Plugin DSL non GA versions. Legacy Plugin Application.

What is source and target compatibility in Gradle?

According to Gradle documentation: sourceCompatibility is "Java version compatibility to use when compiling Java source." targetCompatibility is "Java version to generate classes for."

What is difference between API and implementation in Gradle?

In the first scenario, LibraryD is compiled by using api . If any change is implemented inside LibraryD, gradle needs to recompile LibraryD, LibraryB and all other modules which import LibraryB as any other module might use implementation of LibraryD.

How do plugins work in Gradle?

A plugin is simply any class that implements the Plugin interface. Gradle provides the core plugins (e.g. JavaPlugin ) as part of its distribution which means they are automatically resolved. However, non-core binary plugins need to be resolved before they can be applied.


2 Answers

This works for me in Eclipse:

plugins.withType(EclipsePlugin) {
    plugins.withType(JavaBasePlugin) {
        eclipse {
            classpath {
                file {
                    whenMerged { classpath ->
                        String gradleHome = gradle.getGradleHomeDir()
                            .absolutePath
                            .replace(File.separator, '/')
                        String gradleSourceDirectory = "${gradleHome}/src"
                        classpath.entries.each { entry ->
                            if (entry in org.gradle.plugins.ide.eclipse.model.AbstractLibrary
                                    && entry.library.path.contains('generated-gradle-jars')) {
                                entry.sourcePath =
                                    new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory()
                                        .fromPath(gradleSourceDirectory)
                            }
                        }
                    }
                }
            }
        }
    }
}

Make sure that your Gradle Home contains the source directory. If you use the wrapper this can be done by updating the distributionUrl to an -all version in the wrapper task.

You will also need to stop the Gradle daemons that are running otherwise they will keep their own "home": ./gradlew --stop, then you can go ahead and run the task: ./gradlew eclipse

See GRADLE-2133 (which this answer was adapted from)

like image 132
Heinrich Filter Avatar answered Sep 28 '22 03:09

Heinrich Filter


I don't have an answer for eclipse but I can tell you how I do this for intellij which might give you some inspiration. It would be nice if this were available more easily.

private void addGradleSourceDeps() {
    PathFactory pf = new PathFactory()
    pf.addPathVariable('GRADLE_HOME', project.gradle.gradleHomeDir)
    project.extensions.idea.module.iml.whenMerged { Module module ->
        module.dependencies.grep {
            it instanceof ModuleLibrary && ((ModuleLibrary) it).classes.grep { Path path ->
                path.relPath.substring(path.relPath.lastIndexOf('/') + 1).startsWith('gradle-')
            }
        }.each { ModuleLibrary lib ->
            // TODO this needs to be fixed for gradle 1.9 which now includes separate sub directory for each jar
            // for now a workaround is to execute the following
            // cd $GRADLE_HOME
            // for each in $(find . -mindepth 2 -maxdepth 2 -type d ! -name META\-INF); do cp -a ${each} .;done
            lib.sources.add(pf.path('file://$GRADLE_HOME$/src'))
        }
        module.dependencies.grep {
            it instanceof ModuleLibrary && ((ModuleLibrary) it).classes.grep { Path path ->
                path.relPath.substring(path.relPath.lastIndexOf('/') + 1).startsWith('groovy-all')
            }
        }.each { ModuleLibrary lib -> lib.sources.add(pf.path('file://$GROOVY_SRC_HOME$')) }
    }
}

This relies on me having installed a gradle src distribution into a location available via the GRADLE_HOME path variable in intellij (and similar for GROOVY_SRC_HOME). You can also see my plugin currently uses gradle 1.8, the src layout changed in 1.9 so I need to fix this when I upgrade.

like image 38
Matt Avatar answered Sep 28 '22 01:09

Matt