Michael Barker
Michael Barker

Reputation: 14388

How do I define the source location for a file based dependency in gradle

I have a gradle build script that defines a file dependency.

dependencies {
    testCompile files('lib/test/wibble-1.0.jar')
}

I have a source jar for the library that I would like to add to the dependency so that with in eclipse I can navigate to the source. How do I add that information to the dependency?

Upvotes: 0

Views: 239

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123960

Adding source Jars for artifacts not resolved from a Maven repository requires some scripting of eclipse.classpath (see EclipseClasspath in the Gradle Build Language Reference). It could look like this:

import org.gradle.plugins.ide.eclipse.model.*

eclipse {
    classpath {
        file {
            whenMerged { Classpath classpath ->
                classpath.entries.each { ClasspathEntry entry ->
                    if (entry instanceof AbstractLibrary && entry.library.file == file("lib/test/wibble-1.0.jar")) {
                        entry.sourcePath = fileReferenceFactory.fromFile(file("lib/test/wibble-1.0-sources.jar"))
                    }
                }
            }
        }
    }
}

You could generalize this code to add all source Jars in the lib directory that adhere to some naming convention.

Upvotes: 1

Related Questions