Gradle - the Enterprise Automation Tool

Download Gradle  - the Enterprise Automation Tool

Post on 30-Jun-2015

708 views

Category:

Software

4 download

Embed Size (px)

DESCRIPTION

Here are slides from basic training for Gradle. This training is aimed to help Java Developers to get hands-on experience to use Gradle as a primary build tool for Java source code starting from simple compilation continuing with different kinds of tests and finishing with code quality analysis and artefacts publishing.

TRANSCRIPT

<ul><li> 1. GradleTheEnterprise AutomationTool</li></ul> <p> 2. SA at EPAM Systems primary skill is Java hands-on-coding with Groovy, Ruby trying to learn some Scala and Erlang/Elixir passionate about agile, clean code and devopsIzzet Mustafayev@EPAM Systems@webdizz webdizz izzetmustafaievhttp://webdizz.name 3. Agenda Introduction Gradle Step by step by features Alternatives References Q&amp;A 4. Introduction 5. Continuous Integration 6. Principles#1 Every change automatically pulled 7. Principles#1 Every change automatically pulled#2 Every change automatically built 8. Principles#1 Every change automatically pulled#2 Every change automatically built#3 Every change in the code base line* 9. Benefits Each change guarantees working code Each update should guarantee workingcode ;) Do not delay epic merge Less bugs - depends on your testsefficiency Allows to have code ready to go live 10. Drawbacks Need to build infrastructure Need to build team culture Need to support/enhance infrastructure Overhead with writing a lot of differentkind of tests 11. Continuous Delivery 12. Principles#1 Every commit can result in a release 13. Principles#1 Every commit can result in a release#2 Automate everything! 14. Principles#1 Every commit can result in a release#2 Automate everything!#3 Automate a pain! 15. Principles#1 Every commit can result in a release#2 Automate everything!#3 Automate a pain!#4 Done means released 16. Benefits Automatic delivery of business idea tocustomer Easy going live deployment Less time spent on delivery - more profit More motivation to do more as you cansee what you can change/improve 17. Drawbacks Big effort to implement changes for: infrastructure rollout/rollback database increment/rollback decrease down time towards 0 Need to get customers to buy in Security policies 18. Gradle 19. 2.2 20. Gradle- General purpose build system 21. Gradle- General purpose build system- Comes with a rich DSL based on Groovy 22. Gradle- General purpose build system- Comes with a rich DSL based on Groovy- Follows build-by-convention principles 23. Gradle- General purpose build system- Comes with a rich DSL based on Groovy- Follows build-by-convention principles- Built-in plug-ins for JVM languages, etc 24. Gradle- General purpose build system- Comes with a rich DSL based on Groovy- Follows build-by-convention principles- Built-in plug-ins for JVM languages, etc- Derives all the best from Ivy, Ant &amp; Maven 25. Features 26. #0 Installation1. Download Gradle http://goo.gl/ktu7H62. Download zipped JDK from https://db.tt/HKW6GbI93. Unzip all above into D:ImagesGradle folder4. Open Command Window in dir D:ImagesGradle5. Executeset JAVA_HOME=D:ImagesGradlejdk1.8.0_256. Executegradle-2.1bingradle -v7. Download and install Git from from http://goo.gl/rv2G1X into D:ImagesGradleGit 27. #0 Installation8. Clone Git repositorygitbingit.exe clone https://github.com/webdizz/sbs-gradle.git 28. Groovy is under the hood 29. #1 Hello World!1. Reset code base to initial stategit reset --hard bed2227791git clean -df2. Create file build.gradle3. Typeprintln 'Hello, World! '4. Rungradle 30. Declarative 31. #2 Create simple build1. Run to see default tasks listgradle tasks2. Replace build.gradle file content withapply plugin: 'java'3. Run to see new available tasksgradle tasks4. Checkout step s2_prepare5. Run to build Java source codegradle build6. Explore directory build 32. Flexible Execution 33. #3 Execute tasks1. Run task with part of namegradle ta2. Run task with part of name to clean and compilegradle cle tC3. Run task with part of name to clean and compile andexclude processTestResourcesgradle cle tC -x pTR4. Get details for taskgradle -q help --task clean 34. #4 Use wrapper1. Run taskgradle tasks2. Run task to generate wrappergradle wrapper3. Run tasks using wrapper./gradlew tasks4. Customize task wrapper to use another Gradle versiontask wrapper(type: Wrapper) {gradleVersion = '2.2.1-rc-1'}5. Check Gradle version./gradlew -v 35. Multi-module Structure 36. #5 Create multi-module build1. Checkout step s5_prepare2. Add directory common3. Move src to common4. Create common/build.gradle for Java5. Add new module to settings.gradleinclude ':common'6. Run build./gradlew clean build7. Run task for module./gradlew :com:compJ 37. Dependency Management 38. Gradle- compile - to compile source 39. Gradle- compile - to compile source- runtime - required by classes at runtime 40. Gradle- compile - to compile source- runtime - required by classes at runtime- testCompile - to compile test sources 41. Gradle- compile - to compile source- runtime - required by classes at runtime- testCompile - to compile test sources- testRuntime - required to run the tests 42. #6 Dependencies1. Add repositories to download dependencies from tobuild.gradleallprojects { currProject -&gt;repositories {mavenLocal()mavenCentral()jcenter()maven {url 'http://repo.mycompany.com/}}} 43. #6.1 Dependencies1. Add common dependencies for all subprojects in build.gradlesubprojects {apply plugin: 'java'dependencies {compile 'org.slf4j:slf4j-api:1.7.7'testCompile'org.mockito:mockito-core:1.9.5','junit:junit:4.12-beta-1'}} 44. #6.2 Dependencies1. Add dependencies for concrete module incommon/build.gradledependencies {compile'org.projectlombok:lombok:1.14.4'} 45. #6.3 Dependencies1. List project dependencies./gradlew :common:dependencies 46. Configuration 47. #7 Configuration1. Extract common configuration parameters to gradle .properties filelombokVersion = 1.14.4build.gradle filedependencies {compileorg.projectlombok:lombok:$lombokVersion} 48. #7.1 Configuration1. Parameterise execution for custom task :printParameterin build.gradletask printParameter {println givenParameter}2. Add parameter default value to gradle.properties3. Execute task./gradlew -q :printParameter -PgivenParameter=hello 49. Rich API 50. #8 Rich API 51. Lifecycle Create a Settings instance for the build. Evaluate the settings.gradle script, if present, against the Settingsobject to configure it. Use the configured Settings object to create the hierarchy of Projectinstances. Finally, evaluate each Project by executing its build.gradle file, ifpresent, against the project. The project are evaluated in such orderthat a project is evaluated before its child projects. 52. Tasks A project is essentially a collection of Task objects. Each task performs some basic piece of work. Dependencies A project generally has a number of dependencies. Project generally produces a number of artifacts, which other projectscan use. 53. Plugins Plugins can be used to modularise and reuse project configuration. Properties Any property or method which your script uses is delegated through tothe associated Project object. A project has 5 property 'scopes'. Dynamic Methods A project has 5 method 'scopes'. 54. More Tests 55. #8.1 Rich API1. Add integration test source sets in file gradle/integTest.gradlesourceSets {integTest {compileClasspath += main.output + test.outputruntimeClasspath += main.output + test.output}} 56. #8.1 Rich API2. Add integration test configurations in filegradle/integTest.gradleconfigurations {integTestCompile.extendsFrom testCompileintegTestRuntime.extendsFrom testRuntime}3. Include extension for subprojects in file build.gradleapply from: file("${rootProject.projectDir}/gradle/integTest.gradle") 57. #8.1 Rich API3. Add integration test task in file gradle/integTest.gradletask integTest(type: Test){testClassesDir = sourceSets.integTest.output.classesDirclasspath = sourceSets.integTest.runtimeClasspathshouldRunAfter 'test'}check.dependsOn(integTest)4. Execute integration tests./gradlew integTest 58. #8.2 Tasks Dependencies1. Open build.gradle to add dependency for one taskfrom anotherprintParameter.dependsOn 'help'2. Run printParameter task./gradlew printParameter 59. #8.3 Tasks Ordering*1. Open build.gradle to add ordering for one task fromanothertask areTestsExist {if ([ file("${projectDir}/src/test/java").listFiles() ].isEmpty()) {println 'Test directory is empty'} else {println 'Test directory is not empty, willexecute tests'}}test.mustRunAfter areTestsExist 60. #8.3 Tasks Ordering*2. Run test task./gradlew test 61. #8.4 Rules1. Add rule to validate running of integration tests tasktasks.addRule('Check correctness of running tests'){ StringtaskName -&gt;gradle.taskGraph.whenReady{Map args = gradle.startParameter.systemPropertiesArgsgradle.taskGraph.allTasks.each { Task task -&gt;if (task.name.contains('integTest') &amp;&amp; !args.containsKey('profile')) {throw new org.gradle.api.tasks.StopExecutionException("Profile was not specified to runtests (-Dprofile=ci).")}}}} 62. #8.4 Rules2. Run check task to have failure./gradlew check3. Run check task with expected parameter./gradlew check -Dprofile=ci 63. Parallel Execution 64. #9 Parallel builds1. Switch to 9th step and execute next command./gradlew test --parallel2. Try to modify amount of executable threads./gradlew test --parallel --parallel-threads=3 65. Incremental Builds 66. #10 Custom Inputs/Outputs1. Create file gradle/releaseNotes.gradle to add task forrelease notesext.destDir = new File(buildDir, 'releaseNotes')ext.releaseNotesTemplate = file('releaseNotes.tmpl.txt')tasks.create(name: 'copyTask', type: org.gradle.api.tasks.Copy) {from releaseNotesTemplateinto destDirdoFirst {if (!destDir.exists()) {destDir.mkdir()}}rename { String fileName -&gt;fileName.replace('.tmpl', '')}} 67. #10 Custom Inputs/Outputstasks.create('releaseNotes') {inputs.file copyTaskoutputs.dir destDir}2. Add releaseNotes.tmpl.txt file as a template for releasenotes3. Apply configuration from gradle/releaseNotes.gradle inbuild.gradle4. Lets run releaseNotes task./gradlew releaseNotes 68. #10.1 Files filtering1. Enhance release notes task to prepare nice release notes fileext.changesFile = file('changes.txt')ext.bugs = []ext.features = []changesFile.eachLine { String line -&gt;String bugSymbol = '#bug:'String featureSymbol = '#feature:'if (line.contains(bugSymbol)) {bugs</p>