gradle - the enterprise automation tool

94
Gradle The Enterprise Automation Tool

Upload: izzet-mustafaiev

Post on 30-Jun-2015

732 views

Category:

Software


4 download

DESCRIPTION

Here are slides from basic training for Gradle. This training is aimed to help Java Developers to get hands-on experience to use Gradle as a primary build tool for Java source code starting from simple compilation continuing with different kinds of tests and finishing with code quality analysis and artefacts publishing.

TRANSCRIPT

Page 1: Gradle  - the Enterprise Automation Tool

Gradle The

Enterprise Automation Tool

Page 2: Gradle  - the Enterprise Automation Tool

● SA at EPAM Systems

● primary skill is Java

● hands-on-coding with Groovy, Ruby

● trying to learn some Scala and Erlang/Elixir

● passionate about agile, clean code and devops

Izzet Mustafayev@EPAM Systems@webdizz webdizz izzetmustafaievhttp://webdizz.name

Page 3: Gradle  - the Enterprise Automation Tool

Agenda● Introduction

● Gradle

● Step by step by features

● Alternatives

● References

● Q&A

Page 4: Gradle  - the Enterprise Automation Tool

Introduction

Page 5: Gradle  - the Enterprise Automation Tool
Page 6: Gradle  - the Enterprise Automation Tool
Page 7: Gradle  - the Enterprise Automation Tool

Continuous Integration

Page 8: Gradle  - the Enterprise Automation Tool

Principles

#1 Every change automatically pulled

Page 9: Gradle  - the Enterprise Automation Tool

#2 Every change automatically built

Principles

#1 Every change automatically pulled

Page 10: Gradle  - the Enterprise Automation Tool

#2 Every change automatically built

#1 Every change automatically pulled

#3 Every change in the code base line*

Principles

Page 11: Gradle  - the Enterprise Automation Tool

Benefits● Each change guarantees working code

● Each update should guarantee working

code ;)

● Do not delay epic merge

● Less bugs - depends on your tests

efficiency

● Allows to have code ready to go live

Page 12: Gradle  - the Enterprise Automation Tool

Drawbacks● Need to build infrastructure

● Need to build team culture

● Need to support/enhance infrastructure

● Overhead with writing a lot of different

kind of tests

Page 13: Gradle  - the Enterprise Automation Tool

Continuous Delivery

Page 14: Gradle  - the Enterprise Automation Tool

Principles

#1 Every commit can result in a release

Page 15: Gradle  - the Enterprise Automation Tool

Principles

#1 Every commit can result in a release

#2 Automate everything!

Page 16: Gradle  - the Enterprise Automation Tool

Principles

#1 Every commit can result in a release

#2 Automate everything!

#3 Automate a pain!

Page 17: Gradle  - the Enterprise Automation Tool

Principles

#1 Every commit can result in a release

#2 Automate everything!

#3 Automate a pain!

#4 Done means released

Page 18: Gradle  - the Enterprise Automation Tool

Benefits● Automatic delivery of business idea to

customer

● Easy going live deployment

● Less time spent on delivery - more profit

● More motivation to do more as you can

see what you can change/improve

Page 19: Gradle  - the Enterprise Automation Tool

Drawbacks● Big effort to implement changes for:

○ infrastructure rollout/rollback

○ database increment/rollback

○ decrease down time …towards 0

● Need to get customers to buy in

● Security policies

Page 20: Gradle  - the Enterprise Automation Tool

Gradle

Page 21: Gradle  - the Enterprise Automation Tool

2.2

Page 22: Gradle  - the Enterprise Automation Tool

Gradle- General purpose build system

Page 23: Gradle  - the Enterprise Automation Tool

Gradle- General purpose build system

- Comes with a rich DSL based on Groovy

Page 24: Gradle  - the Enterprise Automation Tool

Gradle- General purpose build system

- Comes with a rich DSL based on Groovy

- Follows ”build-by-convention” principles

Page 25: Gradle  - the Enterprise Automation Tool

Gradle- General purpose build system

- Comes with a rich DSL based on Groovy

- Follows ”build-by-convention” principles

- Built-in plug-ins for JVM languages, etc

Page 26: Gradle  - the Enterprise Automation Tool

Gradle- General purpose build system

- Comes with a rich DSL based on Groovy

- Follows ”build-by-convention” principles

- Built-in plug-ins for JVM languages, etc

- Derives all the best from Ivy, Ant & Maven

Page 27: Gradle  - the Enterprise Automation Tool

Features

Page 28: Gradle  - the Enterprise Automation Tool

1. Download Gradle http://goo.gl/ktu7H6

2. Download zipped JDK from https://db.tt/HKW6GbI9

3. Unzip all above into D:\Images\Gradle folder

4. Open Command Window in dir D:\Images\Gradle

5. Execute

set JAVA_HOME=D:\Images\Gradle\jdk1.8.0_25

6. Execute

gradle-2.1\bin\gradle -v

7. Download and install Git from from http://goo.

gl/rv2G1X into D:\Images\Gradle\Git

#0 Installation

Page 29: Gradle  - the Enterprise Automation Tool

8. Clone Git repository

git\bin\git.exe clone https://github.

com/webdizz/sbs-gradle.git

#0 Installation

Page 30: Gradle  - the Enterprise Automation Tool

Groovy is under the hood

Page 31: Gradle  - the Enterprise Automation Tool

1. Reset code base to initial state

git reset --hard bed2227791

git clean -df

2. Create file build.gradle

3. Type

println 'Hello, World!'

4. Run

gradle

#1 Hello World!

Page 32: Gradle  - the Enterprise Automation Tool

Declarative

Page 33: Gradle  - the Enterprise Automation Tool

1. Run to see default tasks list

gradle tasks

2. Replace build.gradle file content with

apply plugin: 'java'

3. Run to see new available tasks

gradle tasks

4. Checkout step s2_prepare

5. Run to build Java source code

gradle build

6. Explore directory build

#2 Create simple build

Page 34: Gradle  - the Enterprise Automation Tool

Flexible Execution

Page 35: Gradle  - the Enterprise Automation Tool

1. Run task with part of name

gradle ta

2. Run task with part of name to clean and compile

gradle cle tC

3. Run task with part of name to clean and compile and

exclude processTestResources

gradle cle tC -x pTR

4. Get details for task

gradle -q help --task clean

#3 Execute tasks

Page 36: Gradle  - the Enterprise Automation Tool

1. Run task

gradle tasks

2. Run task to generate wrapper

gradle wrapper

3. Run tasks using wrapper

./gradlew tasks

4. Customize task wrapper to use another Gradle version

task wrapper(type: Wrapper) {

gradleVersion = '2.2.1-rc-1'

}

5. Check Gradle version

./gradlew -v

#4 Use wrapper

Page 37: Gradle  - the Enterprise Automation Tool

Multi-module Structure

Page 38: Gradle  - the Enterprise Automation Tool

1. Checkout step s5_prepare

2. Add directory common

3. Move src to common

4. Create common/build.gradle for Java

5. Add new module to settings.gradle

include ':common'

6. Run build

./gradlew clean build

7. Run task for module

./gradlew :com:compJ

#5 Create multi-module build

Page 39: Gradle  - the Enterprise Automation Tool

Dependency Management

Page 40: Gradle  - the Enterprise Automation Tool

Gradle- compile - to compile source

Page 41: Gradle  - the Enterprise Automation Tool

Gradle- compile - to compile source

- runtime - required by classes at runtime

Page 42: Gradle  - the Enterprise Automation Tool

Gradle- compile - to compile source

- runtime - required by classes at runtime

- testCompile - to compile test sources

Page 43: Gradle  - the Enterprise Automation Tool

Gradle- compile - to compile source

- runtime - required by classes at runtime

- testCompile - to compile test sources

- testRuntime - required to run the tests

Page 44: Gradle  - the Enterprise Automation Tool

1. Add repositories to download dependencies from to

build.gradle

allprojects { currProject ->

repositories {

mavenLocal()

mavenCentral()

jcenter()

maven {url 'http://repo.mycompany.com/’}

}

}

#6 Dependencies

Page 45: Gradle  - the Enterprise Automation Tool

1. Add common dependencies for all subprojects in build.

gradle

subprojects {

apply plugin: 'java'

dependencies {

compile 'org.slf4j:slf4j-api:1.7.7'

testCompile

'org.mockito:mockito-core:1.9.5',

'junit:junit:4.12-beta-1'

}

}

#6.1 Dependencies

Page 46: Gradle  - the Enterprise Automation Tool

1. Add dependencies for concrete module in

common/build.gradle

dependencies {

compile

'org.projectlombok:lombok:1.14.4'

}

#6.2 Dependencies

Page 47: Gradle  - the Enterprise Automation Tool

1. List project dependencies

./gradlew :common:dependencies

#6.3 Dependencies

Page 48: Gradle  - the Enterprise Automation Tool

Configuration

Page 49: Gradle  - the Enterprise Automation Tool

1. Extract common configuration parameters to gradle .

properties file

lombokVersion = 1.14.4

build.gradle file

dependencies {

compile

“org.projectlombok:lombok:$lombokVersion”

}

#7 Configuration

Page 50: Gradle  - the Enterprise Automation Tool

1. Parameterise execution for custom task :printParameter

in build.gradle

task printParameter {

println givenParameter

}

2. Add parameter default value to gradle.properties

3. Execute task

./gradlew -q :printParameter -PgivenParameter=hello

#7.1 Configuration

Page 51: Gradle  - the Enterprise Automation Tool

Rich API

Page 52: Gradle  - the Enterprise Automation Tool

#8 Rich API

Page 53: Gradle  - the Enterprise Automation Tool

● Lifecycle● Create a Settings instance for the build.

● Evaluate the settings.gradle script, if present, against the Settings

object to configure it.

● Use the configured Settings object to create the hierarchy of Project

instances.

● Finally, evaluate each Project by executing its build.gradle file, if

present, against the project. The project are evaluated in such order

that a project is evaluated before its child projects.

Page 54: Gradle  - the Enterprise Automation Tool

● Tasks● A project is essentially a collection of Task objects.

● Each task performs some basic piece of work.

● Dependencies● A project generally has a number of dependencies.

● Project generally produces a number of artifacts, which other projects

can use.

Page 55: Gradle  - the Enterprise Automation Tool

● Plugins● Plugins can be used to modularise and reuse project configuration.

● Properties● Any property or method which your script uses is delegated through to

the associated Project object.

● A project has 5 property 'scopes'.

● Dynamic Methods● A project has 5 method 'scopes'.

Page 56: Gradle  - the Enterprise Automation Tool

More Tests

Page 57: Gradle  - the Enterprise Automation Tool

1. Add integration test source sets in file gradle/integTest.

gradle

sourceSets {

integTest {

compileClasspath += main.output + test.output

runtimeClasspath += main.output + test.output

}

}

#8.1 Rich API

Page 58: Gradle  - the Enterprise Automation Tool

2. Add integration test configurations in file

gradle/integTest.gradle

configurations {

integTestCompile.extendsFrom testCompile

integTestRuntime.extendsFrom testRuntime

}

3. Include extension for subprojects in file build.gradle

apply from: file("${rootProject.projectDir}

/gradle/integTest.gradle")

#8.1 Rich API

Page 59: Gradle  - the Enterprise Automation Tool

3. Add integration test task in file gradle/integTest.gradle

task integTest(type: Test){

testClassesDir = sourceSets.integTest.output.

classesDir

classpath = sourceSets.integTest.

runtimeClasspath

shouldRunAfter 'test'

}

check.dependsOn(integTest)

4. Execute integration tests

./gradlew integTest

#8.1 Rich API

Page 60: Gradle  - the Enterprise Automation Tool

1. Open build.gradle to add dependency for one task from another

printParameter.dependsOn 'help'

2. Run printParameter task

./gradlew printParameter

#8.2 Tasks Dependencies

Page 61: Gradle  - the Enterprise Automation Tool

1. Open build.gradle to add ordering for one task from another

task areTestsExist {

if ([ file("${projectDir}/src/test/java").

listFiles() ].isEmpty()) {

println 'Test directory is empty'

} else {

println 'Test directory is not empty, will

execute tests'

}

}

test.mustRunAfter areTestsExist

2. Run printParameter task

./gradlew printParameter

#8.3 Tasks Ordering*

Page 62: Gradle  - the Enterprise Automation Tool

2. Run test task

./gradlew test

#8.3 Tasks Ordering*

Page 63: Gradle  - the Enterprise Automation Tool

1. Add rule to validate running of integration tests tasktasks.addRule('Check correctness of running tests'){ String

taskName ->

gradle.taskGraph.whenReady{

Map<String, String> args = gradle.startParameter.

systemPropertiesArgs

gradle.taskGraph.allTasks.each { Task task ->

if (task.name.contains('integTest') && !args.

containsKey('profile')) {

throw new org.gradle.api.tasks.

StopExecutionException("Profile was not specified to run

tests (-Dprofile=ci).")

}

}

}

}

#8.4 Rules

Page 64: Gradle  - the Enterprise Automation Tool

2. Run check task to have failure

./gradlew check

3. Run check task with expected parameter

./gradlew check -Dprofile=ci

#8.4 Rules

Page 65: Gradle  - the Enterprise Automation Tool

Parallel Execution

Page 66: Gradle  - the Enterprise Automation Tool

1. Switch to 9th step and execute next command

./gradlew test --parallel

2. Try to modify amount of executable threads

./gradlew test --parallel --parallel-threads=3

#9 Parallel builds

Page 67: Gradle  - the Enterprise Automation Tool

Incremental Builds

Page 68: Gradle  - the Enterprise Automation Tool

1. Create file gradle/releaseNotes.gradle to add task for release notes

ext.destDir = new File(buildDir, 'releaseNotes')

ext.releaseNotesTemplate = file('releaseNotes.tmpl.txt')

tasks.create(name: 'copyTask', type: org.gradle.api.tasks.Copy) {

from releaseNotesTemplate

into destDir

doFirst {

if (!destDir.exists()) {

destDir.mkdir()

}

}

rename { String fileName ->

fileName.replace('.tmpl', '')

}

}

#10 Custom Inputs/Outputs

Page 69: Gradle  - the Enterprise Automation Tool

tasks.create('releaseNotes') {

inputs.file copyTask

outputs.dir destDir

}

2. Add releaseNotes.tmpl.txt file as a template for release notes

3. Apply configuration from gradle/releaseNotes.gradle in build.gradle

4. Let’s run releaseNotes task

./gradlew releaseNotes

#10 Custom Inputs/Outputs

Page 70: Gradle  - the Enterprise Automation Tool

1. Enhance release notes task to prepare nice release notes file

ext.changesFile = file('changes.txt')

ext.bugs = []

ext.features = []

changesFile.eachLine { String line ->

String bugSymbol = '#bug:'

String featureSymbol = '#feature:'

if (line.contains(bugSymbol)) {

bugs << line.replace(bugSymbol, '')

} else if (line.contains(featureSymbol)) {

features << line.replace(featureSymbol, '')

}

}

filter(org.apache.tools.ant.filters.ReplaceTokens,

tokens: [bugs: bugs.join("\n"),

features: features.join("\n")])

#10.1 Files filtering

Page 71: Gradle  - the Enterprise Automation Tool

Test Coverage

Page 72: Gradle  - the Enterprise Automation Tool

1. Add JaCoCo configuration in gradle/coverage.gradle apply plugin: "jacoco"

jacoco {

toolVersion = "0.7.2.201409121644"

}

check.dependsOn jacocoTestReport

jacocoTestReport {

dependsOn 'test'

reports {

xml.enabled true

csv.enabled false

html.enabled true

}

}

#11 Test Coverage

Page 73: Gradle  - the Enterprise Automation Tool

2. Apply configuration from gradle/coverage.gradle in build.gradle

3. Implement proper test for proper method4. Let’s run check task to collect coverage metrics

./gradlew check -Dprofile=ci

5. Open common/build/reports/jacoco/test/html/index.html file to overview coverage

#11 Test Coverage

Page 74: Gradle  - the Enterprise Automation Tool

1. Add JaCoCo configuration in gradle/coverage.gradle for integration teststask jacocoIntegrationTestReport(type: JacocoReport) {

dependsOn integTest

sourceSets sourceSets.main

executionData integTest

reports {

xml {

enabled true

destination

"$buildDir/reports/jacoco/integTest/jacocoIntegTestReport.xml"

}

csv.enabled false

html {

destination "$buildDir/reports/jacoco/integTest/html"

}

}

}

#11.1 Integration Test Coverage

Page 75: Gradle  - the Enterprise Automation Tool

check.dependsOn jacocoIntegrationTestReport

jacocoIntegrationTestReport.mustRunAfter jacocoTestReport

2. Let’s run check task to collect coverage metrics for integration tests as well

./gradlew check -Dprofile=ci

#11.1 Integration Test Coverage

Page 76: Gradle  - the Enterprise Automation Tool

Static Code Analysis

Page 77: Gradle  - the Enterprise Automation Tool

Ad-hoc, fast feedback

Page 78: Gradle  - the Enterprise Automation Tool

Ad-hoc, fast feedback

Over time

Page 79: Gradle  - the Enterprise Automation Tool

1. Add configuration in gradle/codeQuality.gradle for code quality analysis and apply configuration in build.gradle

subprojects {

apply plugin: 'findbugs'

findbugs {

ignoreFailures = true

toolVersion = '3.0.0'

}

apply plugin: 'pmd'

pmd {

toolVersion = '5.1.3'

}

}

#12 Static Code Analysis

Page 80: Gradle  - the Enterprise Automation Tool

2. Let’s run check task to collect code quality metrics

./gradlew check -Dprofile=ci

3. Open common/build/reports/pmd|findbugs/*.html

#12 Static Code Analysis

Page 81: Gradle  - the Enterprise Automation Tool

Artefacts Publishing

Page 82: Gradle  - the Enterprise Automation Tool

1. Add configuration in gradle/publishing.gradle for artefacts publishing and apply configuration in build.gradleapply plugin: 'maven-publish'

publishing {

repositories {

maven {

url "http://192.168.56.71:8080/artifactory/libs-release-local"

credentials {

username 'admin'

password 'password'

}

}

}

publications {

mavenJava(MavenPublication) {

groupId "name.webdizz.${rootProject.name}"

version = uploadVersion

from components.java

}

}

}

#13 Artefacts Publishing

Page 83: Gradle  - the Enterprise Automation Tool

2. Let’s run publish task to publish artefacts

./gradlew publish -PuploadVersion=1.1.1

3. Check artefact was uploaded at http://192.168.56.71:8080/artifactory*

#13 Artefacts Publishing

Page 84: Gradle  - the Enterprise Automation Tool

Plugable Architecture

Page 85: Gradle  - the Enterprise Automation Tool

● Build script

Visible for build file

● buildSrc/src/main/groovy

Visible for project

● Standalone project

Could be shared between projects using binary artefact

Page 86: Gradle  - the Enterprise Automation Tool

1. Create file PluginsPrinterPlugin.groovy in buildSrc/src/main/groovyimport org.gradle.api.Pluginimport org.gradle.api.Project

public class PluginsPrinterPlugin implements Plugin<Project> {

void apply(Project project) {

project.task('printPlugins') << {

println 'Current project has next list of plugins:'

ext.plugins = project.plugins.collect { plugin ->

plugin.class.simpleName

}

println plugins

}

}

}

#14 Plugins Printer

Page 87: Gradle  - the Enterprise Automation Tool

2. Apply plugin for all projects in build.gradle file

allprojects {

apply plugin: PluginsPrinterPlugin

}

3. Let’s run printPlugins task to print plugins activated for project

./gradlew printPlugins

#14 Plugins Printer

Page 88: Gradle  - the Enterprise Automation Tool

Alternatives

Page 89: Gradle  - the Enterprise Automation Tool

- build like you

code

Page 90: Gradle  - the Enterprise Automation Tool

- a software project management and comprehension tool

Page 91: Gradle  - the Enterprise Automation Tool

References

Page 93: Gradle  - the Enterprise Automation Tool

Q&A

Page 94: Gradle  - the Enterprise Automation Tool

Izzet Mustafayev@EPAM Systems@webdizz webdizz izzetmustafaievhttp://webdizz.name