---
title: "Gradle for Projects(I)Gradle Concept"
author: "sillycat"
date: "2011-06-09T22:01:46Z"
tags: ["Groovy", "HTML"]
categories: ["编程语言"]
slug: "gradle-for-projects-i-gradle-concept"
draft: false
lastmod: "2011-06-09T22:01:46Z"
id: 1074642
url: "https://sillycat.iteye.com/blog/1074642"
---

Gradle for Projects(I)Gradle Concept

1. Install the gradle
Prerequisites
jdk1.5+, any existing groovy installation is ignored by Gradle.

download the latest file
http://repo.gradle.org/gradle/distributions/gradle-1.0-milestone-3-bin.zip

unpacking
>unzip gradle-1.0-milestone-3-bin.zip
>mv gradle-1.0-milestone-3 ../tools/
>cd ../tools/gradle-1.0-milestone-3

configure in /etc/environment
GRADLE_HOME=/home/luohua/tools/gradle-1.0-milestone-3
PATH="...snip...:${GRADLE_HOME}/bin"
>. /etc/environment

verify the installation
>gradle -v

2. Build Script Basics
2.1 basic task
>vi build.gradle
task hello {
	doLast {
		println 'hello carl!'	
	}
}

>gradle -q hello

2.2 dependency tasks
>vi build.gradle
task hello << {
	println "hello carl!";
}
task introduce(dependsOn: hello) << {
	println "That is great!";
}

2.3 Dynamic tasks
>vi build.gradle
4.times {
	counter ->
	    task "task$counter" << {
		 println "task number $counter";
	    }
}
>gradle -q tasks

2.4 Manipulating existing tasks
>vi build.gradle
4.times {
	counter ->
	    task "task$counter" << {
			println "task number $counter";
	    }
}
task0.dependsOn task2, task3
>gradle -q task0

2.5 Adding behaviour
>build.gradle
task hello << {
	println "hello 1"
}
hello.doFirst {
	println "hello 2";
}
hello.doLast {
	println "hello 3";
}
hello << {
	println "hello 4";
}
>console output: hello 2 hello 1 hello 3 hello 4

2.5 Dynamic task properties
>vi build.gradle
task mytask
mytask.myproperty = "test";

task showProps << {
    println mytask.myproperty;
}
>gradle -q showProps
test

2.6 Default tasks
>vi build.gradle
defaultTasks 'clean', 'run'
>gradle -q    
eq to 
>gradle -q clean run


references:
http://www.gradle.org/
http://www.gradle.org/tutorials.html