我的 Gradle/Buildship 项目中有一个 *.jar
文件,它位于 lib
文件夹中。我通过以下方式将其包含在我的 build.gradle
中:
编译文件('libs/local-lib.jar')
我还有一个相应的local-lib-sources.jar
文件,我想附加到它。在 Eclipse 中,对于手动管理的依赖项,这通过构建路径条目 ->
Properties ->
Java Source Attachment 的上下文菜单起作用。但是,对于 gradle 管理的依赖项,该选项不可用。
有谁知道用 gradle/buildship 做这件事的方式是什么样子的?我的依赖项不在存储库中,所以我现在只能使用编译文件
。
最佳答案
如果您想将 Buildship 与 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 -> "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'
由于 flatDir
在大多数情况下就像一个常规存储库,因此 Buildship 将自动找到 -sources.jar
文件。
关于java - 如何为本地 JAR 依赖项指定 "sources"JAR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868749/