Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
apply plugin: 'groovy'
sourceCompatibility = 1.5
version = '0.1-SNAPSHOT'
repositories {
mavenRepo(urls: 'http://repository.codehaus.org/')
mavenCentral()
}
dependencies {
groovy 'org.codehaus.groovy:groovy:1.7.2'
compile 'org.slf4j:slf4j-log4j12:1.5.2'
testCompile group: 'junit', name: 'junit', version: '4.7'
}
final def GRADLE_TEST_LIBRARIES_ID = 'Gradle Test Libraries'
task intellijSync << {
description = 'Add gradle dependecies to IntelliJ project library'
final def librariesDir = new File(".idea${File.separator}libraries")
librariesDir.mkdirs()
final def userHomeGradle = project.gradle.gradleUserHomeDir
println "Set the USER_HOME_GRADLE variable to '$userHomeGradle.path'"
def makeJarList = { path ->
path.split(File.pathSeparator).collect {
it.replaceAll userHomeGradle.path, "\\\$USER_HOME_GRADLE\\\$"
}
}
final def compileJars = makeJarList(configurations.compile.asPath)
final def testJars = makeJarList(configurations.testCompile.asPath) - compileJars
def createLibrary = { fileName, libraryName, jars ->
final def gradleLibXml = new File(librariesDir, fileName)
gradleLibXml.write """
<component name="libraryTable">
<library name="$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!/"])
}
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(xmlRoot)
gradleLibXml.write writer.toString()
println "File '${gradleLibXml.path}' updated"
}
createLibrary 'Gradle_Libraries.xml', 'Gradle Libraries', compileJars
createLibrary 'Gradle_Test_Libraries.xml', GRADLE_TEST_LIBRARIES_ID, testJars
}
task intellijModuleSync(dependsOn: intellijSync) << {
final def moduleFile = new File('your-module-here.iml') // TODO rename to match your module's name
def root = new XmlParser().parse(moduleFile)
def newModuleRootManager = root.component.find {it.'@name' == 'NewModuleRootManager'}
def orderEntry = newModuleRootManager.orderEntry.find {
it.'@type' == 'library' && it.'@name' == GRADLE_TEST_LIBRARIES_ID
}
if (orderEntry) {
newModuleRootManager.remove(orderEntry)
}
newModuleRootManager.appendNode('orderEntry', [type: 'library', scope: 'TEST', name: GRADLE_TEST_LIBRARIES_ID, level: 'project'])
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(root)
moduleFile.write writer.toString()
println "File '${moduleFile.path}' updated"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment