Investigate Logging

Run gradle –debug hello

13:48:50.799 [QUIET] [system.out] Hello World

Build Lifecycle
-initialization
/myproject
common
api
app

-configuration
assemble, jar, javadoc, classes, processResources, compile Java

-execution
compile java, process Resources, classes, jar, javadoc, assemble
-> BUILD SUCCESSFUL

gradle plugins
https://docs.gradle.org/current/userguide/plugins.html

Incremental Builds

*.java, compile, *.class, dex, *.dex, /res/*, apkbuilder, app.apk

input files -> task -> output files

$ gradle groovy

FAILURE: Build failed with an exception.

* What went wrong:
Task 'groovy' not found in root project '1.12-Exercise-ExploreIncrementalBuilds'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with                                               --info                                                                           or                                                                             --debug                                                                          option to get more log output.

BUILD FAILED

Total time: 6.74 secs

Gradle ClI, gradle.properties files, Environmental variables

creating custom type

class HelloTask extends DefaultTask {
	@TaskAction
	void doAction(){
		println 'Hello World'
	}
}

task hello(type: HelloTask)
[vagrant@localhost ud867]$ gradle hello
:hello
Hello World

BUILD SUCCESSFUL

Total time: 6.504 secs

gradle task

project.task("myTask1")
task("myTask2")
task "myTask3"
task myTask4
myTask4.description = "This is what's shown in the task list"
myTask4.group = "This is the heading for this task in the task list."
myTask4.doLast {println "Do this last"}
myTask4.doFirst {println "Do this first"}
myTask4.leftShift {println "Do this even more last"}
myTask4 << {println "Do this last of all"}

task myTask5 {
	description "Here's a task with a configuration block"
	group "Some group"
	doLast {
		println "Here's the action"
	}
}

task myTask7 {
	description("Description")
	group = "Some group"
}

task myTask8(description: "Another description") << {
	println "Doing something"
}

depend on

task putOnSocks {
	doLast {
		println "Putting on Socks."
	}
}

task putOnShoes {
	dependsOn "putOnSocks"
	doLast {
		println "Putting on Shoes."
	}
}
[vagrant@localhost ud867]$ gradle -q putOnShoes
Putting on Socks.
Putting on Shoes.
task copyImage(type: Copy) {
	from 'images'
	into 'build'
}

task copyJpegs(type: Copy){
	from 'images'
	include '*.jpg'
	into 'build'
}

task copyImageFolders(type: Copy){
	from('images'){
		include '*.jpg'
		into 'jpeg'
	}

	from('images'){
		include '*.gif'
		into 'gif'
	}

	into 'build'
}

task zipImages(type: Zip){
	baseName = 'images'
	destinationDir = file('build')
	from 'images'
}

Tricky groovy

task groovy << {} def foo="bar" println "$foo + foo = ${foo+"foo"}" [/code]

[vagrant@localhost ud867]$ gradle groovy
bar + foo = barfoo
:groovy

BUILD SUCCESSFUL

Total time: 17.521 secs

gradle task

project.task("myTask1")
[vagrant@localhost ud867]$ gradle task
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
dependencies - Displays all dependencies declared in root project 'ud867'.
dependencyInsight - Displays the insight into a specific dependency in root project 'ud867'.
help - Displays a help message
projects - Displays the sub-projects of root project 'ud867'.
properties - Displays the properties of root project 'ud867'.
tasks - Displays the tasks runnable from root project 'ud867'.

Other tasks
-----------
myTask1

To see all tasks and more detail, run with --all.

BUILD SUCCESSFUL

Total time: 13.701 secs

Groovy Object

task groovy << {}

class GroovyGreeter {
	String greeting = "Default greeting"
	def printGreeting(){println "Greeting: $greeting" }
}

def myGroovyGreeter = new GroovyGreeter()

myGroovyGreeter.printGreeting()
myGroovyGreeter.greeting = "My custom greeting"
myGroovyGreeter.printGreeting()
[vagrant@localhost ud867]$ gradle groovy
Greeting: Default greeting
Greeting: My custom greeting
:groovy

BUILD SUCCESSFUL

Total time: 6.641 secs

Groovy Closures

task groovy << {}

def foo = "One million dollars"
def myClosure = {
	println "Hello from a closure"
	println "The value of foo is $foo"
}

myClosure()
[vagrant@localhost ud867]$ gradle groovy
Hello from a closure
The value of foo is One million dollars
:groovy

BUILD SUCCESSFUL

Total time: 6.312 secs
task groovy << {}

def doubleIt = { x -> x + x}

def applyTwice(func, arg){
	func(func(arg))
}

def foo = 5
def fooDoubledTwice = applyTwice(doubleIt, foo)
println "Applying doubleIt twice to $foo equals $fooDoubledTwice"
[vagrant@localhost ud867]$ gradle groovy
Applying doubleIt twice to 5 equals 20
:groovy

BUILD SUCCESSFUL

Total time: 6.366 secs
task groovy << {}

def myList = ["Gradle", "Groovy", "Android"]

def printItem = {item -> println "List item: $item"}
myList.each(printItem)

super cool groovy

task groovy << {}

def noArgs(){
	println "Called the no args function"
}

def oneArg(x) {
	println "Called the 1 arg function with $x"
	x
}

def twoArgs(x, y){
	println "Called the  2 arg function with $x and $y"
	x + y
}

oneArg 500
[vagrant@localhost ud867]$ gradle groovy
Called the 1 arg function with 500
:groovy

BUILD SUCCESSFUL

Total time: 7.983 secs

Hey, what?

task groovy << {}

def noArgs(){
	println "Called the no args function"
}

def oneArg(x) {
	println "Called the 1 arg function with $x"
	x
}

def twoArgs(x, y){
	println "Called the  2 arg function with $x and $y"
	x + y
}

twoArgs 200, 300
[vagrant@localhost ud867]$ gradle groovy
Called the  2 arg function with 200 and 300
:groovy

BUILD SUCCESSFUL

Total time: 6.671 secs
task groovy << {}

def noArgs(){
	println "Called the no args function"
}

def oneArg(x) {
	println "Called the 1 arg function with $x"
	x
}

def twoArgs(x, y){
	println "Called the  2 arg function with $x and $y"
	x + y
}

twoArgs oneArg(500), 100
[vagrant@localhost ud867]$ gradle groovy
Called the 1 arg function with 500
Called the  2 arg function with 500 and 100
:groovy

BUILD SUCCESSFUL

Total time: 6.277 secs

groovy groovy groovy

task groovy << {}

def foo = 6.5

println "foo has value. $foo"
println "Let's do same math. 5 + 6 = ${5 + 6}"
println "foo is of type: ${foo.class} and has value: $foo"

foo = "a string"
println "foo is new of type: ${foo.class} and has value: $foo"
[vagrant@localhost ud867]$ gradle groovy
Hello Java!
:groovy

BUILD SUCCESSFUL

Total time: 6.464 secs
[vagrant@localhost ud867]$ gradle groovy
foo has value. 6.5
Let's do same math. 5 + 6 = 11
foo is of type: class java.math.BigDecimal and has value: 6.5
foo is new of type: class java.lang.String and has value: a string
:groovy

BUILD SUCCESSFUL

Total time: 6.531 secs
task groovy << {}

def doubleIt(n){
	n + n // Note we don't need a return statement
}

def foo = 5
println "doubleIt($foo) = ${doubleIt(foo)}"

foo = "foobar"
println "doubleIt($foo) = ${doubleIt(foo)}"
[vagrant@localhost ud867]$ gradle groovy
doubleIt(5) = 10
doubleIt(foobar) = foobarfoobar
:groovy

BUILD SUCCESSFUL

Total time: 6.396 secs

Groovy Fundamentals

build.gradle

task groovy << {}
[vagrant@localhost ud867]$ gradle groovy
:groovy

BUILD SUCCESSFUL

Total time: 9.107 secs

task groovy << {}

println "Hello Groovy!"
task groovy << {}

class JavaGreeter {
	public static void sayHello(){
		System.out.println("Hello Java!");
	}
}

JavaGreeter greeter = new JavaGreeter()
greeter.sayHello()

Gradle Daemon

[root@localhost src]# gradle --stop
No Gradle daemons are running.
[root@localhost src]# gradle hello World
Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* What went wrong:
Task 'hello' not found in root project 'src'. Some candidates are: 'help'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 7.419 secs
[root@localhost src]# gradle hello World

FAILURE: Build failed with an exception.

* What went wrong:
Task 'hello' not found in root project 'src'. Some candidates are: 'help'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.66 secs