首页 文章

使用Gradle和IntelliJ管理项目依赖项

提问于
浏览
7

我希望使用Gradle构建我的Groovy / Grails项目,我们使用IntelliJ Idea作为IDE .

我使用的是IntelliJ版本11.1.4,Gradle版本1.2 .

我的项目被配置为具有各种Groovy和Grails子项目的多项目构建 .

我希望这会给我提供与我通过Maven管理构建时相同的IDE支持,例如:

  • 自动依赖管理(在添加到各种build.gradle时将新依赖项导入IntelliJ)

  • 构建DSL支持

  • 执行构建任务

  • 执行构建时IDE使用底层构建系统(gradle)

我通过打开root build.gradle文件将我的项目导入IntelliJ .

到目前为止,我遇到了一些恼人的问题:

  • IntelliJ无法识别(或随机识别)build.gradle文件中依赖项的更改,因此不会更新依赖项 .

  • gradle "idea"插件似乎不适用于多模块项目 .

人们如何在IntelliJ中使用Gradle?您是否在IntelliJ中手动管理依赖项?

2 回答

  • 7

    我一直在使用Gradle“idea”插件已经有一段时间了,它运行得很好 . 由于“idea”插件只是简单地生成IntelliJ项目配置文件,因此它可以用它来做一些限制,但是,与IntelliJ gradle支持(JetGradle事物)相比,我已经取得了更大的成功 .

    Gradle "idea"插件可以与多模块项目一起使用,从来没有遇到过问题 . 我总是将父项目配置放在 master 文件夹中(参见Initialization chapter),这似乎有效 . 从未尝试过嵌套结构 .

    为了进行额外的IntelliJ配置,你可以从gradle做一些 .ipr.iml 修补,或者尝试使用my plugins之一(参见实用工具插件),它将为你做大部分的修修补补 .

  • 3

    最后我跟上了rodion的建议并使用了想法插件 . 事实证明我第一次尝试时没有正确配置它(仅将插件应用于主项目,而不是子项目) .

    在编写自己的任务后,我才发现这一点,以更新IntelliJ项目的依赖项(基于.idea目录布局项目结构) . 我将使用该插件,因为维护较少,但这是我的后代解决方案,以防万一对任何人都有用:

    ext {
        intelliJLibraryDir = "$gradle.rootProject.rootDir/.idea/libraries"
        userHomeDir = gradle.gradleUserHomeDir.parent
    }
    
    
    task cleanIntelliJLibraries << {
        ant.delete (includeEmptyDirs: 'true') { 
            fileset(dir: intelliJLibraryDir, includes: '*.xml') 
        }
    }
    
    
    task createIntelliJLibraries(dependsOn: cleanIntelliJLibraries) << {
    
        // The unique set of dependency artifacts across all subprojects
        def uniqueProjectArtifacts = subprojects.collectMany {
            if (it.configurations.compile) {
                it.configurations.compile.resolvedConfiguration.resolvedArtifacts.findAll { 
                    it.moduleVersion.id.group != "my.project" 
                }
            }
            else { [] }
        }.unique()
    
        // Output a library xml file for each of the dependency artifacts
        uniqueProjectArtifacts.each { artifact ->
    
            def artifactPath = artifact.file.path                       
            def artifactName = artifact.moduleVersion.id.with { "$group:$name:$version" }
    
            def intelliJLibraryPath = artifactPath.replace(userHomeDir, '$USER_HOME$')                  
            def intelliJLibraryFileName = "Gradle__$artifactName".replace(':', '_').replace('.','_') + ".xml"
    
            new File("$intelliJLibraryDir/$intelliJLibraryFileName").withWriter { writer ->
    
                def dependencyXML = new MarkupBuilder( writer )
    
                dependencyXML.component (name: "libraryTable") {
                    library (name: "Gradle: $artifactName") {
                        CLASSES {
                            root (url: "jar://$intelliJLibraryPath!/")
                        }
                        JAVADOC {}
                        SOURCES {}
                    }
                }
            }
        }           
    }
    
    
    task updateIntelliJModules(dependsOn: createIntelliJLibraries) << {
    
        subprojects.each { project ->
    
            def root = new XmlSlurper().parse(new File("${project.name}.iml"))
    
            // Remove the existing dependencies
            root.component.orderEntry.findAll { it.@type == "library" && it.@level == "project" }.replaceNode {}
    
            // Add in the new dependencies
            if (project.configurations.compile) {
    
                project.configurations.compile.resolvedConfiguration.resolvedArtifacts.findAll {
                    it.moduleVersion.id.group != "my.project"
                }.each { artifact ->
                    def artifactName = artifact.moduleVersion.id.with { "Gradle: $group:$name:$version" }
    
                    root.component.appendNode {
                        orderEntry (type: "library", exported: "", name: artifactName, level: "project")
                    }
                }
    
            }
    
            def outputBuilder = new StreamingMarkupBuilder()
    
            new File("${project.name}.iml").withWriter { writer ->
                groovy.xml.XmlUtil.serialize(outputBuilder.bind{ mkp.yield root }, writer)
            }
        }
    }
    

相关问题