如何为本地JAR依赖指定"sources"JAR?

Ala*_*n47 7 java eclipse dependency-management gradle buildship

*.jar我的Gradle/Buildship项目中有一个文件位于一个lib文件夹中.我把它包含在我的build.gradle通道中:

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

我还有一个相应的local-lib-sources.jar文件,我想附加到它.在Eclipse中,对于手动管理的依赖项,这可以通过构建路径条目->属性->Java源附件的上下文菜单来工作.但是,对于gradle-managed依赖项,该选项不可用.

有没有人知道gradle/buildship这样做的方式是什么样的?我的依赖是没有存储库,所以我现在一直坚持compile files.

use*_*916 5

如果你想使用Buildship with Eclipse,那么你运气不好,因为gradle目前不支持它(参见https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-缺乏他们/ 11456/8).

如果您没有使用Buildship并手动生成Eclipse点文件,则可以在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
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,您将从命令行运行gradle eclipse并使用Import - >"Existing Projects into Workspace"将项目导入Eclipse

另一个(可能更好)选项是使用这样的平面文件存储库:

repositories {
    flatDir { 
        dirs 'lib'
}
Run Code Online (Sandbox Code Playgroud)

请参阅https://docs.gradle.org/current/userguide/dependency_management.html#sec:flat_dir_resolver

然后,您可以像其他任何一样包含您的依赖项; 在你的情况下:

compile ':local-lib'
Run Code Online (Sandbox Code Playgroud)

这样,Buildship将自动查找-sources.jar文件,因为flatDir大部分内容都像常规存储库一样.