intellij-idea - 如何将依赖源添加到 IntelliJ

标签 intellij-idea gradle dependencies

我有一个带有 gradle 构建文件的 IntelliJ 项目,其中包括来自 Maven 中央存储库的多个项目。 Geb 就是这样一种依赖项。

当我浏览我的类(class)时,我有时会遇到一个看起来很有趣的 Geb 类(class)。我选择“前往申报”并得到一个悲伤的“找不到要前往的申报”。

显然这是因为 IntelliJ 没有加载 Geb 源文件。但是,在我的项目中不包含 Geb 作为源的情况下,如何让它做到这一点呢?我不希望 Geb 从源代码编译到我的项目中,因为我已经将它作为依赖项包含在我的 gradle 构建文件中。

  • 将其添加为模块依赖项不起作用。这就像添加更多来源。
  • 我想我可以获取 repo 并构建 jars,然后包含它们。这真的有必要吗?
  • 将 IDEA 插件添加到 gradle 文件不起作用。

  • gradle 脚本的相关部分:
    apply plugin: 'groovy'
    apply plugin: 'idea'
    
    dependencies {
        // need to depend on geb-spock
        testCompile "org.gebish:geb-spock:0.13.1"
        testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
        testCompile "org.apache.commons:commons-lang3:3.4"
        testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
        testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
    }
    
    idea {
        module {
            downloadJavadoc = true // defaults to false
            downloadSources = true
        }
    }
    

    最佳答案

    这是一个完整的构建脚本,可以下载所有源代码的依赖项:

    apply plugin: 'java'
    apply plugin: 'idea'
    
    idea {
        module {
            downloadJavadoc = true // defaults to false
            downloadSources = true
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        // need to depend on geb-spock
        testCompile "org.gebish:geb-spock:0.13.1"
        testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
        testCompile "org.apache.commons:commons-lang3:3.4"
        testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
        testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
    }
    
    task wrapper(type: Wrapper) {
        gradleVersion = '3.3'
    }
    

    依赖项显示在列表中:

    Dependency

    而且我可以浏览源代码,您可以看到那里的注释:

    The source code

    一种可能的解释可能是您的存储库列表中有一个存储库,例如 mavenLocal 或缓存 Artifactory,它没有源依赖项。

    存储库的顺序很重要,所以如果 mavenLocal 是第一个并且那里的源不可用,我相信它们不会被下载。一个可能的解决方法是从 mavenLocal 中删除依赖项并重新下载它,更改依赖项的顺序,或者如果它是父脚本,则在添加存储库时免除您的子项目:
    configure(allprojects - project(':my-subproj')) { 
        repositories {
        ...
        }
    }
    

    我不认为有任何方法可以防止子项目的构建脚本发生这种情况。它必须在父级中完成。

    关于intellij-idea - 如何将依赖源添加到 IntelliJ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079908/

    相关文章:

    java - 带有红圈的Intellij Idea文件

    eclipse - 使用IntelliJ IDE自动完成行和放置分号

    makefile - 自动依赖检测在 GFortran 中不起作用

    ios - Xcode 11.4 编译错误 'Missing package product <package name>'

    c# - Xamarin 安卓 : Self-contradicting dependency requirements?

    java - 无法运行GWT项目

    java - 想要静态但不能支持?

    android - Android Studio将项目从eclipse导入到Android Studio

    spring - 如何使用相互依赖的 Spring Boot 项目和 jars 创建多构建 gradle 项目

    android - 无法找到 'java.nio.file.Files.walkFiletree'