Skip to content

Instantly share code, notes, and snippets.

@fcy
Created April 8, 2010 13:57
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fcy/360092 to your computer and use it in GitHub Desktop.
Save fcy/360092 to your computer and use it in GitHub Desktop.
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