Managing Project Dependencies with Gradle and IntelliJ

I want to use Gradle to create my Groovy / Grails project, for which we use IntelliJ Idea as an IDE.

I am using IntelliJ version 11.1.4, Gradle version 1.2.

My project is configured as a multi-project build with various Groovy and Grails subprojects.

I was hoping this would give me the same IDE support that I would get if I managed the assembly through Maven, for example:

  • Automatic dependency management (import of new dependencies with IntelliJ when adding to various build.gradle)
  • DSL Build Support
  • Complete build tasks
  • Using the base gradle system using the IDE when building \

I imported my project into IntelliJ by opening the root build.gradle file.

So far, I am facing several unpleasant problems:

  • IntelliJ does not recognize (or randomly recognize) dependency changes in build.gradle files, and therefore dependencies are not updated.
  • The Gradle Idea plugin does not seem to work with projects with multiple modules.

How are people working with Gradle inside IntelliJ? Do you manage dependencies manually in IntelliJ ??

+7
source share
2 answers

I have been using the Gradle "idea" plugin for some time, and it works very well. Since the idea plugin simply generates the configuration files for the IntelliJ project, it is somewhat limited in what you can do with it, but, nevertheless, I had more success with it compared to IntelliJ Gradle support (JetGradle thing).

Gradle The idea plugin works with multi-module projects; there have never been problems with this. I always specified the parent configuration of the project in the master folder (see the initialization chapter ), which seems to work. Never tried a nested structure.

To do additional customization of IntelliJ, you can do some .ipr and .iml mastering from gradle, or try using one of my plugins (see "Utility Plugin"), which will do most of the search.

+7
source

In the end, I went with the offer of homeland above and used the idea plugin. It turns out that I did not configure it correctly the first time I tried it (I applied only the plugin to the main project, not the subprojects).

I only found out about this after writing my own task of updating dependencies for an IntelliJ project (based on the project structure of the .idea catalog layout). I am going to use the plugin as there will be less maintenance, but here is my solution for posterity if it is useful to anyone:

 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) } } } 
+3
source

All Articles