Groovy script to bootstrap a Gradle Project

A Groovy script to bootstrap a Gradle Groovy Project. Based on Ted Naleid’s shell script.

I took Ted Naleid’s “Quick Shell Function to Bootstrap a Gradle Groovy Project” example code and converted it to a Groovy script.

Updates
1. Fixed. This was not running. I was using triple quoted string, when I should have used the triple single-quoted string for the HEREDOC like usage below.
2. Gradle now includes (since 1.6) a Build Init Plugin that will create a project. This should be used and not the code in this blog post.
In Gradle 1.9 this works:

    gradle init --type java-library
    

3. Gradle IDE plugins have the ability to create Gradle based project.
4. Would be nice to be able to reuse Maven style “archetypes”.


Deprecated
But, may serve as a Groovy code example.
Listing 1.

// NewGradle.groovy
// Author: Josef Betancourt
// Based on T. Naleid's shell script 

println "Creating files for new Gradle project ..."

new File(".gitignore").withPrintWriter{ w ->
     "*.un~,*.iml,*.ipr,*.iws,build,.gradle".split(",").each{ 
         w.println(it)
     }
}

new File("build.gradle") << '''
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'eclipse'
 
repositories {
    mavenCentral()
}
 
dependencies {
    groovy 'org.codehaus.groovy:groovy:1.8.6'
    compile 'org.apache.ivy:ivy:2.2.0'
}
 
task createSourceDirs(description : 
   'Create empty source directories for all defined sourceSets') 
   << {
        sourceSets*.allSource.srcDirs.flatten().each { 
            File sourceDirectory ->        
            if (!sourceDirectory.exists()) {
                println "Making $sourceDirectory"
                sourceDirectory.mkdirs()
            }
        }
}
 
idea {
    project {
        jdkName = '1.6'
    }
}

''' // end content

"cmd /c gradle createSourceDirs".execute()

"cmd /c git init".execute()

Thread.start{
	sleep 5000 // allow time for all files to be created
	new File(".").eachFile{
		println it
	}
}

Not expert Groovy, but was easy to do. The bulk of it is the creation of a “here” doc using Groovy’s triple quote string. I didn’t duplicate the last line of Naleid’s script: “ls -a1 && find src # list all created assets”.

This script is not fully cross-platform. The invocation of shell commands at the end are in the Windows format. Left as an exercise to reader is the use of inline AntBuilder to reuse Ant’s exec task. 🙂

Updates
2012-03-09: Tweaked the source. Removed use of two temp variables.
2012-03-09: Added the Eclipse plugin. Now after creating the Gradle project executing eclipse will create the eclipse project: gradle eclipse
Or instead generate the Idea project: gradle idea.

Further Reading

  1. Groovy (Programming language)
  2. Groovy
  3. Gradle
  4. Quick Shell Function to Bootstrap a Gradle Groovy Project
  5. Strings and GString
  6. Groovy JDK extensions
  7. Executing External Processes
  8. Using Gradle to Bootstrap your Legacy Ant Builds

Off Topic

Leave a Reply

Your email address will not be published. Required fields are marked *