Practical Gradle

Gradle beyond hello world

Archive for February, 2011

Running Gradle from Textmate

Posted by rene on February 8, 2011

For those of you who prefer editing and running your Gradle builds via TextMate, I have created a TextMate bundle to run your builds directly from your favourite editor. The bundle is available at https://github.com/breskeby/tmBundles.

To run a Gradle build script that you are editing in TextMate, you just have to execute the “Run Gradle build” command of the Gradle Bundle. The “Run Gradle build” command can be started in three different ways:

  1. If you have selected a text within your build file, the selected text is interpreted as the task to execute.
  2. If the build file defines a default task, the default task is executed.
  3. If no default task is configured and no text is selected, the “Run Gradle build” command prompts an input dialog to specifiy the tasks you want to execute.

The output is formatted as HTML and it uses javascript to render the output dynamically.
gradletmbundle

You may notice some limitations of this bundle. Actually, it works only for one defined default task. I have uploaded the mentioned bundle to github to share it with you and to profit from others’ bash/javascript know-how.

UPDATE: To show the Gradle bundle in action, I have created a short screencast, available at http://www.breskeby.com/gradlecasts/2011/02/08/33/

Posted in general | 2 Comments »

3-part Gradle series in Javamagazin

Posted by etienne on February 6, 2011

I’ve written a 3-part series on Gradle for the Javamagazin. On a total of about 18 pages, I explain Gradle for Java developers, Gradle concepts, and Gradle in the enterprise. The full series is contained in the first three issues of 2011. I hope that many of the 20’000 monthly readers enjoy reading it, and hopefully some of them will give Gradle a try.

Gradle for Java developers Enterprise Gradle

Posted in general | Tagged: | 2 Comments »

Task auto-completion on the command-line

Posted by etienne on February 2, 2011

Gradle’s camel-case support to abbreviate long task names on the command line is very efficient to work with. For example, if you have a task called processTestResources, you can run it like this:

$ gradle pTR

Nevertheless, task auto-completion on the command-line would be very nice to have, too. The following task definition generates a Bash auto-completion file for all tasks available in the current Gradle project. The Bash script itself has been taken from the Gradle cookbook.

class AutoCompletion extends DefaultTask {
    def includedTasks = project.tasks.all
    def File outputFile = project.file("${project.name}")

    def template = """
#!/bin/bash

_gradle_complete()
{
  local cur tasks

  COMPREPLY=()
  cur=\${COMP_WORDS[COMP_CWORD]}
  tasks='@@TASKS@@'
  cur=`echo \$cur | sed 's/\\\\\\\\//g'`
  COMPREPLY=(\$(compgen -W " \${tasks}" \${cur} | sed 's/\\\\\\\\//g') )
}

complete -F _gradle_complete -o filenames gradle
 """

    @TaskAction
    void generateFile() {
        String joinedTasks = includedTasks*.name.join(' ')
        String result = template.substring(1).replace('@@TASKS@@', joinedTasks)
        outputFile.write result
        logger.info "Successfuly generated Bash auto-completion file $outputFile.name. " +
                "Sudo copy $outputFile.name to /etc/bash_completion.d and call source /etc/bash_completion.d/$outputFile.name."
    }
}

We can now declare an AutoCompletion task in our Gradle build script. For example, in the following build script, we declare to generate the Bash auto-completion file without the tasks that are only relevant in a multi-project context. We also declare a different file name.

task autoCompletion(type: AutoCompletion) {
    includedTasks = tasks.all - tasks.findByPath(':buildDependents') - tasks.findByPath('buildNeeded')
    outputFile = file("${project.name}_ac")
}

Finally, we can run the autoCompletion task declared above.

$ gradle aC

Once we have the Bash auto-completion file, we just need to sudo copy it to /etc/bash_completion.d and call source on it.

Posted in general | Tagged: , | 5 Comments »