Java Unit Testing

apply plugin: 'java'

repositories {
	mavenCentral()
}

dependencies {
	testCompile 'junit:junit:4.11'
}

[vagrant@localhost ud867]$ gradle test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE

BUILD SUCCESSFUL

Total time: 7.617 secs

Dependency Reports

apply plugin: 'java'

repositories {
	mavenCentral()
}

dependencies {
	compile 'org.springframework:spring-core:4.1.1.RELEASE'
}
[vagrant@localhost ud867]$ gradle dependencies
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies

compile - Compile classpath for source set 'main'.
Download http://repo1.maven.org/maven2/org/springframework/spring-core/4.1.1.RELEASE/spring-core-4.1.1.RELEASE.pom
Download http://repo1.maven.org/maven2/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.pom
Download http://repo1.maven.org/maven2/org/apache/commons/commons-parent/28/commons-parent-28.pom
Download http://repo1.maven.org/maven2/org/apache/apache/13/apache-13.pom
\--- org.springframework:spring-core:4.1.1.RELEASE
     \--- commons-logging:commons-logging:1.1.3

default - Configuration for default artifacts.
\--- org.springframework:spring-core:4.1.1.RELEASE
     \--- commons-logging:commons-logging:1.1.3

runtime - Runtime classpath for source set 'main'.
\--- org.springframework:spring-core:4.1.1.RELEASE
     \--- commons-logging:commons-logging:1.1.3

testCompile - Compile classpath for source set 'test'.
\--- org.springframework:spring-core:4.1.1.RELEASE
     \--- commons-logging:commons-logging:1.1.3

testRuntime - Runtime classpath for source set 'test'.
\--- org.springframework:spring-core:4.1.1.RELEASE
     \--- commons-logging:commons-logging:1.1.3

BUILD SUCCESSFUL

Total time: 12.807 secs

$ gradle dependencies –configuration runtime
$ gradle dependencyInsight –dependency commons-logging

apply plugin: 'java'

repositories {
	mavenCentral()
}

dependencies {
	compile 'commons-logging:commons-logging:1.1.3'
	testCompile 'junit:junit:4.11'
}
[vagrant@localhost ud867]$ gradle dependencies
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies

compile - Compile classpath for source set 'main'.
\--- commons-logging:commons-logging:1.1.3

default - Configuration for default artifacts.
\--- commons-logging:commons-logging:1.1.3

runtime - Runtime classpath for source set 'main'.
\--- commons-logging:commons-logging:1.1.3

testCompile - Compile classpath for source set 'test'.
Download http://repo1.maven.org/maven2/junit/junit/4.11/junit-4.11.pom
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom
+--- commons-logging:commons-logging:1.1.3
\--- junit:junit:4.11
     \--- org.hamcrest:hamcrest-core:1.3

testRuntime - Runtime classpath for source set 'test'.
+--- commons-logging:commons-logging:1.1.3
\--- junit:junit:4.11
     \--- org.hamcrest:hamcrest-core:1.3

BUILD SUCCESSFUL

Total time: 10.61 secs

Configure the Java Plugin

jar {
    manifest {
        attributes 'Implementation-Version': '1.0' // 2. Add manifest attribute
    }
}

Working with repositories

repositories {
	flatDir {
		dirs 'libs'
	}
}

repositories {
	mavenCentral()
	mavenLocal()
	jcenter()
}

repositories {
	maven {
		url 'https://repo.foo.org/m2'
	}
}

repositories {
	ivy {
		url 'https://repo.foo.org/m2'
		credentials {
			username 'user'
			password 'secret'
		}
	}
}

Java Build Tasks

compile -> jar -> documentation -> test -> report

apply plugin: "java"
[vagrant@localhost ud867]$ gradle tasks
:tasks

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

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles classes 'main'.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles classes 'test'.

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

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

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'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Rules
-----
Pattern: clean: Cleans the output files of a task.
Pattern: build: Assembles the artifacts of a configuration.
Pattern: upload: Assembles and uploads the artifacts belonging to a configuration.

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

BUILD SUCCESSFUL

Total time: 8.759 secs

asemble

[vagrant@localhost ud867]$ gradle assemble
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble

BUILD SUCCESSFUL

Total time: 6.788 secs

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)