9

我的*.jarGradle / Buildship 项目中有一个文件位于lib文件夹中。我将它包含在我的build.gradle通过:

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

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

有人知道执行此操作的 gradle/buildship 方式是什么样的吗?我的依赖项不在存储库中,所以我现在被困住了compile files

4

2 回答 2

6

如果您想将 Buildship 与 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
        }
      }
    }
  }
}

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

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

repositories {
    flatDir { 
        dirs 'lib'
}

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

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

compile ':local-lib'

这样,Buildship 将自动查找-sources.jar文件,因为flatDir大部分情况下它就像一个常规存储库。

于 2017-03-22T19:44:33.240 回答
0

在与 src 或您构建脚本相同的目录级别上使用名为 lib 或类似的额外文件夹。

dependencies {
//local file
     compile files('lib/local-lib-sources.jar')
// others local or remote file
 }
于 2017-03-17T23:55:47.357 回答