iconuk 2015 - gradle up!

75
1 Gradle Up! René Winkelmeyer midpoints GmbH

Upload: rene-winkelmeyer

Post on 21-Jan-2018

696 views

Category:

Software


0 download

TRANSCRIPT

Page 1: ICONUK 2015 - Gradle Up!

1

Gradle Up!

René Winkelmeyer midpoints GmbH

Page 2: ICONUK 2015 - Gradle Up!

2

René Winkelmeyer Head of Development

midpoints GmbH http://www.midpoints.de IBM Advanced Business Partner IBM Design Partner (Notes Domino, Mobile, Verse) Apple Enterprise Developer und MDM Group Member Samsung Enterprise Alliance Partner Services - Enterprise Mobility Service - Mobile Device und Application Management - IBM Traveler und IBM Mobile Connect

About me

Page 3: ICONUK 2015 - Gradle Up!

3

•  Skype

muenzpraeger •  Twitter

muenzpraeger •  LinkedIn

muenzpraeger •  Slideshare

muenzpraeger

•  Web https://blog.winkelmeyer.com http://www.midpoints.de

•  Mail [email protected] [email protected]

OpenNTF •  File Navigator •  Generic NSF View Widget for IBM Connections

About me

René Winkelmeyer Head of Development

Page 4: ICONUK 2015 - Gradle Up!

4

Agenda

§  Build Automation – Why?

§  Why Gradle - and not Maven or Ant?

§  Theory First - Gradle Basics

§  Your first Gradle project – start small

§  Building a web application

§  Building an OSGi plug-in project

Page 5: ICONUK 2015 - Gradle Up!

5

Agenda

§  Build Automation – Why?

§  Why Gradle - and not Maven or Ant?

§  Theory First - Gradle Basics

§  Your first Gradle project – start small

§  Building a web application

§  Building an OSGi plug-in project

Page 6: ICONUK 2015 - Gradle Up!

6

Why this session?

Doing things for a first time?

Doing things for a second time?

Doing things more often?

Noteworthy: http://www.slideshare.net/HollyCummins/confessions-of-an-automation-addict

Shoot and forget!

Write down the needed steps.

Automate!

Page 7: ICONUK 2015 - Gradle Up!

7

Tasks excerpt in software development and delivery

Compile binary code Set version number

Execute integration tests

Generate JavaDoc

Resolve/update dependencies

Modify MANIFEST.MF

Deploy to QA system Generate production package Compile binary code

Automated code validation

Set binary file name

...

Debugging

Continous Integration

Deploy to different platforms Generate installers

Execute regression tests

Include documentation

Apply translations ...

... ...

...

...

Page 8: ICONUK 2015 - Gradle Up!

8

Tasks excerpt in software development and delivery

Compile binary code Set version number

Execute integration tests

Generate JavaDoc

Resolve/update dependencies

Modify MANIFEST.MF

Deploy to QA system Generate production package Compile binary code

Automated code validation

Set binary file name

...

Debugging

Continous Integration

Deploy to different platforms Generate installers

Execute regression tests

Include documentation

Apply translations ...

... ...

...

...

Page 9: ICONUK 2015 - Gradle Up!

9

Example A – Resolve/update dependencies

§  You want to use a specific 3rd party library, i. e. for supporting MIME SMTP Mailing (mail-1.3.4.jar)

Search, compile

and copy hell

mail-1.3.4

slf4j-3.1.1

commons-io-4.2.1

guava-17.1

log4j-2.3.4

Page 10: ICONUK 2015 - Gradle Up!

10

Example A – Resolve/update dependencies

§  The 3rd party library needs to be updated from mail-1.3.4.jar to mail-1.4.1.jar

Search, compile,

delete and copy hell

mail-1.4.1

slf4j-4.2.1 log4j-2.5.1

commons-io-4.4.5

guava-17.1

commons-codec-2.1.3

Page 11: ICONUK 2015 - Gradle Up!

11

Example B – Automated code validation

§  Code validation can be done using 3rd party libraries/tools

§  FindBugs

§  CheckStyle

§  PMD

§  ...

Who executes them

– and when?

Where are they

executed? IDE?

Where are they

documented?

Page 12: ICONUK 2015 - Gradle Up!

12

Example C: Set version number

§  Define the version number so that it can be re-used

§  In the UI of your web application

§  In the file name of the compiled code

§  In the attached product documentation/help

§  In JavaDoc

§  ... Where do you set it

nowadays?

Page 13: ICONUK 2015 - Gradle Up!

13

Example D: Generate production package

§  Generate a production ready package with the same servlet based application (optionally including server configuration) for

§  Domino HTTP

§  WebSphere Liberty

§  TomEE

§  ...

Who maintains the

needed steps?

Page 14: ICONUK 2015 - Gradle Up!

14

Build automation – some PROs

§  Frees YOU from the hassle of repeating tasks

§  Makes SURE that you can concentrate on the essential part – coding

§  Helps to enforce STANDARDS across multiple developers and/or projects

§  Eases the MAINTAINABILITY of your applications

§  And much more!

Page 15: ICONUK 2015 - Gradle Up!

15

Automated Builds (here: Atlassian Bamboo)

Page 16: ICONUK 2015 - Gradle Up!

16

Build automation – It’s not for free!

§  Takes some time for adoption!

§  You‘ve to give up some (bad) habits.

§  Infrastructure needed (i. e. for automated testings).

Page 17: ICONUK 2015 - Gradle Up!

17

Agenda

§  Build Automation – Why?

§  Why Gradle - and not Maven or Ant?

§  Theory First - Gradle Basics

§  Your first Gradle project – start small

§  Building a web application

§  Building an OSGi plug-in project

Page 18: ICONUK 2015 - Gradle Up!

18

Why Gradle - and not Maven or Ant?

§  Please not the same discussion as with Windows vs. Linux

§  All widely accepted

§  Personal preferences may apply (like XML vs. custom DSL)

§  Make your choice!

Page 19: ICONUK 2015 - Gradle Up!

19

Why Gradle - and not Maven or Ant?

Source: http://zeroturnaround.com/rebellabs/java-build-tools-part-2-a-decision-makers-comparison-of-maven-gradle-and-ant-ivy/

Page 20: ICONUK 2015 - Gradle Up!

20

Why Gradle - and not Maven or Ant?

convention-over-configuration

vs.

configuration-over-convention

Page 21: ICONUK 2015 - Gradle Up!

21

Example configuration (Maven vs. Gradle)

§  Comparing a simple build configuration for code testing purposes

§  Use JUnit and Hamcrest

§  Apply CheckStyle, FindBugs and PMD

Source: http://technologyconversations.com/2014/06/18/build-tools/ Code: https://github.com/vfarcic/JavaBuildTools

Page 22: ICONUK 2015 - Gradle Up!

22

Maven configuration (pom.xml)

Page 23: ICONUK 2015 - Gradle Up!

23

Gradle configuration (build.gradle)

Page 24: ICONUK 2015 - Gradle Up!

24

Agenda

§  Build Automation – Why?

§  Why Gradle - and not Maven or Ant?

§  Theory First - Gradle Basics

§  Your first Gradle project – start small

§  Building a web application

§  Building an OSGi plug-in project

Page 25: ICONUK 2015 - Gradle Up!

25

Basics – General

§  Gradle is built on top of Groovy – you „configure“ in code!

§  You don‘t have to learn Groovy in the first place when using Gradle.

§  https://en.wikipedia.org/wiki/Groovy_(programming_language)

§  Build scripts are using a custom DSL (Domain Specific Language)

§  https://docs.gradle.org/current/dsl/index.html

Page 26: ICONUK 2015 - Gradle Up!

26

Basics – DSL

Page 27: ICONUK 2015 - Gradle Up!

27

Basics – Releases and availability

§  New versions are delivered every 4-6 weeks

§  Current version is 2.7 (14.09.2015)

§  Release Notes: https://docs.gradle.org/2.7/release-notes

§  Download: https://gradle.org/gradle-download/

Page 28: ICONUK 2015 - Gradle Up!

28

Components

gradle

gradlew

Gradle daemon

build.gradle

settings.gradle

gradle.properties

Binaries Configuration

Page 29: ICONUK 2015 - Gradle Up!

29

Binaries

§  You can use either way the gradle executable of the binary package or the gradle wrapper (gradlew).

§  The wrapper allows you to use Gradle on systems that don‘t have Gradle installed (yet)

§  Enhance your build times by using the daemon

§  https://docs.gradle.org/2.7/userguide/gradle_daemon.html

Page 30: ICONUK 2015 - Gradle Up!

30

Configuration – Global

§  Custom global properties can be set via a gradle.properties file

§  Project build dir

§  Users gradle home dir (i. e. ~/.gradle/ on *nix systems)

§  Via System properties

§  Define proxy settings, passwords for remote repository access and much more

§  https://docs.gradle.org/current/userguide/build_environment.html

Page 31: ICONUK 2015 - Gradle Up!

31

Configuration – Project specific

§  Gradle uses two build script files

§  build.gradle

§  settings.gradle

§  Minimum in the root of the project folder structure

§  Can have more of them in a multi-project structure

Page 32: ICONUK 2015 - Gradle Up!

32

Tasks

§  Gradle offers you a variety of built-in tasks.

§  Tasks can either run standalone – or they depend on the successful execution of other tasks and run in a chain.

§  Configuration provided in the build.gradle file or via (custom) plugins.

§  You can write custom tasks directly in the build.gradle file.

Page 33: ICONUK 2015 - Gradle Up!

33

Task example – gradle init

§  Creates a new Gradle project according to the specified type (or a basic Gradle project if no type is supplied)

Dedicated tasks

Different types for init are available

Page 34: ICONUK 2015 - Gradle Up!

34

Task example – gradle tasks

§  Lists all tasks that are available to the current project

Name and description

Page 35: ICONUK 2015 - Gradle Up!

35

Tasks – gradle test

§  Runs the test cases of the project (if you test your code ;-))

Gradle stores hash values of task in- and output and skips the processing if no changes haven been made.

Page 36: ICONUK 2015 - Gradle Up!

36

IDE integration (here: Eclipse)

I recommend this at the moment

Relatively new, needs improvement

Page 37: ICONUK 2015 - Gradle Up!

37

Agenda

§  Build Automation – Why?

§  Why Gradle - and not Maven or Ant?

§  Theory First - Gradle Basics

§  Your first Gradle project – start small

§  Building a web application

§  Building an OSGi plug-in project

Page 38: ICONUK 2015 - Gradle Up!

38

Your first Gradle project – start small

§  We‘ll create a simple Java project here

§  Init a standalone project (non-Eclipse specific)

§  Learn about the project structure §  Generate a runnable JAR file

§  Run the JAR file

Page 39: ICONUK 2015 - Gradle Up!

39

Init the project

Page 40: ICONUK 2015 - Gradle Up!

40

Learn about the project structure

build script

build script

Gradle wrapper files

Example source code

Page 41: ICONUK 2015 - Gradle Up!

41

Starting point for a build.gradle configuration

Standard and custom

plugins are available

jcenter references to Bintray, you can also use i. e. mavenCentral,

mavenLocal and local directoreis

Define the needed dependencies that are needed by this project.

Page 42: ICONUK 2015 - Gradle Up!

42

Assemble the JAR file

Gradle detects unavailable dependencies and downloads them

automatically.

Page 43: ICONUK 2015 - Gradle Up!

43

Standard project structure

The build directory contains the output of the Gradle build

Page 44: ICONUK 2015 - Gradle Up!

44

Run the assembled JAR file (or try to ;-))

The build/libs directory normally contains the generated packages.

Page 45: ICONUK 2015 - Gradle Up!

45

Add your first additional plugin

Adding the „application“ plugin for getting a runnable JAR file

New property that tells the „application“ plugin the name of the main class.

Page 46: ICONUK 2015 - Gradle Up!

46

Validate the new tasks (added by the plugin)

New tasks

Page 47: ICONUK 2015 - Gradle Up!

47

Run the JAR file

Output of the sample code

Page 48: ICONUK 2015 - Gradle Up!

48

Create a distribution package for the JAR file

Task of the „application“ plugin

Page 49: ICONUK 2015 - Gradle Up!

49

Validate the distribution package

installDist generates the fully runnable distribution package

Page 50: ICONUK 2015 - Gradle Up!

50

Agenda

§  Build Automation – Why?

§  Why Gradle - and not Maven or Ant?

§  Theory First - Gradle Basics

§  Your first Gradle project – start small

§  Building a web application

§  Building an OSGi plug-in project

Page 51: ICONUK 2015 - Gradle Up!

51

Building a web application

§  We‘ll create a simple web application (with a single servlet) which will be used from within the Eclipse IDE

§  Populate a blank build.gradle file with the needed settings

§  Use Maven Central as remote repository §  Generate a WAR file

§  Run the WAR in a Jetty container

Page 52: ICONUK 2015 - Gradle Up!

52

Populate new build.gradle file

Code generation should respect Java 8

Plugins that help with Eclipse integration

Adds tasks for WAR files

Page 53: ICONUK 2015 - Gradle Up!

53

Initialize Eclipse project

Preparing the Gradle project for Eclipse

Page 54: ICONUK 2015 - Gradle Up!

54

Validate Eclipse data creation

Page 55: ICONUK 2015 - Gradle Up!

55

Import as “Gradle Project” 1/2

Page 56: ICONUK 2015 - Gradle Up!

56

Import as “Gradle Project” 2/2

Page 57: ICONUK 2015 - Gradle Up!

57

Create new Source Folders 1/2

Page 58: ICONUK 2015 - Gradle Up!

58

Create new Source Folders 1/2

Page 59: ICONUK 2015 - Gradle Up!

59

Create a simple servlet (or try to ;-))

HttpServlet isn‘t available

Page 60: ICONUK 2015 - Gradle Up!

60

Repository and dependency configuration

Adding mavenCentral as new remote repo

Adding the servlet classes as new dep

Page 61: ICONUK 2015 - Gradle Up!

61

Refresh dependencies to download them (if needed)

Page 62: ICONUK 2015 - Gradle Up!

62

“Finish” the simple servlet

Page 63: ICONUK 2015 - Gradle Up!

63

Add web.xml configuration 1/3

Page 64: ICONUK 2015 - Gradle Up!

64

Add web.xml configuration 2/3

Page 65: ICONUK 2015 - Gradle Up!

65

Add web.xml configuration 3/3

Page 66: ICONUK 2015 - Gradle Up!

66

Need to run the WAR? Here we go!

Another plugin that allows quick tests within a Jetty container

Page 67: ICONUK 2015 - Gradle Up!

67

Quick start from within Eclipse

Use the Quick Launcher to run the WAR in the Jetty container

Page 68: ICONUK 2015 - Gradle Up!

68

Using WebSphere Liberty? Lets ci.gradle help you!

https://github.com/WASdev/ci.gradle

Page 69: ICONUK 2015 - Gradle Up!

69

Agenda

§  Build Automation – Why?

§  Why Gradle - and not Maven or Ant?

§  Theory First - Gradle Basics

§  Your first Gradle project – start small

§  Building a web application

§  Building an OSGi plug-in project

Page 70: ICONUK 2015 - Gradle Up!

70

Building an OSGi plugin

§  Standard OSGi support in Gradle isn‘t very good

§  A simple plugin to help with OSGi-Manifest generation

§  https://docs.gradle.org/current/userguide/osgi_plugin.html

And now what?

Page 71: ICONUK 2015 - Gradle Up!

71

Wuff – to the rescue?

https://github.com/akhikhl/wuff

Page 72: ICONUK 2015 - Gradle Up!

72

Wuff – the solution?

§  Great for all Eclipse related build tasks.

§  Missing features – DIY

§  Feature generation

§  Updatesite creation

Page 73: ICONUK 2015 - Gradle Up!

73

Wuff – additional considerations (for Domino)

Page 74: ICONUK 2015 - Gradle Up!

74

Wuff – find the custom build here

§  https://github.com/petarov/wuff

Thanks to Petar, he did some of the dirty work

Page 75: ICONUK 2015 - Gradle Up!

75

Thank you!