Skip to content

Instantly share code, notes, and snippets.

@stephanos
Created January 27, 2014 09:48
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save stephanos/8645809 to your computer and use it in GitHub Desktop.
Save stephanos/8645809 to your computer and use it in GitHub Desktop.
Gradle: Automate IntelliJ IDEA project generation for a Java App Engine project
def jvmRunFlags = '''
-Xmx700m
- ...
'''.split().toList()
def jvmTestFlags = '''
-Dlocally=true
-ea
- ...
'''.split().toList()
...
apply plugin: 'idea'
def IDEA_MODULE_NAME = 'mymodule'
def IDEA_ARTIFACT_NAME = 'myproject'
def IDEA_BUILD_DIR = 'build_ide'
def IDEA_BUILD_APP_DIR = IDEA_BUILD_DIR + '/exploded-app'
def GAE_DATASTORE_DIR = IDEA_BUILD_DIR + '/local_db.bin'
// make sure build dir exists
new File(IDEA_BUILD_DIR).mkdirs()
idea {
// see 'http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html'
module {
name = IDEA_MODULE_NAME
outputDir = file(IDEA_BUILD_DIR)
testOutputDir = file(IDEA_BUILD_DIR)
excludeDirs += file(IDEA_BUILD_APP_DIR)
downloadSources = true
downloadJavadoc = false
iml.withXml { xmlFile ->
// setup 'Web' and 'App Engine' facets
def facetManager = xmlFile.asNode().component.find { it.@name == 'FacetManager' } as Node
if (facetManager) {
Node webFacet = facetManager.facet.find { it.@type == 'web' }
if (webFacet)
facetManager.remove(webFacet)
} else {
facetManager = xmlFile.asNode().appendNode('component', [name: 'FacetManager']);
}
def builder = new NodeBuilder()
def webFacet = builder.facet(type: "web", name: 'Web') {
configuration {
descriptors {
deploymentDescriptor(name: 'web.xml', url: 'file://$MODULE_DIR$/web/WEB-INF/web.xml')
}
webroots {
root(url: 'file://$MODULE_DIR$/web', relative: '/')
}
sourceRoots {
root(url: 'file://$MODULE_DIR$/src/main/resources')
root(url: 'file://$MODULE_DIR$/src/main/java')
}
}
facet(type: 'google-app-engine', name: 'Google App Engine') {
configuration {
'sdk-home-path'(GAE_SDK_DIR)
}
}
}
facetManager.append webFacet
}
}
project {
ipr.withXml { xmlFile ->
// enable 'Annotation Processors'
xmlFile.asNode().component.find { it.@name == 'CompilerConfiguration' }['annotationProcessing'][0].replaceNode {
annotationProcessing {
profile(default: true, name: 'Default', useClasspath: 'true', enabled: true)
}
}
// setup Git root
xmlFile.asNode().component.find { it.@name == 'VcsDirectoryMappings' }.replaceNode {
component(name: 'VcsDirectoryMappings') {
mapping(directory: "", vcs: "")
mapping(directory: "\$PROJECT_DIR\$", vcs: 'Git')
}
}
// setup compiler output directory
xmlFile.asNode().component.find { it.@name == 'ProjectRootManager' }['output'][0].replaceNode {
output(url: "file://\$PROJECT_DIR\$/$IDEA_BUILD_DIR/out")
}
// setup artifact configuration
def artifactManager = xmlFile.asNode().component.find { it.@name == 'ArtifactManager' } as Node
if (artifactManager) {
Node artifact = artifactManager.artifact.find { it.@type == 'exploded-war' }
if (artifact)
artifactManager.remove(artifact)
} else {
artifactManager = xmlFile.asNode().appendNode('component', [name: 'ArtifactManager']);
}
def artifact = new NodeBuilder().artifact(type: 'exploded-war', 'build-on-make': "true", name: "$IDEA_ARTIFACT_NAME") {
'output-path'("\$PROJECT_DIR\$/$IDEA_BUILD_APP_DIR")
root(id: 'root') {
element(id: 'directory', name: 'WEB-INF') {
element(id: 'directory', name: 'classes') {
element(id: 'module-output', name: IDEA_MODULE_NAME)
}
element(id: 'directory', name: 'lib') {
this.project.configurations.runtime.each {
element(id: 'file-copy', path: it)
}
}
}
element(id: 'javaee-facet-resources', facet: "$IDEA_MODULE_NAME/web/Web")
}
}
artifactManager.append artifact
}
}
workspace {
iws.withXml { xmlFile ->
def runManager = xmlFile.asNode().component.find { it.@name == 'RunManager' }
// setup JUnit's default run configuration
def junitDefaults = runManager.configuration.find { it.@default == 'true' && it.@type == 'JUnit' }
junitDefaults.option.find { it.@name == 'VM_PARAMETERS' }.replaceNode {
option(name: 'VM_PARAMETERS', value: jvmTestFlags.join(" "))
}
junitDefaults.module.replaceNode {
module(name: IDEA_MODULE_NAME)
}
// setup App Engine's default run configuration
def gaeDefaults = runManager.configuration.find { it.@default == 'true' && it.@type == 'GoogleAppEngineDevServer' }
if (!gaeDefaults) {
def builder = new NodeBuilder()
gaeDefaults = builder.configuration(default: 'true', factoryName: 'Local', type: 'GoogleAppEngineDevServer') {
option(name: 'OPEN_IN_BROWSER', value: 'false')
option(name: 'SHOW_DIALOG_ON_UPDATE', value: 'false')
}
runManager.append gaeDefaults
}
def gaeJvmArgs = gaeDefaults.option.find { it.@name == 'COMMON_VM_ARGUMENTS' }
if (gaeJvmArgs)
gaeDefaults.remove(gaeJvmArgs)
def jvmArgs = jvmRunFlags.join(" ") + " -Ddatastore.backing_store=\$PROJECT_DIR\$/$GAE_DATASTORE_DIR"
gaeDefaults.append new NodeBuilder().option(name: 'COMMON_VM_ARGUMENTS', value: jvmArgs)
def gaeServerSettings = gaeDefaults.'server-settings'
if (gaeServerSettings)
gaeDefaults.remove(gaeServerSettings)
gaeDefaults.append new NodeBuilder().'server-settings' {
option(name: "artifact", value: IDEA_ARTIFACT_NAME)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment