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

Similar documents
Getting Started with Gradle

Gradle. The Basics and Beyond

Enter 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.

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

Chapter 1. Installation and Setup

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

Software Engineering - Development Infrastructure 2. Kristjan Talvar Nortal

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

... Fisheye Crucible Bamboo

Lab 5 Exercise Build and continuous integration tools

MAVEN INTERVIEW QUESTIONS

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

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

C++ Binary Dependency Management with Gradle. Hugh Greene

Apache Buildr in Action

I Got My Mojo Workin'

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

Configuring Artifactory

Maven. INF5750/ Lecture 2 (Part II)

Package Management and Build Tools

Simplified Build Management with Maven

Class Dependency Analyzer CDA Developer Guide

Why switch exist-db from Ant to Maven?

Importing and Exporting

Build Automation Kurt Christensen

sites</distribsiteroot>

How to rebuild your build without reworking your work

Maven in the wild. An introduction to Maven

SpringSource Tool Suite 2.7.1

Maven 2 & Continuum. by Trygve Laugstøl

MAVEN MOCK TEST MAVEN MOCK TEST I

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

Distributing JavaFX Applications with Java WebStart and Artifactory

User Plugins. About Plugins. Deploying Plugins

Repository Management and Sonatype Nexus. Repository Management and Sonatype Nexus

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

Maven POM project modelversion groupid artifactid packaging version name

Selenium Testing Course Content

REASONS TO USE A BINARY REPOSITORY MANAGER WHEN DEVELOPING WITH. White Paper

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

Sonatype CLM Enforcement Points - Nexus. Sonatype CLM Enforcement Points - Nexus

CHAPTER 6. Java Project Configuration

Tuscany: Applying OSGi modularity after the fact

1) CB plugin for Jenkins 2) Requirements Mapping

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

SpringSource Tool Suite M2

DITA Gradle and Git. DITA-OT day Rotterdam

Maven 2 The powerful buildsystem. a presentation for EL4J developers by Martin Zeltner (MZE) November 2007

GAVIN KING RED HAT CEYLON SWARM

Tattletale. What is Tattletale? Enterprise archives JBoss Application Server 7 Putting it all together Roadmap

USING ARTIFACTORY TO MANAGE BINARIES ACROSS MULTI-SITE TOPOLOGIES

JVM Survival Guide. Hadi Hariri

TIBCO StreamBase 10.2 Building and Running Applications in Studio, Studio Projects and Project Structure. November 2017

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

gradle : Building Android Apps Mobel Meetup

Component based Development. Table of Contents. Notes. Notes. Notes. Web Application Development. Zsolt Tóth

COMP220/285 Lab sessions 1-3

What s new in IBM Operational Decision Manager 8.9 Standard Edition

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

Web Application Expectations

Apache Maven. Created by anova r&d bvba

Build Tools. Software Engineering SS 2007

FIWARE Advanced Middleware KIARA Documentation

DevOps examples on NonStop Tools Overview. Cor Geboers, ATC Consultant

This tutorial is designed for all Java enthusiasts who want to learn document type detection and content extraction using Apache Tika.

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

GradleFx Documentation

PHP Composer 9 Benefits of Using a Binary Repository Manager

MAVEN MOCK TEST MAVEN MOCK TEST III

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

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

FreeMarker in Spring Web. Marin Kalapać

Unable To The Artifact From Any Repository Maven-clean-plugin

9 Reasons To Use a Binary Repository for Front-End Development with Bower

Who Moved My Module? 1

MAVEN MOCK TEST MAVEN MOCK TEST IV

Client Server Communication

SpringSource Tool Suite 2.8.0

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

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

The jpos REST tutorial shows you how to use an out-of-the-box binary distribution of jpos to build a REST server that responds to an /echo call.

Spring Framework 5.0 on JDK 8 & 9

Skyway Builder 6.3 Reference

Modularity in Java. With OSGi. Alex Docklands.LJC January Copyright 2016 Alex Blewitt

Introduction to Programming (Java) 2/12

Who am I? Harlan Iverson. Programming enthusiast. Seeker of truth. Imperfect. I'll be wrong about some things. Please correct me if you can.

$HIVE_HOME/bin/hive is a shell utility which can be used to run Hive queries in either interactive or batch mode.

JBoss Tattletale 1.1 Developer's Guide

Exceptions and Libraries

Setting up a Maven Project

SECURE PRIVATE VAGRANT BOXES AND MORE WITH A BINARY REPOSITORY MANAGER. White Paper

Eclipse and Java 8. Daniel Megert Platform and JDT Lead Eclipse PMC Member IBM Rational Zurich Research Lab

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

BUILD AND DEPLOY SOA PROJECTS FROM DEVELOPER CLOUD SERVICE TO ORACLE SOA CLOUD SERVICE

Con$nuous Integra$on Development Environment. Kovács Gábor

Checking Out and Building Felix with NetBeans

Tuesday, April 26, 2011

AppDev StudioTM 3.2 SAS. Migration Guide

Transcription:

Gradle Workshop

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

The Labs Pairing is encouraged Solutions are available (but avoid cheating) Take your time and experiment First lab is Gradle installation/setup.

Objectives Gradle introduction Cover fundamental concepts Go beyond using Gradle builds Look at some of the newer features

Agenda The Gradle Project Groovy and Gradle Basics Tasks Task Inputs/Outputs Plugins Java Plugin Feature Tour Dependency Management (time permitting)

Gradle Introduction

Gradle Gradle is a build tool with a focus on whole project automation and support for multi-language development. Created in 2008 Implemented in Java (Groovy outer layer) 100% Free Open Source - Apache Standard License 2.0

Gradleware The company behind Gradle. Employs full time engineers Gradle consulting, support, development services etc. Training: online, public and in-house General build automation services Germany, Australia, Austria, Switzerland, Sweden, Czech Republic, UK, Canada and the US. http://www.gradleware.com

Gradle Project Open Source (Apache v2 license), free to use and modify Source code at github.com/gradle/gradle Community centered around forums.gradle.org

Gradle Documentation http://gradle.org/documentation User Guide 300+ pages, many complete samples In depth explanations, introductions single page HTML multi page HTML PDF DSL Reference ( gradle.org/docs/current/dsl/ ) API reference Frequently accessed Links through to Javadoc

Gradle Downloads http://gradle.org/downloads -bin distribution contains just the runtime. -all distribution contains the runtime, source, and all documentation.

Gradle 2.2 Released on Nov 10th, 2014 Latest version

Lab 01-setup

Groovy and Gradle Basics

Groovy Modern scripting language for the JVM Design goal is to be easily picked up by Java developers Reuse of Java semantics and API Compiles to byte code

Groovy Language Features Dynamic and optional static typing Compile time and runtime metaprogramming Operator overloading (e.g, <<, +=, -= on collections) Removes a lot of Java syntax noise (no semicolons etc.) First-class properties Closures (cf. Ruby blocks, Javascript functions, Java 8 lambdas) Many JDK library enhancements

More Groovy Groovy has many uses outside Gradle. Particularly good for testing Java code. Groovy In Action (2nd Ed) is a great source for more Groovy goodness.

Groovy and Gradle Gradle is implemented in Java, with an outer Groovy layer. Gradle build scripts are written in a Groovy-based DSL. Gradle tasks and plugins can be written in Groovy. apply plugin: "java" description = "My first Gradle build" version = 1.0 repositories { mavencentral() dependencies { compile "org.springframework:spring-core:4.0.5.release" test { jvmargs "-Xmx1024m"

Gradle Build Scripts Named build.gradle by default Must be valid Groovy syntax Can t be executed by plain Groovy runtime Are backed by a org.gradle.api.project object // does not compile: println 'Gradle // compiles, fails when run with Groovy or Gradle: println zipcode // compiles, fails when run with plain Groovy: println name

Tasks

Tasks Tasks are the basic unit of work in Gradle. Declared & configured by build scripts and plugins 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 helloworld { dolast { println "World!" dofirst { println "Hello" Executing a task means executing all of its actions (in order). Most tasks have one useful main action. dofirst() and dolast() can be used to further decorate this action.

Executing Tasks Execute tasks by specifying them on the command line. $ gradle helloworld :helloworld Hello world!

Abbreviated Task Name Execution Task names can be abbreviated on the command line. task mynameisprettylong { dolast { println "Long name" task someothertask { dolast { println "Other task" //running: $ gradle mnipl sothert Characters between word boundaries can be dropped.

Task Dependencies A task may require other tasks to run (i.e. produce or do something) before it can run Tasks and their dependencies form a directed acyclic graph (DAG) task compilejava task processresources task jar { dependson compilejava dependson processresources

Task Types Most tasks explicitly specify a type. task copyfiles(type: Copy) { // configure the task Task types (e.g. The default task type is Copy) provide a task action and a configuration API. DefaultTask, and does not have an action.

Task API task helloworld { onlyif { System.getProperty("be.nice") == "true" dolast { println "Hello" The onlyif() and dolast() methods are available for all tasks (i.e. are part of the Task interface). task copyfiles(type: Copy) { from "sourcedir" into "targetdir" The from() and into() methods are only available for Copy tasks. The task's API allows the task to be configured.

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

Lab 02-custom-tasks

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.

Lab 03-build-lifecycle

Quick Quiz Bonus question: What's happening here? task bar { dolast { dependson foo Will foo execute before bar?

Task Modeling tasks as functions Inputs/Outputs

Inputs and Outputs Most tasks can be described as functions Inputs : Files, configuration values Outputs: Files Modeling inputs and outputs enables powerful features: Do not rerun tasks that would produce the same outputs. Infer task dependency if one task's output becomes another task's input. Validate that task inputs exist before running a task. Create output directories that don't exist. Clean all outputs of a particular task. Etc. Built-in task types already describe their inputs and outputs. When implementing a custom task type, tell Gradle what its inputs and outputs are.

Input/Output Annotations class MyTask extends DefaultTask { @InputFile File text @InputFiles FileCollection path @InputDirectory File templates @Input String mode @OutputFile File result @OutputDirectory transformedtemplates boolean verbose // ignored @TaskAction generate() {...

Incremental Building A task can be skipped (UP-TO-DATE) if: Inputs have not changed since last run Outputs are still there and haven't changed Change detection is content (not time) based: Input/output files are hashed Content of input/output dirs is hashed Values of input properties are serialized

Dependency Inference Many Gradle types (e.g. FileCollection and ProjectDependency) implement Buildable. Any task input that is Buildable creates inferred task dependencies. task generatefiles { outputs.dir = "$builddir/generated-files" // same as @OutputDirectory dolast { /* generate files */ compilejava { classpath += generatefiles.outputs.files // -> generatefiles task copy(type: Copy) { from generatefiles // -> generatefiles into "somedir" jar { from sourcesets.main.output // -> compilejava, processresources from sourcesets.test.output // -> compiletestjava, processtestresources

Lab 04-task-input-output

Plugins

Plugins Plugins are reusable build logic. They can do everything a build script can do (and vice versa): Configure defaults Add and configure tasks Extend the build language Etc. Script plugins are applied by path: apply from: "$rootdir/gradle/integration-test.gradle" Binary plugins are applied by ID: apply plugin: "java"

External Plugins Need to be declared as build script dependencies. buildscript { repositories { mavencentral() dependencies { classpath "org.hidetake:gradle-ssh-plugin:0.3.7" apply plugin: "ssh"

Standard Gradle Plugins Gradle ships with many useful plugins. Some examples: java - compile, test, package, upload Java projects checkstyle - static analysis for Java code maven - uploading artifacts to Apache Maven repositories scala - compile, test, package, upload Scala projects idea and eclipse - generates metadata so IDEs understand the project application - support packaging your Java code as a runnable application c / cpp - support building native binaries using gcc, clang or visual-cpp Many more, listed in the Gradle User Guide.

The Java Plugin

Java Plugin The basis of Java development with Gradle. Introduces concept of source sets main and test source set conventions Compilation pre-configured Dependency management JUnit & TestNG testing Produces single JAR JavaDoc generation Publishing (Maven or Ivy format) IDE integration Standard lifecycle/interface

Source Sets A logical compilation/processing unit of sources. Java source files Non compiled source files (e.g. properties files) Classpath (compile & runtime) Output class files Compilation tasks sourcesets { main { java { srcdir "src/main/java" // default resources { srcdir "src/main/resources" // default

Lifecycle Tasks The java plugin provides a set of lifecycle tasks for common tasks. clean - delete all build output classes - compile code, process resources test - run tests assemble - make all archives (e.g. zips, jars, wars etc.) check - run all quality checks (e.g. tests + static code analysis) build - combination of assemble & check

Testing Built in support for JUnit and TestNG. Pre-configured test task Automatic test detection Forked JVM execution Parallel execution Configurable console output Human and CI server friendly reports

Lab 05-java-plugin

Feature Tour A small selection of useful features.

Gradle Wrapper A way to bootstrap Gradle installations. $ gradle wrapper gradle gradle-wrapper.jar gradle-wrapper.properties gradlew gradlew.bat $./gradlew build

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 optimization.

Init Plugin Create build from template, convert Maven build. $ gradle init --type java-library $ gradle init --type pom

Continue after Failure $ gradle build --continue Especially useful for CI builds.

Parallel Builds Run independent tasks from different projects in parallel. $ gradle build --parallel Incubating feature; some restrictions apply.

Q&A Thank you for attending the workshop Questions? Feedback? http://gradle.org http://gradleware.com http://www.gradle.org

Dependency Management

Dependency Management Gradle supports managed and unmanaged dependencies. Managed dependencies have identity and possibly metadata. Unmanaged dependencies are just anonymous files. Managed dependencies are superior as their use can be automated and reported on.

Unmanaged Dependencies dependencies { compile filetree(dir: "lib", include: "*.jar") Can be useful during migration.

Managed Dependencies dependencies { compile "org.springframework:spring-core:4.0.5.release" compile group: "org.springframework", name: "spring-web", version: "4.0.5.RELEASE" Group/Module/Version

Configurations Dependencies are assigned to configurations. configurations { // default with `java` plugin compile runtime testcompile testruntime dependencies { compile "org.springframework:spring-core:4.0.5.release" See Configuration in DSL reference.

Transitive Dependencies Gradle (by default) fetches dependencies of your dependencies. This can introduce version conflicts. Only one version of a given dependency can be part of a configuration. Options: Use default strategy (highest version number) Disable transitive dependency management Excludes Force a version Fail on version conflict Dependency resolution rules

Disable Transitives Per dependency dependencies { compile("org.foo:bar:1.0") { transitive = false Configuration-wide configurations { compile.transitive = false

Excludes Per dependency dependencies { compile "org.foo:bar:1.0", { exclude module: "spring-core" Configuration-wide configurations { compile { exclude module: "spring-core"

Version Forcing Per dependency dependencies { compile("org.springframework:spring-core:4.0.5.release") { force = true Configuration-wide configurations { compile { resolutionstrategy.force "org.springframework:spring-core:4.0.5.release"

Fail on Conflict Automatic conflict resolution can be disabled. configurations { compile { resolutionstrategy.failonversionconflict() If disabled, conflicts have to be resolved manually (using force, exclude etc.)

Cross Configuration Rules Configuration level rules can be applied to all configurations. configurations { all { resolutionstrategy.failonversionconflict() all is a special keyword, meaning all things in the configuration container.

Dependency Cache Default location: ~/.gradle/caches/. Multi process safe Source location aware Optimized for reading (finding deps is fast) Checksum based storage Avoids unnecessary downloading Finds local candidates Uses checksums/etags An opaque cache, not a repository.

Changing Dependencies Changing dependencies are mutable. Version numbers ending in -SNAPSHOT are changing by default. dependencies { compile "org.company:some-lib:1.0-snapshot" compile("org:somename:1.0") { changing = true Default TTL is 24 hours.

Dynamic Dependencies Dynamic dependencies do not refer to concrete versions. Can use Ivy symbolic versions. dependencies { compile "org.company:some-lib:2.+" compile "org:somename:latest.release" Default TTL is 24 hours.

Controlling Updates & TTL configurations.all { resolutionstrategy.cachechangingmodulesfor 4, "hours" resolutionstrategy.cachedynamicversionsfor 10, "minutes" --offline - don't look for updates, regardless of TTL --refresh-dependencies - look for updates, regardless of TTL

Dependency Reports View the dependency graph. $ gradle dependencies [--configuration «name»] View a dependency in the graph. $ gradle dependencyinsight --dependency «name» [--configuration «name»] Built in tasks.

Repositories Any Maven/Ivy repository can be used Very flexible layouts are possible for Ivy repositories repositories { jcenter() mavencentral() maven { name "codehaus" url "http://repository.codehaus.org" ivy { url "http://repo.mycompany.com" layout "gradle" // default flatdir(dirs: ["dir1", "dir2"])

Lab 06-dependencies

Uploading Upload your artifacts to any Maven/Ivy repository ivy.xml/pom.xml is generated Repository metadata (e.g. maven-metadata.xml) is generated

Uploading to Maven Repositories apply plugin: "maven" uploadarchives { repositories { mavendeployer { repository(url: "http://my.org/m2repo/") Provided by the maven plugin All Maven wagon protocols can be used For Artifactory, JFrog provides an artifactory-publish plugin