Top Banner
Gradle 2. Breaking stereotypes . Sergey Morenes, [email protected] March, 26 2015
89
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Gradle 2.breaking stereotypes.

Gradle 2.Breaking stereotypes.

Sergey Morenes, [email protected]

March, 26 2015

Page 2: Gradle 2.breaking stereotypes.

About author• Works in IT since 2000

• 11 year of Java SE/EE experience

• Migrated to Gradle after 7 years of Ant/Maven

usage

• Regular speaker at Java conferences

Page 3: Gradle 2.breaking stereotypes.

Agenda

• Builds tools overview

• Gradle under the microscope

• Examples

• Q & A

Page 4: Gradle 2.breaking stereotypes.

Lost in selection

Page 5: Gradle 2.breaking stereotypes.

Magic pill

Page 6: Gradle 2.breaking stereotypes.

Apache Ant• Offers extreme flexibility

• Imposes no convention

• Lightweight dependency management

Page 7: Gradle 2.breaking stereotypes.

Apache Ivy• Cross-platform dependency management

• Transitive dependencies

• Enhanced support of repositories

Page 8: Gradle 2.breaking stereotypes.

Apache Maven

• Offers rigid standards and support for dependency

management

• More difficult and inflexible standards/procedures

• Declarative approach

Page 9: Gradle 2.breaking stereotypes.

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 10: Gradle 2.breaking stereotypes.

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

• Share base syntax, type system, packages

hierarchy with Java

• Every Gradle build file is Groovy script

Page 11: Gradle 2.breaking stereotypes.

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 12: Gradle 2.breaking stereotypes.

Gradle

• First release in Apr 2008

• Current version 2.3 released in February 2015

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

Spring-Security and Spring-Integration, Netflix,

LinkedIn

• Default build tool for Android OS

Page 13: Gradle 2.breaking stereotypes.

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 14: Gradle 2.breaking stereotypes.

Slogan

• Make the impossible possible

• Make the possible easy

• Make the easy elegant

Page 15: Gradle 2.breaking stereotypes.

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

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

top

• Programming tool

• Based on Groovy

Page 16: Gradle 2.breaking stereotypes.

Development

Version Release date

Maven 1.0 2004

Maven 2.0 2005

Maven 3.0 2010

Maven 3.1 2013

Maven 3.3 2015

Page 17: Gradle 2.breaking stereotypes.

Development

Version Release date

Gradle 0.7 2009

Gradle 1.0 2012

Gradle 1.5 2013

Gradle 2.0 2014

Gradle 2.3 2015

Page 18: Gradle 2.breaking stereotypes.

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 2.breaking stereotypes.

Hello world

$ gradle helloWorld

Page 20: Gradle 2.breaking stereotypes.

build.gradle

Page 21: Gradle 2.breaking stereotypes.

Maven pom

Page 22: Gradle 2.breaking stereotypes.

Dependencies

Page 23: Gradle 2.breaking stereotypes.

Dependencies

Page 24: Gradle 2.breaking stereotypes.

Properties

Page 25: Gradle 2.breaking stereotypes.

Properties

Page 26: Gradle 2.breaking stereotypes.

Build phases

Execution

Configuration

Initialization

Page 27: Gradle 2.breaking stereotypes.

Initialization

• Gradle defines which projects are involved in build

• Project instance is defined for each involved

project

Page 28: Gradle 2.breaking stereotypes.

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 29: Gradle 2.breaking stereotypes.

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 30: Gradle 2.breaking stereotypes.

DefaultTask

• dependsOn(task)

• doFirst(closure)

• doLast(closure)

• onlyIf(closure)

Page 31: Gradle 2.breaking stereotypes.

Sample project

Page 32: Gradle 2.breaking stereotypes.

Sample project

Page 33: Gradle 2.breaking stereotypes.

Dependent tasks

Page 34: Gradle 2.breaking stereotypes.

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 35: Gradle 2.breaking stereotypes.

Custom task

Page 36: Gradle 2.breaking stereotypes.

Custom task

Page 37: Gradle 2.breaking stereotypes.

Custom task

Page 38: Gradle 2.breaking stereotypes.

Custom task

Page 39: Gradle 2.breaking stereotypes.

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 40: Gradle 2.breaking stereotypes.

settings.gradle

Page 41: Gradle 2.breaking stereotypes.

Plugin

• Reusable pieces of build logic

• Can be used in different projects/builds

• Can be written in Groovy, Java or Scala

Page 42: Gradle 2.breaking stereotypes.

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 43: Gradle 2.breaking stereotypes.

Plugins

• Android

• AspectJ

• Flex

• Grails

• GWT

• JavaScript

• JAXB

• Jenkins

• SvnKit

• Tomcat

• Xslt

Page 44: Gradle 2.breaking stereotypes.

Plugin import

Page 45: Gradle 2.breaking stereotypes.

Plugin portal

Page 46: Gradle 2.breaking stereotypes.

Plugin import

Page 47: Gradle 2.breaking stereotypes.

C++ support

Page 48: Gradle 2.breaking stereotypes.

Packaging

• Build script

• buildSrc project

• rootProjectDir/buildSrc/src/main/groovy

• Standalone project

Page 49: Gradle 2.breaking stereotypes.

Custom plugin

Page 50: Gradle 2.breaking stereotypes.

Custom plugin

Page 51: Gradle 2.breaking stereotypes.

Custom plugin

Page 52: Gradle 2.breaking stereotypes.

Custom plugin

• $ gradle writeBuild

Page 53: Gradle 2.breaking stereotypes.

Custom plugin

Page 54: Gradle 2.breaking stereotypes.

Integration• Ant

• Maven

• Ivy

Page 55: Gradle 2.breaking stereotypes.

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 56: Gradle 2.breaking stereotypes.

Hello Ant

Page 57: Gradle 2.breaking stereotypes.

Ant import

Page 58: Gradle 2.breaking stereotypes.

Gradle and Maven

Page 59: Gradle 2.breaking stereotypes.

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 60: Gradle 2.breaking stereotypes.

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 61: Gradle 2.breaking stereotypes.

Maven converter

• Create build.gradle file in the root folder

• Specify apply plugin: 'maven2Gradle' in the

build.gradle file.

• Run gradle maven2Gradle

Page 62: Gradle 2.breaking stereotypes.

Repositories

• Maven and Ivy support

Page 63: Gradle 2.breaking stereotypes.

Maven Scope

Page 64: Gradle 2.breaking stereotypes.

Maven Scope

Scopes:

• compile

• provided

• runtime

• test

• system

• import

Page 65: Gradle 2.breaking stereotypes.

Dependencies

Page 66: Gradle 2.breaking stereotypes.

Configurations

• compile

• default

• testCompile

• testRuntime

• archives

• runtime

Page 67: Gradle 2.breaking stereotypes.

Scope flexibility

Page 68: Gradle 2.breaking stereotypes.

Profiles

• build.gradle

• dev-profile.gradle

• test-profile.gradle

• $ gradle –Pprofile=dev build

Page 69: Gradle 2.breaking stereotypes.

Resource handling

Page 70: Gradle 2.breaking stereotypes.

Resource handling

Page 71: Gradle 2.breaking stereotypes.

Unit testing

• Junit

• TestNG

• Spock

• Geb

• EasyB

Page 72: Gradle 2.breaking stereotypes.

Skip tasks

• $ gradle -PskipTests

Page 73: Gradle 2.breaking stereotypes.

Skip tasks

Page 74: Gradle 2.breaking stereotypes.

Skip tasks

Page 75: Gradle 2.breaking stereotypes.

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 76: Gradle 2.breaking stereotypes.

GUI

Page 77: Gradle 2.breaking stereotypes.

Daemon

• Improves startup and execution time of Gradle

• Initial Gradle command forks daemon process

• Subsequent Gradle commands reuse the build

daemon

• If daemon is currently busy then new daemon

process is started on-demand

• Useful for small tasks execution

• Expires after 3 hours of idle time

Page 78: Gradle 2.breaking stereotypes.

Practical tasks

• Multiple projects

• Liquibase

• Deployment

Page 79: Gradle 2.breaking stereotypes.

Multiple projects

• Huge project

• Multiple sub-projects/pom.xml files

• Complicated maintenance

Page 80: Gradle 2.breaking stereotypes.

Liquibase

• Plugins for 2 and 3 versions

• Lightweight front-end for Liquibase command-line

• Gradle task for each Liquibase command

Page 81: Gradle 2.breaking stereotypes.

Deployment

• Separate plugins for Jetty/Tomcat

• General plugin for multiple containers

Page 82: Gradle 2.breaking stereotypes.

Cargo plugin

Page 83: Gradle 2.breaking stereotypes.

Cargo plugin

Page 84: Gradle 2.breaking stereotypes.

Pros

• Native Java support

• Ant/Maven integration

• Full IDE support

• Transitive dependency management(based on

Maven/Ivy)

• Multiple third-party plugins(70+)

• Incremental builds

• Rapid development

Page 85: Gradle 2.breaking stereotypes.

Cons

• Less efficient and possible compilation issues due to

script nature

• Large learning curve

• Less community & industry support

Page 86: Gradle 2.breaking stereotypes.

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 87: Gradle 2.breaking stereotypes.

Resources

Page 88: Gradle 2.breaking stereotypes.

Practice

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

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

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

Page 89: Gradle 2.breaking stereotypes.

Q&A

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