如何指定"来源" JAR对于本地JAR依赖?

时间:2017-03-17 23:45:54

标签: java eclipse gradle dependency-management buildship

我的Gradle / Buildship项目中有*.jar个文件,该文件位于lib文件夹中。我将它包含在我的build.gradle via:

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

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

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

2 个答案:

答案 0 :(得分:5)

如果你想使用Buildship with Eclipse,那么你运气不好,因为gradle目前不支持这一点(参见https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/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导入项目到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大部分都像常规存储库一样。

答案 1 :(得分:0)

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

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