Martin Häusler
Martin Häusler

Reputation: 7286

How to specify "sources" JAR for local JAR dependency?

I have a *.jar file in my Gradle / Buildship project that resides in a lib folder. I include it in my build.gradle via:

compile files('libs/local-lib.jar')

I also have a correspondinglocal-lib-sources.jar file that I would like to attach to it. In Eclipse, for manually managed dependencies this works via context menu of build path entry -> Properties -> Java Source Attachment. However, for gradle-managed dependencies, that option is not available.

Does anybody know how what the gradle/buildship way to do this looks like? My dependency is in no repository, so I'm stuck with compile files for now.

Upvotes: 11

Views: 5049

Answers (2)

user1585916
user1585916

Reputation: 851

If you want to use Buildship with Eclipse then you are out of luck since this is not currently supported by gradle (see https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/8).

If you are ok with not using Buildship and manually generating the Eclipse dot files you can do something like this in your build.gradle:

apply plugin: 'eclipse'

eclipse.classpath.file {
  withXml {
    xml ->
    def node = xml.asNode()
    node.classpathentry.forEach {
      if(it.@kind == 'lib') {
        def sourcePath = [email protected]('.jar', '-sources.jar')
        if(file(sourcePath).exists()) {
          it.@sourcepath = sourcePath
        }
      }
    }
  }
}

You would then run gradle eclipse from the command line and import the project into Eclipse using Import -> "Existing Projects into Workspace"

Another (possibly better) option would be to use a flat file repository like this:

repositories {
    flatDir { 
        dirs 'lib'
    }
}

see https://docs.gradle.org/current/userguide/dependency_management.html#sec:flat_dir_resolver

You would then just include your dependency like any other; in your case:

compile ':local-lib'

This way Buildship will automatically find the -sources.jar files since flatDir acts like a regular repository for the most part.

Upvotes: 7

Incognito
Incognito

Reputation: 133

Use an extra folder called lib or similar on the same directory level as src or you build script.

dependencies {
//local file
     compile files('lib/local-lib-sources.jar')
// others local or remote file
 }

Upvotes: 0

Related Questions