10

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.

Martin Häusler
  • 6,544
  • 8
  • 39
  • 66
  • 1
    Have you tried? http://stackoverflow.com/questions/10156847/how-to-tell-gradle-to-download-all-the-source-jars – Adonis Mar 19 '17 at 01:40

2 Answers2

7

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 = it.@path.replace('.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.

user1585916
  • 793
  • 7
  • 15
  • Thanks for pointing this out. I actually encountered the topic you linked when I was searching for a solution, but I thought "The topic is one year old, surely there must have been a fix for it since then..." - apparently, I was wrong. – Martin Häusler Mar 23 '17 at 00:34
  • The 1st approach seems to be no longer working. If I look at the .classpath, it's not expanded anymore and has a single entry . Can you confirm ? The 2nd one worked for me. – Sybuser Feb 14 '22 at 17:52
0

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
 }
Incognito
  • 133
  • 1
  • 10
  • 1
    I should have mentioned that in my question (sorry about that): while your code works and does indeed add the "sources.jar" to the classpath of the Eclipse Project, it does not "link" the sources. So if I step into a class that is contributed by the *.jar, unfortunately I still don't see the corresponding file from the *-sources.jar, but the Eclipse *.class file (bytecode) viewer. – Martin Häusler Mar 18 '17 at 00:04
  • So in éclipse go to eclipse market place and have a look to "java decompiler" plugin. You will see source code now – Incognito Mar 18 '17 at 00:10
  • 1
    I am looking for a solution to this issue as well. The Java Decompiler plugin is not a complete solution since it does not provide javadoc information. – user1585916 Mar 22 '17 at 18:32
  • Try this n your gradle file : apply plugin: 'java' apply plugin: 'eclipse' eclipse { classpath { downloadJavadoc = true downloadSources = true } – Incognito Mar 22 '17 at 19:35