53
#jbcn2016 JBCN 2016 © S 1 GRADLE IN 45MIN Schalk Cronjé

Gradle in 45min - JBCN2-16 version

Embed Size (px)

Citation preview

Page 1: Gradle in 45min - JBCN2-16 version

#jbcn2016 JBCN 2016 © Schalk W. Cronjé1

GRADLE IN 45MINSchalk Cronjé

Page 2: Gradle in 45min - JBCN2-16 version

ABOUT ME

Email:

Twitter / Ello : @ysb33r

[email protected]

Gradle plugins authored/contributed to: VFS, Asciidoctor,JRuby family (base, jar, war etc.), GnuMake, Doxygen

Page 3: Gradle in 45min - JBCN2-16 version

2

GET YOUR DAILY GRADLE DOSE

@DailyGradle

#gradleTip

Page 4: Gradle in 45min - JBCN2-16 version

3

 

Page 5: Gradle in 45min - JBCN2-16 version

4 . 1

SDKMANManages parallel version of multiple SDKs

Mostly for (but not limited to) JVM-related systems

Windows users can use Posh-GVM (Powershell)

Windows 10 Bash ??

curl -s http://get.sdkman.io | bash

Page 6: Gradle in 45min - JBCN2-16 version

4 . 2

SDKMAN

SdkMan:

Posh-GVM:

@sdkmanager

http://sdkman.io

https://github.com/�ofreud/posh-gvm

Page 7: Gradle in 45min - JBCN2-16 version

4 . 3

5

GRADLEA next generation build-and-deploy pipeline

tool

Page 8: Gradle in 45min - JBCN2-16 version

6 . 1

MOST TRIVIAL JAVA PROJECTapply plugin: 'java'

Will look for sources under src/main/java

Page 9: Gradle in 45min - JBCN2-16 version

6 . 2

JAVA PROJECTrepositories { jcenter() }

apply plugin : 'java'

dependencies { testCompile 'junit:junit:4.1' }

Page 10: Gradle in 45min - JBCN2-16 version

GRADLE DEPENDENCY MANAGEMENTEasy to use

Flexible to con�gure for exceptions

Uses dependencies closure

First word on line is usually name of a con�guration.

Con�gurations are usually supplied by plugins.

Dependencies are downloaded from repositories

Maven coordinates are used as format

Page 11: Gradle in 45min - JBCN2-16 version

7 . 1

GRADLE REPOSITORIES

Speci�ed within a repositories closure

Processed in listed order to look for dependencies

jcenter() preferred open-source repo.

mavenLocal(), mavenCentral(), maven {}

Ivy repositories via ivy {}

Flat-directory repositories via flatDir

Page 12: Gradle in 45min - JBCN2-16 version

7 . 2

GRADLE REPOSITORIESrepositories { jcenter() mavenCentral() maven { url "https://plugins.gradle.org/m2/" } }

repositories { ivy { url 'file://path/to/repo' layout 'pattern', { artifact '[module]/[revision]/[artifact](.[ext])' ivy '[module]/[revision]/ivy.xml' } } }

Page 13: Gradle in 45min - JBCN2-16 version

7 . 3

GRADLE DSLUnderlying language is Groovy

You don’t need to be a Groovy expert to be a Gradle poweruser

Groovy doesn’t need ; in most cases

Groovy does more with less punctuation, making it an idealchoice for a DSL

In most cases lines that do not end on an operator isconsidered a completed statement.

Page 14: Gradle in 45min - JBCN2-16 version

8 . 1

GROOVY VS JAVA

In Groovy:

All class members are public by default

No need to create getters/setters for public �elds

Both static & dynamic typing supported

def means Object

Page 15: Gradle in 45min - JBCN2-16 version

8 . 2

CALLING METHODSclass Foo { void bar( def a,def b ) {} }

def foo = new Foo()

foo.bar( '123',456 ) foo.bar '123', 456

foo.with { bar '123', 456}

Page 16: Gradle in 45min - JBCN2-16 version

8 . 3

CALLING METHODS WITH CLOSURESclass Foo { void bar( def a,Closure b ) {}}

def foo = new Foo()

foo.bar( '123',{ println it } )

foo.bar ('123') { println it }

foo.bar '123', { println it }

Page 17: Gradle in 45min - JBCN2-16 version

8 . 4

MAPS IN GROOVY

Hashmaps in Groovy are simple to use

def myMap = [ plugin : 'java' ]

Maps are easy to pass inline to functions

project.apply( plugin : 'java' )

Which in Gradle can become

apply plugin : 'java'

Page 18: Gradle in 45min - JBCN2-16 version

8 . 5

LISTS IN GROOVY

Lists in Groovy are simple too

def myList = [ 'clone', ''http://github.com/ysb33r/GradleLectures' ]

This makes it possible for Gradle to do

args 'clone', 'http://github.com/ysb33r/GradleLectures'

Page 19: Gradle in 45min - JBCN2-16 version

8 . 6

CLOSURE DELEGATION IN GROOVY

When a symbol cannot be resolved within a closure,Groovy will look elsewhere

In Groovy speak this is called a Delegate.

This can be programmatically controlled via theClosure.delegate property.

Page 20: Gradle in 45min - JBCN2-16 version

8 . 7

CLOSURE DELEGATION IN GROOVYclass Foo { def target }

class Bar { Foo foo = new Foo() void doSomething( Closure c ) { c.delegate = foo c() } }

Bar bar = new Bar() bar.doSomething { target = 10 }

Page 21: Gradle in 45min - JBCN2-16 version

8 . 8

MORE CLOSURE MAGIC

If a Groovy class has a method 'call(Closure)`, the object canbe passed a closure directly.

class Foo { def call( Closure c) { /* ... */ } }

Foo foo = new Foo() foo {

println 'Hello, world'

}

// This avoids ugly syntax foo.call({ println 'Hello, world' })

Page 22: Gradle in 45min - JBCN2-16 version

8 . 9

CLOSURE DELEGATION IN GRADLE

In most cases the delegation will be entity the closure ispassed to.

Will also look at the Project and ext objects.

The Closure.delegate property allows plugin writersability to create beautiful DSLs

task runSomething(type : Exec ) { cmdline 'git' }

is roughly the equivalent of

ExecTask runSomething = new ExecTask() runSomething.cmdline( 'git' )

Page 23: Gradle in 45min - JBCN2-16 version

8 . 10

GRADLE TASKSCan be based upon a task type

task runSomething ( type : Exec ) {

command 'git'

args 'clone', 'https://bitbucket.com/ysb33r/GradleWorkshop'

}

Can be free-form

task hellowWorld << {

println 'Hello, world'

}

Page 24: Gradle in 45min - JBCN2-16 version

9 . 1

9 . 2

GRADLE TASKS : CONFIGURATION VS ACTION

Use of << {} adds action to be executed

Tasks supplied by plugin will have default actions

Use of {} con�gures a task

Page 25: Gradle in 45min - JBCN2-16 version

BUILDSCRIPTThe buildscript closure is special

It tells Gradle what to load into the classpath beforeevaluating the script itself.

It also tells it where to look for those dependencies.

Even though Gradle 2.1 has added a new way of addingexternal plugins, buildscript are much more �exible.

Page 26: Gradle in 45min - JBCN2-16 version

10

EXTENSIONS

Extensions are global con�guration blocks added byplugins.

Example: The jruby-gradle-base plugin will add ajruby block.

apply plugin: 'com.github.jruby-gradle.base'

jruby { defaultVersion = '1.7.11' }

Page 27: Gradle in 45min - JBCN2-16 version

11

12 . 1

GRADLE COMMAND-LINEgradle -v gradle -h gradle tasks gradle tasks --info

Page 28: Gradle in 45min - JBCN2-16 version

GRADLE WRAPPERUse wrapper where possible:

Eliminates need to install Gradle in order to build project

Leads to more reproducible builds

gradle wrapper --wrapper-version 2.12

./gradlew tasks

Page 29: Gradle in 45min - JBCN2-16 version

12 . 2

12 . 3

EXECUTING TASKS./gradlew <taskName1> <taskName2> ...

./gradlew build

Page 30: Gradle in 45min - JBCN2-16 version

DEPENDENCIESExamine dependencies involved with various con�gurations

./gradlew dependencies

Page 31: Gradle in 45min - JBCN2-16 version

12 . 4

13 . 1

SUPPORT FOR OTHER JVM LANGUAGES

Page 32: Gradle in 45min - JBCN2-16 version

GROOVY PROJECTrepositories { jcenter() }

apply plugin : 'groovy'

dependencies { compile 'org.codehaus.groovy:groovy-all:2.4.3'

testCompile ('org.spockframework:spock-core:1.0-groovy-2.4') { exclude module : 'groovy-all' } }

Page 33: Gradle in 45min - JBCN2-16 version

13 . 2

13 . 3

SCALA PROJECTrepositories { jcenter() }

apply plugin : 'scala'

dependencies { compile 'org.scala-lang:scala-library:2.11.8' }

Page 34: Gradle in 45min - JBCN2-16 version

13 . 4

BUILDING KOTLINplugins { id "com.zoltu.kotlin" version "1.0.1" }

dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.1-1"

}

Page 35: Gradle in 45min - JBCN2-16 version

RUNNING JRUBYplugins {

id 'com.github.jruby-gradle.base' version '1.2.1'

}

import com.github.jrubygradle.JRubyExec

dependencies {

jrubyExec "rubygems:colorize:0.7.7"

}

task printSomePrettyOutputPlease(type: JRubyExec) {

description "Execute our nice local print-script.rb"

script "${projectDir}/print-script.rb"

}

(Example from JRuby-Gradle project)

Page 36: Gradle in 45min - JBCN2-16 version

13 . 5

OTHER LANGUAGESC++ / C / ASM / Resources (built-in)

Clojure (plugin)

Frege (plugin)

Golang (plugin)

Gosu (plugin)

Ceyon (plugin)

Mirah (plugin)

Page 37: Gradle in 45min - JBCN2-16 version

14

SUPPORT FOR OTHER BUILDSYSTEMSANT (built-in)

GNU Make

MSBuild / xBuild

Grunt, Gulp

Anything else craftable via Exec or JavaExec task

Page 38: Gradle in 45min - JBCN2-16 version

15

BUILDING DOCUMENTATION

Javadoc, Groovydoc, Scaladoc (built-in)

Doxygen (C, C++) (plugin)

Markdown (plugin)

Asciidoctor (plugin)

Page 39: Gradle in 45min - JBCN2-16 version

16 . 1

16 . 2

BUILDING WITH ASCIIDOCTORplugins { id 'org.asciidoctor.convert' version '1.5.2' }

Page 40: Gradle in 45min - JBCN2-16 version

BUILDING WITH ASCIIDOCTORrepositories { jcenter() }

asciidoctor { sources { include 'example.adoc' }

backends 'html5'

}

Page 41: Gradle in 45min - JBCN2-16 version

16 . 3

PUBLISHING

Built-in to Maven, Ivy

Metadata publishing for native projects still lacking

Various plugins for AWS and other cloud storage

Plain old copies to FTP, SFTP etc.

Page 42: Gradle in 45min - JBCN2-16 version

17

18

MORE SUPPORT…

Of�cial buildsystem for Android

Docker

Hadoop

Page 43: Gradle in 45min - JBCN2-16 version

TOUR DE FORCE

Build a distributable application packaged as as ZIP

Runnable via shell script or batch �le

Contains classes written Java, Groovy & Kotlin source

Test source code with Spock Framework

Page 44: Gradle in 45min - JBCN2-16 version

19 . 1

TOUR DE FORCEplugins { id 'java' id 'groovy' id 'com.zoltu.kotlin' version '1.0.1' id 'application' }

repositories { jcenter() }

dependencies { compile 'org.codehaus.groovy:groovy-all:2.4.3' compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.1-1' testCompile 'org.spockframework:spock-core:1.0-groovy-2.4' }

version = '1.0' mainClassName = "gradle.workshop.HelloJava"

compileGroovy.dependsOn compileKotlin

Page 45: Gradle in 45min - JBCN2-16 version

19 . 2

ENDGAMEGradle is breaking new ground

Ever improving native support

Continuous performance improvements

Go �nd some more plugins at https://plugins.gradle.org

Page 46: Gradle in 45min - JBCN2-16 version

20

ABOUT THIS PRESENTATIONWritten in Asciidoctor

Styled by asciidoctor-revealjs extension

Built using:

Gradle

gradle-asciidoctor-plugin

gradle-vfs-plugin

Code snippets tested as part of build

Source code:https://github.com/ysb33r/GradleLectures/tree/Jbcn2016

Page 47: Gradle in 45min - JBCN2-16 version

21

WRITING PLUGINS?

https://leanpub.com/b/idiomaticgradle

Page 48: Gradle in 45min - JBCN2-16 version

22

THANK YOUEmail:

Twitter / Ello : @ysb33r

#idiomaticgradle

[email protected]

(Just in-case you need a buildtool consultant)

Page 49: Gradle in 45min - JBCN2-16 version

23

24 . 1

MIGRATIONS

Page 50: Gradle in 45min - JBCN2-16 version

24 . 2

ANT TO GRADLE

Re�ect Ant Build into Gradle

ant.importBuild('build.xml')

Page 51: Gradle in 45min - JBCN2-16 version

24 . 3

MAVEN TO GRADLE

Go to directory where pom.xml is and type

gradle init --type pom

Page 52: Gradle in 45min - JBCN2-16 version

25 . 1

USEFUL STUFF

Page 53: Gradle in 45min - JBCN2-16 version

PUBLISHING VIA VFSplugins { id "org.ysb33r.vfs" version "1.0" }

task publishToWebserver << {

vfs { cp "${buildDir}/website", "ftp://${username}:${password}@int.someserver.com/var/www", recursive : true, overwrite : true } }