首页 > 解决方案 > 在 Gradle 中实现 IntelliJ“选择源”对话框的结果

问题描述

语境

我正在为具有相当复杂的 Gradle 文件系统的 Java 项目 (Ghidra) 构建扩展。IntelliJ 正确地拾取依赖 jar,但每次.class打开文件时(例如在调试期间)IntelliJ 都会在顶部显示一个横幅,说明: Decompiled .class file, bytecode version: 55.0 (Java 11) Choose Sources...

Choose Sources打开对话框选择源,可以选择文件*-src.zip旁边的来添加源。*.jar这也可以通过项目设置来完成。在后一种情况下,有一个明确的警告:A Library is imported from Gradle. Any changes made in its configuration might be lost after reimporting.. 使用时这种行为仍然Choose Sources存在,只是没有警告。

核心问题

如何以 IntelliJ(或任何其他 IDE)自动识别的方式将源添加到 Gradle 配置?即,实现Choose SourcesIntelliJ IDEA 中对话框的功能的 Gradle 指令是什么?

在 Gradle 文件中声明依赖项的方式,该文件包含在实际build.gradle中,方式如下:

dependencies {
    api fileTree(dir: ghidraDir + '/Framework', include: "**/*.jar")
}

尝试

我的第一次尝试是将其更改为api fileTree(dir: ghidraDir + '/Framework', includes: ["**/*.jar", "**/*-src.zip"]),但这会将 zip 添加为 IntelliJ 中的单独存档文件,而不是作为依赖项 jar 的源。

Eclipse 的 Gradle 代码

似乎为 Eclipse 实现相同结果的 Gradle 代码如下所示,特别是entry.setSourcePath

eclipse {
    classpath {
        downloadJavadoc = true
        downloadSources = true
        file {
            whenMerged {
                File javaDoc = new File(ghidraInstallDir+"/docs/GhidraAPI_javadoc.zip");
                for (entry in entries) {
                    if (entry.path.contains('jar')) {
                        File folder = new File(entry.getPath()).getParentFile();
                        for (File file : folder.listFiles()) {
                            if (file.getName().endsWith(".zip")) {
                                if (file.getName().contains("-src")) {
                                    entry.setSourcePath(it.fileReference(file));
                                }
                                entry.setJavadocPath(it.fileReference(javaDoc));
                            }
                        }
                    }
                }
                entries.add(
                    new org.gradle.plugins.ide.eclipse.model.Library(
                        it.fileReference(new File(projectDir, '/bin'))
                    )
                )
            }
        }
    }
}

标签: gradleintellij-ideaghidra

解决方案


推荐阅读