Created
December 1, 2013 01:03
-
-
Save paddycakes/7727356 to your computer and use it in GitHub Desktop.
A Groovy script to interactively create a Gradle project (Java, Groovy or Application as specified by user input) and directory structure. Also inits a Git repository for the project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def GROOVY = "groovy" | |
def JAVA = "java" | |
def APPLICATION = "application" | |
def plugins = [GROOVY, JAVA, APPLICATION] | |
def projectName | |
def projectType | |
def console = System.console() | |
if (console) { | |
projectName = console.readLine('> Please enter the project name: ') | |
projectType = console.readLine("> Please enter the project type (eg. ${plugins.join(', ')}): ") | |
if (!plugins.contains(projectType)) { | |
println "Unknown project type '$projectType'. Exiting." | |
System.exit 1 | |
} | |
} else { | |
println "Cannot get console. Exiting." | |
System.exit 1 | |
} | |
def dependencies = { out -> | |
if (projectType == GROOVY) out << "groovy 'org.codehaus.groovy:groovy-all:1.8.8'" | |
out << | |
""" | |
compile files(fileTree(dir: 'lib', includes: ['*.jar'])), | |
'joda-time:joda-time:2.1', | |
'com.google.guava:guava:12.0' | |
testCompile 'junit:junit:4.10' | |
""" | |
if (projectType == GROOVY) { | |
out << | |
""" | |
testCompile('org.spockframework:spock-core:0.7-groovy-1.8') { | |
exclude module: 'groovy-all' | |
exclude module: 'hamcrest-core' | |
exclude module: 'junit-dep' | |
} | |
""" | |
} | |
out << "runtime 'com.h2database:h2:1.3.170'" | |
} | |
println "Creating directories & files for new ${projectType.capitalize()} Gradle project ..." | |
// "mkdir $projectName".execute() | |
println "Making directory $projectName" | |
new File("$projectName").mkdir() | |
new File("$projectName/lib").mkdir() | |
new File("$projectName/docs").mkdir() | |
new File("$projectName/.gitignore").withPrintWriter{ w -> | |
"build,bin,.gradle,.settings,.classpath,.project".split(",").each{ | |
w.println(it) | |
} | |
} | |
new File("$projectName/build.gradle") << """\ | |
apply plugin: '$projectType' | |
apply plugin: 'eclipse' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
$dependencies | |
} | |
task createSourceDirs { | |
doLast { | |
sourceSets*.allSource.srcDirs.flatten().each { File sourceDirectory -> | |
if (!sourceDirectory.exists()) { | |
println "Making \$sourceDirectory" | |
sourceDirectory.mkdirs() | |
} | |
} | |
} | |
} | |
task wrapper(type: Wrapper) { | |
gradleVersion = '1.4' | |
} | |
""" | |
// Use Ant to execute gradle and git commands | |
def ant = new AntBuilder() | |
// "cmd /c gradle createSourceDirs eclipse".execute() | |
ant.exec(executable: "gradle", dir: "$projectName") { | |
arg(value: "createSourceDirs") | |
arg(value: "eclipse") | |
} | |
// "cmd /c git init".execute() | |
ant.exec(executable: "git", dir: "$projectName") { | |
arg(value: "init") | |
} | |
Thread.start { | |
sleep 5000 // allow time for all files to be created | |
new File("$projectName").eachFile { | |
println it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment