Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 300
Alan47 Avatar asked Mar 17 '17 23:03

Alan47


1 Answers

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.

like image 78
user1585916 Avatar answered Nov 02 '22 06:11

user1585916