2012-11-05 91 views
7

我正在寻找使用Gradle构建我们正在使用IntelliJ Idea作为IDE的基于Groovy/Grails的项目。使用Gradle和IntelliJ管理项目依赖关系

我正在使用IntelliJ版本11.1.4,Gradle版本1.2。

我的项目是一个配置为多个项目的Groovy & Grails子项目。

我希望这会给我同类的IDE支持,我会得到,如果基于Maven管理构建,如:

  • 自动依赖管理(当添加到导入新的依存关系的IntelliJ不同的build.gradle)
  • 构建DSL支持
  • 执行的构建任务执行时
  • 由IDE使用底层构建系统(gradle产出)的构建\

我已经通过打开root build.gradle文件将我的项目导入到IntelliJ中。

到目前为止,我来面对一些恼人的问题:

  1. 的IntelliJ是不承认(或随机确认)变为依赖于文件的build.gradle,因此它的依赖不会被更新。
  2. gradle“idea”插件似乎不适用于多模块项目。

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

回答

7

我一直在使用Gradle“idea”插件一段时间,它工作得很好。由于“idea”插件只是简单地生成IntelliJ项目配置文件,所以它可以用它做些什么,但是,与IntelliJ gradle支持(JetGradle的东西)相比,我获得了更多的成功。

Gradle“idea”插件可以处理多模块项目,从来没有问题。我总是把父项目配置放在master文件夹(请参阅Initialization chapter),这似乎工作。从来没有尝试过嵌套结构。

为了做额外的IntelliJ配置你可以做一些.ipr.iml从修补的gradle,或者尝试使用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 { [email protected] == "library" && [email protected] == "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) 
     } 
    } 
}