May 20, 2010
Gradle and IntelliJ IDEA
At work, I’ve been migrating a legacy build system to gradle. We use IntelliJ IDEA as IDE and the integration between the two is quite barebones.
Inspired by Felipe Cypriano, I extended his task to sync dependencies from Gradle to IDEA in a multiproject build.
It creates a library file for each subproject and each configuration (if it contains any dependencies). Project dependencies are ignored.
Read more for source
task syncIdeaLibraries << {
description = 'Add gradle dependecies for each project to IntelliJ project libraries'
final File librariesDir = new File(".idea${File.separator}libraries")
librariesDir.mkdirs()
def createLibrary = { fileName, libraryName, jars ->
final def gradleLibXml = new File(librariesDir, fileName)
gradleLibXml.write '''
<component name=\"libraryTable\">
<library name=\"''' + String.valueOf(libraryName) + '''\"/>
</component>'''
final def xmlRoot = new XmlParser().parse(gradleLibXml)
final def classesNode = xmlRoot.library[0].appendNode('CLASSES')
jars.each { jar ->
classesNode.appendNode('root', [url: "jar://${jar.trim()}!/"])
}
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(xmlRoot)
gradleLibXml.write writer.toString()
println "File '${gradleLibXml.path}' updated"
}
println "Idea library path: ${librariesDir.path}"
final File userHomeGradle = project.gradle.gradleUserHomeDir
println "Set the USER_HOME_GRADLE variable to '$userHomeGradle.path'"
project.subprojects.each { p ->
p.configurations.each { c ->
if (!c.dependencies.isEmpty()) {
def jars = c.files.collect { f ->
if (f.path.startsWith(userHomeGradle.path)) {
"\$USER_HOME_GRADLE\$${f.path.substring(userHomeGradle.path.size())}"
}
}
jars.removeAll([null])
createLibrary "Gradle_${p.name}_${c.name}.xml", "Gradle ${p.name} ${c.name}", jars
}
}
}
}
Advertisement