Getting Started with Gradle

Similar documents
The Workshop. Slides (you have a copy in the zip) Practical labs Ask questions

Enter the Gradle Hans Dockter CEO, Gradleware Founder Gradle

Chapter 1. Installation and Setup

Gradle. The Basics and Beyond

JavaBasel 16. René Gröschke. Gradle 3.0 and beyond. Gradle 3.0 and beyond - #javabasel

Rocking the Gradle! Hans Dockter CEO, Gradleware Founder Gradle

Hello Gradle. TestNG, Eclipse, IntelliJ IDEA. Óbuda University, Java Enterprise Edition John von Neumann Faculty of Informatics Lab 2.

... Fisheye Crucible Bamboo

This tutorial explains how you can use Gradle as a build automation tool for Java as well as Groovy projects.

Gradle Leveraging Groovy for Building Java Applications. Hans Dockter Gradle Project Lead

Lab 5 Exercise Build and continuous integration tools

How to rebuild your build without reworking your work

Build Automation Kurt Christensen

MAVEN INTERVIEW QUESTIONS

Maven Introduction to Concepts: POM, Dependencies, Plugins, Phases

I Got My Mojo Workin'

Advanced Dependency Management with Gradle. Benjamin Muschko, Gradle Inc.

Software Engineering - Development Infrastructure 2. Kristjan Talvar Nortal

Hello Maven. TestNG, Eclipse, IntelliJ IDEA. Óbuda University, Java Enterprise Edition John von Neumann Faculty of Informatics Lab 2.

Static Analysis of Dynamic Languages. Jennifer Strater

SpringSource Tool Suite 2.7.1

Groovy & Grails Scripting for Modern Web Applications. Rohit Nayak Talentica Software

Package Management and Build Tools

SpringSource Tool Suite M2

Why switch exist-db from Ant to Maven?

Groovy. Extending Java with scripting capabilities. Last updated: 10 July 2017

Apache Pig Releases. Table of contents

Apache Buildr in Action

How to set up a continuous integration process in the cloud

Geo Catching Sprint #3 Kick-off

Working in Teams CS 520 Theory and Practice of Software Engineering Fall 2018

Exceptions and Libraries

Building a (resumable and extensible) DSL with Apache Groovy Jesse Glick CloudBees, Inc.

Selenium Testing Course Content

Maven in the wild. An introduction to Maven

Continuous Delivery with Grade. Hans Dockter CEO Gradle Inc., Founder

Continuous Integration (CI) with Jenkins

Maven POM project modelversion groupid artifactid packaging version name

DITA Gradle and Git. DITA-OT day Rotterdam

Topics covered. Introduction to Maven Maven for Dependency Management Maven Lifecycles and Plugins Hands on session. Maven 2

Index. Decomposability, 13 Deep reflection, 136 Dependency hell, 19 --describe-module, 39

#jenkinsconf. Jenkins user plugin. This time it's. Jenkins User Conference Israel. Shiran JFrog

C++ Binary Dependency Management with Gradle. Hugh Greene

S AMPLE CHAPTER IN ACTION. Benjamin Muschko. FOREWORD BY Hans Dockter MANNING

Semantic Versioning A Large Existing Codebase

1) CB plugin for Jenkins 2) Requirements Mapping

Teiid Designer User Guide 7.5.0

CS 520: VCS and Git. Intermediate Topics Ben Kushigian

Build Tools. Software Engineering SS A tool was needed. Agenda for today. Build tools. Software complexity. Build tools

IntelliJ IDEA Getting Started Guide for FIRST Robotics Competition

Class Dependency Analyzer CDA Developer Guide

FIWARE Advanced Middleware KIARA Documentation

Build Tools. Software Engineering SS 2007

Tuscany: Applying OSGi modularity after the fact

glu deployment automation platform July 2011 Yan Pujante in: blog:

Welcome to CSSE 220. We are excited that you are here:

RedBarrel Documentation

mimacom & LiferayDXP Campaign

ArcGIS Runtime SDK for Android: Building Apps. Shelly Gill

Android Sdk Install Documentation Eclipse. Ubuntu >>>CLICK HERE<<<

Index. Symbols. /**, symbol, 73 >> symbol, 21

Sams Teach Yourself ASP.NET in 24 Hours

JenkinsPipelineUnit. Test your Continuous Delivery Pipeline. Ozan Gunalp - Emmanuel Quincerot

CS 231 Data Structures and Algorithms, Fall 2016

DevOps and Maven. Eamonn de Leastar Dr. Siobhán Drohan Produced by:

HPE Application Performance Management

COPYRIGHTED MATERIAL

Atelier Java - J2. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Meet. Brian Stansberry Principal Software Engineer, Red Hat, Inc. June 10, Friday, June 14, 13

Make sure you have the latest Hive trunk by running svn up in your Hive directory. More detailed instructions on downloading and setting up

Checking Out and Building Felix with NetBeans

Thomas Pelaia II, Ph.D. XAL Workshop 2012 December 13, 2012 Managed by UT-Battelle for the Department of Energy

Simplified Build Management with Maven

Java: framework overview and in-the-small features

Groovy & Grails in Depth

Content. Development Tools 2(57)

What is Maven? Apache Maven is a software project management and comprehension tool (build, test, packaging, reporting, site, deploy).

Analyzing with SonarQube Scanner for Gradle

HPE Security Fortify Plugins for Eclipse

High-Level Wrapper for CloudKeeper. Architecture Configuration

MAVEN MOCK TEST MAVEN MOCK TEST III

Elegant Builds at Scale. Etienne Studer VP of Product Tooling, Gradle Inc.

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

Repository Management and Sonatype Nexus. Repository Management and Sonatype Nexus

vrealize Code Stream Plug-In SDK Development Guide

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Sonatype CLM - IDE User Guide. Sonatype CLM - IDE User Guide

What s new in IBM Operational Decision Manager 8.9 Standard Edition

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

CMSC 330: Organization of Programming Languages

What s new in Grails 2.0?

Oracle Forms and Oracle APEX The Odd Couple

JAVA. 1. Introduction to JAVA

Application prerequisites

Advanced Java Testing. What s next?

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

CS 11 java track: lecture 1

(Refer Slide Time: 0:48)

Transcription:

Getting Started with Gradle

Speaker Sterling Greene ( sterling@gradle.com) Principal Engineer, Gradle Inc Clone the example project

Agenda Gradle Project History Gradle Best Practices Gradle Basics Java Plugin

Gradle About the project

Gradle Gradle is a build and automation tool. Gradle can automate the building, testing, publishing, deployment and more of software packages or other types of projects such as generated static websites, generated documentation or indeed anything else. JVM based Implemented in Java (core) and Groovy (outer layers) http://www.gradle.org

Gradle Project Open Source, Apache v2 license - Completely free to use Source code on Github - github.com/gradle/gradle Active user community, centered around discuss.gradle.org Frequent releases (minor releases roughly every 4-8 weeks) Strong quality commitment Extensive automated testing (including documentation) - builds.gradle.org Backwards compatibility & feature lifecycle policy Thorough design and review process Developed by domain experts Gradle, Inc. staff and community contributors

Gradle Documentation User Guide 400+ pages, many samples single page HTML, multi page HTML, PDF Search tip: Full text search on single page HTML Build Language Reference ( gradle.org/docs/current/dsl/ ) Best starting point when authoring build scripts Javadoc-like, but higher-level Links into Javadoc and Groovydoc Gradle Distribution Includes user guide, build language reference, and many sample builds gradle-all vs. gradle-bin

Other Gradle Resources discuss.gradle.org Best place to ask questions Expanding knowledge base Books, several are free! StackOverflow, tagged with gradle

Gradle Evolution

Gradle 1.x 13 releases from June 2012 (1.0) to April 2014 (1.12) New features in each release Better performance More extensibility

Gradle 2.0 (July 2014) New baseline for backwards compatibility Removed deprecated APIs Upgraded to Groovy 2.x (from 1.8) Support for Java 8 Performance and extensibility are a major focus for Gradle 2.x Most build scripts still work between Gradle 1.x and 2.x Plugins can be built to support both 1.x and 2.x

Gradle 2.1 (September 2014) Gradle Plugin Portal introduced Searchable location for community Gradle plugins Incremental Java compilation

Gradle 2.2 (November 2014) Component selection rules Reject versions of particular dependencies based on group and name Module replacement rules Provide replacement dependencies (Google Collections -> Guava) Sharing configuration files across builds

Gradle 2.3 (February 2015) Artifact query resolution Fine grained access to dependency artifacts

Gradle 2.4 (May 2015) Fastest Gradle yet 20% to 40% drop in build times Reduced memory consumption for Java 7/8 builds Native (C/C++) parallel compilation 50% better build times Support for S3 backed Maven/Ivy repositories

Gradle 2.5 (July 2015) Continuous build (-t) Automatically rebuild when sources change Dependency substitution rules Replace external dependency with a local subproject

Gradle 2.6 (August 2015) Play 2.3.x support Hot-reload support when in continuous build Introduction of Gradle TestKit Test plugins and build logic with functional tests

Gradle 2.7 (September 2015) Preemptive authenication with Maven repositories (e.g., GitHub) Play 2.4.x support TestKit improvements

Gradle 2.8 (October 2015) Groovy 2.4.4 upgrade Compiler daemon reuse when in continuous build Faster incremental build For very large projects (100k's of sources), dramatic drop in no-op build times

Gradle 2.9 (ETA: November 2015) More TestKit improvements Even faster incremental build

Running Gradle

Getting Information Print basic help information: $ gradle help Print command line options: $ gradle -? Print the available tasks in a project: $ gradle tasks

Best Practices for Running Gradle Always use the wrapper Prefer using the daemon for desktop builds

Gradle Wrapper A way to bootstrap Gradle installations. gradle-wrapper.jar: Micro-library for downloading distribution gradle-wrapper.properties: Defines distribution download URL and options gradlew: Gradle executable for *nix systems gradlew.bat: Gradle executable for Windows systems Usage example: $./gradlew build

Wrapper task Wrapper task is built-in and generates: wrapper scripts wrapper jar wrapper properties $ gradle wrapper --gradle-version=2.6

Gradle Build Daemon Makes builds start faster. Enable with --daemon command line option org.gradle.daemon=true in gradle.properties -Dorg.gradle.daemon=true in GRADLE_OPTS Force shutdown with gradle --stop Will be used in the future for more optimizations.

Tasks

Tasks Tasks are the basic unit of work in Gradle. created & configured by the user executed by Gradle task helloworld { dolast { println "Hello World!" task is a keyword in the Gradle DSL. All tasks implement the Task interface.

Task Actions Tasks have a list of actions. task hello { dolast { println "World!" dofirst { println "Hello" Most tasks have one useful main action. dolast() and dofirst() can be used to decorate the action.

Quiz What would we expect to happen with dolast() if a preceeding action fails? What would we expect to happen with dofirst() if the task is skipped?

Quiz (Answers) What would we expect to happen with dolast() if a preceeding action fails? The task fails execution and the rest of the task's actions are not executed. What would we expect to happen with dofirst() if the task is skipped? The dofirst action is never executed.

Abbreviated Task Name Execution Save your fingers by only typing the bare minimum to identify a task. task mynameisprettylong { dolast { println "long task!" > gradle mnipl It understands camel case.

DSL Syntax and Tasks // << is synonymous with dolast() // we'll use dolast() from here on task hello << { println "Hello" // access existing task via its name hello.dependson othertask // configure existing task via closure hello { dependson othertask // configure new task task greet { dependson othertask dolast { println "Hello Gradler!"

Task Types You will usually use tasks of a certain type, that provide useful behavior (e.g. copy files). task copyfiles(type: Copy) { // Only configuration (actions are defined by the type) from('somedirectory') into('anotherdirectory') Task is of type Copy. Configure it using its API. If you don't specify a type, you get a DefaultTask.

Task Types and API task hello { onlyif { day == "monday" dofirst { println "Hello" The onlyif() method is a method of all tasks (i.e. part of Task interface). task copy(type: Copy) { from "somedir" into "anotherdir" The from() method here is part of the Copy API. The task API allows the task to be configured.

Quiz What does each individual line do? // task whatamidoing // whatamidoing // tasks.whatamidoing // whatamidoing { // // whatamidoing << {

Quiz (Answers) What does each individual line do? // Defines a task called 'whatamidoing' task whatamidoing // Refers to a task called 'whatamidoing' whatamidoing // Refers to a task called 'whatamidoing' through the TaskContainer tasks.whatamidoing // Performs additional configuration on a task called 'whatamidoing' whatamidoing { // Adds an action to the list of actions to task called 'whatamidoing' // This is the same as whatamidoing.dolast { whatamidoing << {

Implementing Task Types POJO extending DefaultTask Declare action with @org.gradle.api.tasks.taskaction class FtpTask extends DefaultTask { String host = "docs.mycompany.com" @TaskAction void ftp() { // do something complicated

Task Type > Ad-hoc Task Prefer implementing task types to implementing ad-hoc tasks. Avoid global properties and methods Separate the imperative from the declarative Easy to refactor (e.g. from build script to Jar) Easier to utilize other Gradle features Ad-hoc tasks are OK for small simple tasks.

Task Dependencies Tasks can depend on each other Semantic relationship (A produces something that B consumes) Executed tasks form a directed acyclic graph task foo // multiple ways to declare task dependencies task bar(dependson: foo) // won't use this syntax from here on bar { dependson foo bar.dependson foo

Build Lifecycle Initialization Phase Configure environment (init.gradle, gradle.properties) Find projects and build scripts (settings.gradle) Configuration Phase Evaluate all build scripts Build object model (Gradle -> Project -> Task, etc.) Build task execution graph Execution Phase Execute (subset of) tasks A key concept to grasp.

Quiz What happens here? // // // // task bar { dolast { dependson foo

Quiz (Answers) What happens here? // Dependency configuration happens during execution phase // so 'bar' would execute without waiting for 'foo' // In the current version of Gradle, this throws an Exception // to catch this kind of error. task bar { dolast { dependson foo

Java Plugin

Java Plugins java-base java Provides additional Task types Defines rules for conventions Adds declarative elements to the DSL (e.g. SourceSet) Adds task instances to the project Adds default values to task instances Adds source sets for production and test code Configures the dependency management for Java projects (adds scopes for compile, runtime,...)

Clean Task By default clean deletes the builddir You can specify additional files to delete name: 'clean', type: Delete clean { delete "foodir", "bar.txt", filetree("texts").matching {...

Javadoc Task Provides all the options of the Javadoc command name: 'javadoc', type: Javadoc input: sourcesets.main.java, sourcesets.main.compileclasspath javadoc { maxmemory = "512M" include "org/gradle/api/**" title = "Gradle API $version"

Resources Tasks Usually configured via the source set Can use the powerful Copy API name: processresources, processtestresources type: Copy input: sourcesets.main(test).resources

Compile Tasks Usually configured via the source set Provides all the options of the Ant javac task name: compile, testcompile, type: JavaCompile input: sourcesets.main(test).java, sourcesets.main(test).compileclasspath compilejava { options.fork = true options.forkoptions.with { memorymaximumsize = "512M"

Classes Tasks Aggregates compile related tasks name: classes, testclasses, type: DefaultTask dependson: compile testcompile, processresources processtestresources

Jar Task Content of the Jar: production classes name: 'jar', type: Jar input: sourcesets.main.output jar { //you can add more content: from sourcesets.main.alljava from ziptree("lib/somejar.jar")

Test Task Support for JUnit and TestNG Parallel testing Custom fork frequency Test listeners Tests auto-detected in sourcesets.test.output name: 'test', type: Test input: sourcesets.test.output, sourcesets.test.runtimeclasspath

Test Task Example test { jvmargs "-Xmx512M" scanfortestclasses = false //disables auto-detection include "**/tests/special/**/*test.class" exclude "**/Old*Test.class" forkevery = 30 maxparallelforks = guessmaxforks() def guessmaxforks() { int processors = Runtime.runtime.availableProcessors() Math.max(2, processors.intdiv(2))

Test Task Listeners test { beforetest { desc -> // do something aftertest { desc, result -> // do something aftersuite { desc, result -> // do something

Multi-Project Builds

Multi-Project Builds Flexible directory layout Configuration injection Project dependencies & partial builds

Configuration Injection ultimateapp api services webservice shared subprojects { apply plugin: "java" dependencies { testcompile "junit:junit:4.7" test { jvmargs "-Xmx512M"

Filtered Injection ultimateapp api services webservice shared configure(nonwebprojects()) { jar.manifest.attributes Implementor: "Gradleware" def nonwebprojects() { subprojects.findall {!it.name.startswith("web")

Project Dependencies ultimateapp api services webservice shared dependencies { compile "commons-lang:commons-lang:2.4" compile project(":shared")

Partial Builds ultimateapp api services webservice shared >gradle build >gradle builddependents >gradle buildneeded

Name Matching Execution ultimateapp api services webservice shared >gradle build >gradle classes >gradle war

Task/Project Paths For projects and tasks there is a fully qualified path notation: : (root project) :clean (the clean task of the root project) :api (the api project) :services:webservice (the webservice project) :services:webservice:clean (the clean task of webservice) >gradle :api:classes

Defining a Multi-Project Build settings.gradle (location defines the root) root project is implicitly included //declare projects: include "api", "shared", "services:webservice" //Everything is configurable: //default: root dir name rootproject.name = "main" //default: 'api' dir project(":api").projectdir = file("/mylocation") //default: 'build.gradle' project(":shared").buildfilename = "shared.gradle"

DEMO Clone the example project

Thank You! Thank you for attending! Questions? Feedback? Gradle Home Get more help!