ebook img

Gradle Build Automation Handbook PDF

78 Pages·2016·4.474 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Gradle Build Automation Handbook

GradleBuildAutomationHandbook i Gradle Build Automation Handbook GradleBuildAutomationHandbook ii Contents 1 Gradle“HelloWorld”Tutorial 1 1.1 WhatisGradle? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 WhyGradle? Ireallyneedabuildtool? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 DownloadingGradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.4 Settingenvironmentvariables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.5 HelloWorld! Gradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.6 GradleJVMOptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.7 GradleBasicConcepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.8 WorkingwithGradleTasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.8.1 DefaultTasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.8.2 TaskDependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.8.3 AbbreviatedTaskExecution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.9 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.10 DownloadtheGradleScripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2 GradleProperties: BuildandConfigurationExample 9 2.1 WhyshouldweuseGradleProperties? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2 Whatdoweneed?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.3 TypesofGradle’sProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.3.1 SystemProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.4 ProjectProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.4.1 Gradle’sProperties,hello! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.4.2 CustomProjectPropertiesinScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.4.3 SettingPropertiesinCommandLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.4.4 SettingPropertiesusinganexternalfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.5 KeyPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.6 DownloadtheGradleScripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 GradleBuildAutomationHandbook iii 3 GradleGWTIntegrationExample 17 3.1 IntroductiontoGradleGWTIntegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1.1 GradlePlugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1.2 DependencyManagement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1.3 SourceSets(JavaPlugin). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.2 Whatdoweneed?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.3 EnvironmentConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.3.1 Gradle’sInstallation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.3.2 EclipseGradle&GWTPlugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.3.3 InstallandConfigureGWTSDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.4 CreatingGWTProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.5 SettingGradleinGWTProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.6 GWTGradleBuildScript(Stepbystep) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.6.1 PluginsandDependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.6.2 GradleGWTIntegration(Buildingandassembling). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.7 RunningtheExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.8 KeyPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.9 DownloadtheEclipseProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4 GradleSourceSetsExample 31 4.1 IntroductiontoGradleSourceSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.1.1 WhatisaGradleSourceSet?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.2 WhatdoWeNeed? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.3 EnvironmentConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.4 CreatingaGradleProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 4.5 GeneratedBuild.Gradle(NewSourceSetDefinition). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 4.6 SourceSet’sProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.7 AssemblingSourceSetsinJARFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4.8 CreatingSourceSetDocumentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.9 Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.10 FinalBuild.GradleSourceSetScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.11 KeyPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.12 DownloadtheEclipseProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5 GradleOSGiPluginExample: BNDToolsBundleIntegration 42 5.1 IntroductiontoGradleandOSGiIntegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5.1.1 BasicConcepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5.2 WhatdoWeNeed? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5.3 EnvironmentConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 GradleBuildAutomationHandbook iv 5.4 CreatingOSGiProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.5 DeployingOSGiProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 5.6 GradleIntegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.7 RunningGradleOSGiIntegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.8 TestingGradleOSGi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.9 KeyPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 5.10 DownloadtheEclipseProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6 GradleWrapperExample 54 6.1 What’sGradleWrapper? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 6.2 Whatweneedtostart? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 6.3 EnvironmentConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 6.4 CreatingWrapperScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 6.5 UsingGradleWrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6.6 KeyPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6.7 DownloadtheEclipseProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 7 GradleNetBeansExample 57 7.1 WhyuseGradleinNetBeansIDE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.2 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.3 InstallingtheGradlePlugininNetBeansIDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.4 HowtoStartusingGradleinNetBeans? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 7.5 CreateyourfirstGradleProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 7.6 TestingGradleProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 7.7 KeyPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 7.8 DownloadtheNetBeansProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 8 GradleWarPlugin(&Tomcat)Example 64 8.1 IntroductiontoGradleWarPlugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.2 Whatweneedtostart? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.3 EnvironmentConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.4 CreateJavaWebApplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 8.5 UsingGradleWARPlugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 8.6 RunningExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 8.7 DeployingWARFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 8.8 KeyPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 8.9 DownloadtheEclipseProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 GradleBuildAutomationHandbook v Copyright (c) Exelixis Media P.C., 2016 All rights reserved. Without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored or introduced into a retrieval system, or transmitted, in any form or by any means (electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of the copyright owner. GradleBuildAutomationHandbook vi Preface GradleisanopensourcebuildautomationsystemthatbuildsupontheconceptsofApacheAntandApacheMavenandintroduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven of declaring the project configuration. Gradleusesadirectedacyclicgraph("DAG")todeterminetheorderinwhichtaskscanberun. Gradlewasdesignedformulti-projectbuildswhichcangrowtobequitelarge,andsupportsincrementalbuildsbyintelligently determining which parts of the build tree are up-to-date, so that any task dependent upon those parts will not need to be re- executed. The initial plugins are primarily focused around Java, Groovy and Scala development and deployment, but more languagesandprojectworkflowsareontheroadmap. (Source: https://en.wikipedia.org/wiki/Gradle) In this ebook, we provide a compilation of Gradle examples that will help you kick-start your own projects. We cover a wide range of topics, from installation and configuration, to how to use various plugins and how to integrate Gradle with 3rd party tools. Withourstraightforwardtutorials,youwillbeabletogetyourownprojectsupandrunninginminimumtime. GradleBuildAutomationHandbook vii About the Author Andres is a Java Software Craftsman from Medellin Colombia, who strongly develops on DevOps practices, RESTful Web Services,Continuousintegrationanddelivery. Andresisworkingtoimprovesoftwareprocessandmodernizingsoftwareculture onColombia. GradleBuildAutomationHandbook 1/70 Chapter 1 Gradle “Hello World” Tutorial In this post we’ll look at Gradle, its installation and configuration, and how to automate stages of development and release of softwarethroughitsbaseconcept,theGradletasks. 1.1 What is Gradle ? Gradleisabuildandautomationtool,thatcanautomateourbuilding,testing,deployingtasksandmanymore. Gradleisthenext generation build system for Java technologies that includes some advantages from older tools like ant or maven. Let’s have a look: • Allowsdeclarativeandexpressivedomain-specific-language(DSL).Thisisapowerfulconceptbecauseitallowsustowritea customlanguagethatismorefriendlythanJava. • IsGroovy-based. ThismeansthatyourconfigurationismadeinGroovystatementsinsteadofxmlblocks,makingitveryeasy todefinethetaskstobeperformed. • SupportslegacyscriptsinAntorMaven,andhasfullsupporttoIvyrepositoryinfrastructure. • It’sdesignedtotakeadvantageofconventionoverconfiguration. • Worksonnon-javaprojectstoo. • Easilycustomizableandscalable. 1.2 Why Gradle ? I really need a build tool? Today,weworkonlargeprojectsthatneedautomatedreleaseprocesstomitigaterisksandfailures;abuildingsystemasGradle, permitsyoutostructureaprocessfromthecompilationtothedeploymentinyourapplicationserver. Thisapproachhasseveral advantages, like spending time on more important tasks for our project (like modeling and coding features) and delegating repetitivetaskstoGradle,oralsotheuseofGroovydynamiclanguagescripts(DSL)insteadoftoolongxmlfiles. Ifyouwanttodocontinuousdeliveryandmakeyourreleaseprocessautomatic,Gradleisanicetooltocarryoutthesegoals. 1.3 Downloading Gradle • Attimeofthistutorial,Gradleisinyour2.3version. Youcandownloadfromhere. • WeneedaJDK1.6beforeinstallingGradle,ifyoudon’thave,youcandownloadfromhere. GradleBuildAutomationHandbook 2/70 • So,unzipthefileinanydirectorythatyouchoose,inthisexamplewesetGradleinC:/Desarrollo/Lib/gradle-2.3. Figure1.1: GradleDirectoryInstallation 1.4 Setting environment variables Then, wehavetosetenviromentvariablestogetafullaccesstoGradle, socreatetheGRADLE_HOMEvariablethatpointto theearlierdirectorythatyouset. Note: ThescreenshotsshownherearetakenfromWindows8. YourversionofWindowsmayvary.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.