Skip to content
Christopher J. Stehno edited this page Jan 25, 2014 · 1 revision

General

Entering gradle tasks will show you the default available Templates tasks:

createGradlePlugin - Creates a new Gradle Plugin project in a new directory named after your project.
createGroovyClass - Creates a new Groovy class in the current project.
createGroovyProject - Creates a new Gradle Groovy project in a new directory named after your project.
createJavaClass - Creates a new Java class in the current project.
createJavaProject - Creates a new Gradle Java project in a new directory named after your project.
createScalaClass - Creates a new Scala class in the current project.
createScalaObject - Creates a new Scala object in the current project.
createScalaProject - Creates a new Gradle Scala project in a new directory named after your project.
createWebappProject - Creates a new Gradle Webapp project in a new directory named after your project.
initGradlePlugin - Initializes a new Gradle Plugin project in the current directory.
initGroovyProject - Initializes a new Gradle Groovy project in the current directory.
initJavaProject - Initializes a new Gradle Java project in the current directory.
initScalaProject - Initializes a new Gradle Scala project in the current directory.
initWebappProject - Initializes a new Gradle Webapp project in the current directory.

The main difference between the create*Project and init*Project tasks is that the create tasks end up creating a new directory for your new project, and the init tasks will create the default directory structure under the current directory.

Running createGroovyProject will prompt you for your new project's name:

[10:48:18 elberry@pulse:~/development/projects]
[543] gradle createGroovyProject
> Building > :createGroovyProject
?> Project Name: TestGroovyProject
:createGroovyProject

BUILD SUCCESSFUL

and create a directory like:

TestGroovyProject
    /src
        /main
            /groovy
            /resources
        /test
            /groovy
            /resources

Running the initGroovyProject task will generate the default groovy project directory structure in the current directory.

./src
    /main
        /groovy
        /resources
    /test
        /groovy
        /resources

Note that both of these tasks will also generate a build.gradle file, and a LICENSE.txt file in their respective roots.

This results in this basic directory hierarchy:

[project root]
    /src
        /main
            /groovy
            /resources
        /test
            /groovy
            /resources
    build.gradle
    LICENSE.txt

Which matches the default Gradle configuration.

Extra stuff

The TemplatesPlugin contains a few helpful methods and properties like the prompt method.

Common Properties
  • newProjectName - the name of the new project, which should follow standard Java naming rules.
  • projectGroup - the project repository group to be used by the project being created.
  • projectVersion - the project version to be used by the project being created.
  • projectParentDir - the optional parent directory path where the project is to be created.
Properties

group: Can be used when defining tasks to have all template tasks grouped together.

task "createJavaProject"(group: TemplatesPlugin.group) << { ... }
Methods

prependPlugin: Can be used in tasks to help add plugins to project build scripts.

task "createJavaProject" << {
	...
	TemplatesPlugin.prependPlugin 'java', new File(projectName, "build.gradle")
}

prompt: Can be used in tasks to prompt the user for some input.

task "favoriteColor" << {
	String color = TemplatesPlugin.prompt("What is your favorite color?")
	println "Your favorite color is ${color}"
}

Also takes a default value.

task "favoriteColor" << {
	String color = TemplatesPlugin.prompt("What is your favorite color?", "Green")
	println "Your favorite color is ${color}"
}

promptOptions: Can be used to prompt the user for a choice of 1 of a list of options.

task "bestColor" << {
	def colors = ["red", "blue", "green"]
	int choice = TemplatesPlugin.promptOptions("Which color do you like best?", colors)
	println "You like ${colors[choice]} best."
}

Or you can pass in a default value:

// Default value is 1 based - matches the numeric values displayed to the user.
task "bestColor" << {
	def colors = ["red", "blue", "green"]
	int choice = TemplatesPlugin.promptOptions("Which color do you like best?", 1, colors)
	println "You like ${colors[choice]} best."
}

promptYesOrNo: Can be used in tasks to prompt for boolean 'yes', or 'no' answers.

task "useful" << {
	if(TemplatesPlugin.promptYesOrNo("Is the templates plugin useful to you?")) {
		println "Great! I'm glad to hear that!"
	} else {
		println "Awww, I'm sorry to hear that."
	}
}

Gradle Properties for tasks

The Templates tasks are listed below with the available -P properties. Supplying the -P properties on the command line will avoid being prompted for the values during task execution.

createGradlePlugin

Has the same properties as initGradlePlugin plus the following.

property description
newProjectName Your new Gradle Plugin's name
exportPluginTemplates

There are no prompts for this task.

initGradlePlugin
property description
projectGroup Your project's group
projectVersion The version of your project
pluginApplyLabel The apply label for use in 'apply plugin: “label”'
pluginClassName The classname of your new plugin. Can include package, eg. 'com.example.MyClass'
initGroovyProject

There are no prompts for this task.

createGroovyProject
property description
newProjectName Your new Gradle Plugin's name