Gradle.Enemy at the gates

78
Gradle . Enemy at the gates . Сергей Моренец, [email protected] 23 мая 2014 г.

description

Grade 2. History and benefits comparing to other build management tools

Transcript of Gradle.Enemy at the gates

Page 1: Gradle.Enemy at the gates

Gradle.Enemy at the gates.

Сергей Моренец, [email protected]

23 мая 2014 г.

Page 2: Gradle.Enemy at the gates

About author• Works in IT since 2000

• 10 year of Java SE/EE experience

• Migrated to Gradle after 7 years of Ant/Maven

usage

• Regular speaker at Java conferences

Page 3: Gradle.Enemy at the gates

Agenda

• Builds tools overview

• Gradle under the microscope

• Examples

• Q & A

Page 4: Gradle.Enemy at the gates

Lost in selection

Page 5: Gradle.Enemy at the gates

Magic pill

Page 6: Gradle.Enemy at the gates

Gradle• Flexible yet model-driven JVM-based build tool

• Acknowledges and improves on the very best ideas

from Make, Ant, Ivy, Maven, Rake, Gant, Scons, SBT,

Leinengen, and Buildr

Page 7: Gradle.Enemy at the gates

Gradle

• Flexibility of Ant

• Dependency management from Ivy

• Intelligence of Maven

• Speed and hashing of Git

• Metaprogramming power of Groovy

Page 8: Gradle.Enemy at the gates

Apache Ant• Offers extreme flexibility

• Imposes no convention

• Lightweight dependency management

Page 9: Gradle.Enemy at the gates

Apache Ivy• Cross-platform dependency management

• Transitive dependencies

• Enhanced support of repositories

Page 10: Gradle.Enemy at the gates

Apache Maven

• Offers rigid standards and support for dependency

management

• More difficult and inflexible standards/procedures

• Declarative approach

Page 11: Gradle.Enemy at the gates

Issue #1. XML

• Large and complex files are hard to understand

• Hierarchical structure limits the expressiveness

of the format

• Good format for the data and complex for the flow

Page 12: Gradle.Enemy at the gates

Issues #2. Frameworkitis

• Frameworkitis is the disease that a framework wants

to do too much for you or it does it in a way that

you don't want but you can't change it

• The bigger the framework becomes, the greater the

chances that it will want to do too much, the bigger

the learning curves become, and the more difficult

it becomes to maintain it

• Group of small frameworks(toolkits) is better than

big framework

Page 13: Gradle.Enemy at the gates

Solution #1. Groovy

• An agile and dynamic language for the Java

Virtual Machine

• Makes modern programming features available to

Java developers with almost-zero learning curve

• Provides the ability to statically type

check and statically compile your code for

robustness and performance

• Supports Domain-Specific Languages and other

compact syntax

• Every Gradle build file is Groovy script

Page 14: Gradle.Enemy at the gates

Gradle

• First release in Apr 2008

• Current version 1.12 released in April 2014

• Used in Carrier, EADS, Hibernate, Grails, Groovy,

Spring-Security and Spring-Integration

Page 15: Gradle.Enemy at the gates

Hans Dockter• Founder of Gradle and Gradleware

• 13 years of experience as a software developer,

team leader, architect, trainer, and mentor

• Previously worked at Jboss and founded Jboss-IDE

• Holds a Diploma in Physics with a minor in Computer

Science

• Admirer of domain-driven-design

Page 16: Gradle.Enemy at the gates

Slogan

• Make the impossible possible

• Make the possible easy

• Make the easy elegant

Page 17: Gradle.Enemy at the gates

Gradle overview

• A flexible general purpose build tool

• Declarative builds and build-by-convention plugins

on top

• Multi-project support

• Powerful dependency management based on Ivy

• Programming tool

• Based on Groovy

• Gradle is written in Java with a Groovy DSL layer on

top

Page 18: Gradle.Enemy at the gates

Build structure

• Gradle build consists of one or more projects

• Project contains one or more tasks

• Task is fundamental unit of build activity

• Tasks are named collections of build instructions

• Tasks are the equivalent to Ant targets

• Task is made up of actions

Page 19: Gradle.Enemy at the gates

Hello world

• $ gradle helloWorld

Page 20: Gradle.Enemy at the gates

build.gradle

Page 21: Gradle.Enemy at the gates

Maven pom

Page 22: Gradle.Enemy at the gates

Build phases

Execution

Configuration

Initialization

Page 23: Gradle.Enemy at the gates

Initialization

• Gradle defines which projects are involved in build

• Project instance is defined for each involved

project

Page 24: Gradle.Enemy at the gates

Configuration

• Task objects are assembled into an internal object

model, usually called the DAG

• The build scripts of all projects which are part of the

build are executed

• If “configuration on demand” feature is enabled

then only relevant projects are configured

Page 25: Gradle.Enemy at the gates

Execution

• Gradle determines the subset of the tasks, created

and configured during the configuration phase

• Subset of tasks depends on the gradle command

argument and the contents of the current directory.

• Selected tasks are executed in the order required

by their dependency relationships

Page 26: Gradle.Enemy at the gates

DefaultTask

• dependsOn(task)

• doFirst(closure)

• doLast(closure)

• onlyIf(closure)

Page 27: Gradle.Enemy at the gates

Sample project

Page 28: Gradle.Enemy at the gates

Sample project

Page 29: Gradle.Enemy at the gates

Dependent tasks

Page 30: Gradle.Enemy at the gates

Task types

Copy Jar

JavaCompile JavaExec

JettyRun War

Page 31: Gradle.Enemy at the gates

Custom task

• Writes audit information at the end of the build

• Audit information includes project name and build

timestamp

• Audit files are located in the separate folder

Page 32: Gradle.Enemy at the gates

Custom task

Page 33: Gradle.Enemy at the gates

Custom task

Page 34: Gradle.Enemy at the gates

Custom task

Page 35: Gradle.Enemy at the gates

Custom task

Page 36: Gradle.Enemy at the gates

Multi-project builds

• Build where you build more than one project during

a single execution of Gradle

• Sub-projects should be defined in settings.gradle

• Settings file is analyzed in the initialization phase

when sub-projects are revealed and included into

DAG

• Sub-projects are sub-directories in the simplest case

Page 37: Gradle.Enemy at the gates

settings.gradle

Page 38: Gradle.Enemy at the gates

Plugin

• Reusable pieces of build logic

• Can be used in different projects/builds

• Can be written in Groovy, Java or Scala

Page 39: Gradle.Enemy at the gates

Plugin• Add tasks to the project

• Pre-configure added tasks with useful defaults

• Add dependency configurations to the project

• Add new properties and methods to existing type

via extensions

Page 40: Gradle.Enemy at the gates

Plugins

• Android

• AspectJ

• Flex

• Grails

• GWT

• JavaScript

• JAXB

• Jenkins

• SvnKit

• Tomcat

• Xslt

Page 41: Gradle.Enemy at the gates

Plugin import

Page 42: Gradle.Enemy at the gates

Packaging

• Build script

• buildSrc project

• rootProjectDir/buildSrc/src/main/groovy

• Standalone project

Page 43: Gradle.Enemy at the gates

Custom plugin

Page 44: Gradle.Enemy at the gates

Custom plugin

Page 45: Gradle.Enemy at the gates

Custom plugin

Page 46: Gradle.Enemy at the gates

Custom plugin

• $ gradle writeBuild

Page 47: Gradle.Enemy at the gates

Custom plugin

Page 48: Gradle.Enemy at the gates

Integration• Ant

• Maven

Page 49: Gradle.Enemy at the gates

Gradle and Ant• Gradle is often described as Groovy-based Ant.

• Competitor of Gant(Groovy Ant scripting)

• Share DAG concept

• Gradle tasks are similar to Ant targrets

• Gradle variables(typeless) are close to Ant

properties

Page 50: Gradle.Enemy at the gates

Hello Ant

Page 51: Gradle.Enemy at the gates

Ant import

Page 52: Gradle.Enemy at the gates

Gradle and Maven

Page 53: Gradle.Enemy at the gates

Gradle and Maven

Maven Coordinate Gradle Property Gradle Default

groupId group blank

artifactId name Project directory name

version version unspecified

name N/A N/A

description description null

Page 54: Gradle.Enemy at the gates

Comparison

Operation Gradle Maven

Build(sec) 21,2 24,2

Inc build(sec) 8,7 11

Build with tests(sec)

29 28

Clean(sec) 3,9 3,1

Distributive(Mb) 44 3

Page 55: Gradle.Enemy at the gates

Maven converter

• Create build.gradle file in the root folder

• Specify apply plugin: 'maven2Gradle' in the

build.gradle file.

• Run gradle maven2Gradle

Page 56: Gradle.Enemy at the gates

Maven Dependencies

Scopes:

• compile

• provided

• runtime

• test

• system

• import

Page 57: Gradle.Enemy at the gates

Dependencies

Page 58: Gradle.Enemy at the gates

Gradle Dependenciesconfigurations:

• compile

• default

• testCompile

• testRuntime

• archives

• runtime

Page 59: Gradle.Enemy at the gates

Scope flexibility

Page 60: Gradle.Enemy at the gates

Repositories• Gradle provides a bridge implementation to Ivy- or

Maven-formatted repositories

Page 61: Gradle.Enemy at the gates

Profiles• build.gradle

• dev-profile.gradle

• test-profile.gradle

• $ gradle –Pprofile=dev build

Page 62: Gradle.Enemy at the gates

Resource handling

Page 63: Gradle.Enemy at the gates

Resource handling

Page 64: Gradle.Enemy at the gates

Unit testing• Junit

• TestNG

• Spock

• Geb

• EasyB

Page 65: Gradle.Enemy at the gates

Skip tasks

• $ gradle -PskipTests

Page 66: Gradle.Enemy at the gates

Skip tasks

Page 67: Gradle.Enemy at the gates

Skip tasks

Page 68: Gradle.Enemy at the gates

Caching

• Gradle caches all compiles scripts by default

• Compiled scripts are put into .gradle folder

• Gradle uses compiled version if the script hasn’t

changed

• --recompile-scripts option discards cache

Page 69: Gradle.Enemy at the gates

Practical tasks

• Multiple projects

• Liquibase

• Deployment

Page 70: Gradle.Enemy at the gates

Multiple projects

• Huge project

• Multiple sub-projects/pom.xml files

• Complicated maintenance

Page 71: Gradle.Enemy at the gates

Liquibase

• Plugins for 2 and 3 versions

• Lightweight front-end for Liquibase command-line

• Gradle task for each Liquibase command

Page 72: Gradle.Enemy at the gates

Deployment

• Separate plugins for Jetty/Tomcat

• General plugin for multiple containers

Page 73: Gradle.Enemy at the gates

Pros• Native Java interaction

• Ant/Maven integration

• Transitive dependency management(based on

Maven/Ivy)

• Multiple third-party plugins(70+)

• Incremental builds

• Rapid development

Page 74: Gradle.Enemy at the gates

Cons

• Less efficient due to script nature

• Large learning curve

• Less community & industry support

Page 75: Gradle.Enemy at the gates

Future• Testing toolkit for integration into business logic

• Improved plugin portal and plugin development

• Execution of Maven builds/plugins at runtime

• Distributed testing

• Parallel and distributed execution

… to be continued

Page 76: Gradle.Enemy at the gates

Resources

Page 77: Gradle.Enemy at the gates

Practice

• https://github.com/hibernate/hibernate-orm

• https://github.com/SpringSource/spring-framework

• https://github.com/gradle/gradle

Page 78: Gradle.Enemy at the gates

Q&A

• Сергей Моренец, [email protected]