build systems • Some exposure to Gradle • Beginner level of exposure to Groovy/Java ! • Takeaways • Learn a trick or two to read and write DSLs • A simpler way to look at Gradle Scripts 2
Ruby, Scala and JavaScript (CoffeeScript). • Work at ThoughtWorks • Co-‐organizer of Pune Java User Group • Projects hosted on Github • (want to get better at) running, playing drums and writing book 3
a programming language designed specifically to express solutions to problems in a specific domain” ! ! ! ! ! • http://c2.com/cgi/wiki?DomainSpecificLanguage • http://en.wikipedia.org/wiki/Domain-‐specific_language 5
: ! apply plugin: 'java' ! • A valid minimal Java project build file • It can build a Jar of project • Uses conventional src/main/java for source files 12
repositories { mavenCentral() } ! • And use them in your project ! dependencies { compile 'org.slf4j:slf4j-‐api:1.7.9' testCompile 'junit:junit:4.10' } 14
Configures this project using plugins or scripts. The following options are available: !•! from: A script to apply to the project. Accepts any path supported by Project.uri(). !•! plugin: The id or implementation class of the plugin to apply to the project. !•! to: The target delegate object or objects. Use this to configure objects other than the project. For more detail, see ObjectConfigurationAction. ! • http://www.gradle.org/docs/current/dsl/ org.gradle.api.Project.html#org.gradle.api.Project:apply(java.util.Map) 27
• Available as ‘project’ reference in build script • Build Script is evaluated against the instance of this project object. • Available implicitly in the script top level* • methods/properties are looked up on this object by default. • One project per build.gradle 29
a description for this project. ! • Methods boolean delete(Object... paths) Deletes files and directories. ! • Script blocks void dependencies(Closure configureClosure) Configures the dependencies for this project. 30
group = 'org.example' version = '0.1' description = 'An awesome groovy library' ! • Find properties declared on Project API. • Make sure they are not read-‐only. 31
getBuildDir()/setBuildDir(Object path) • Can be set using groovy property notation buildDir = “…” ! • Extra properties • can be declared using ext namespace. ext.myProp = “abc” • it becomes property of project println project.myProp 32
plugins • Plugins can add more methods to project • like sourceSets is added by java plugin ! • Each task becomes a method on project • Each task name becomes a method ! ! taskA { description = “some task” }! • so that task can be easily referenced in a script ! • Methods of parent project 34
Closure as parameter • Delegates to a special Handler whose methods are available in the block’s context ! ! ! ! ! http://www.gradle.org/docs/current/dsl/org.gradle.api.Project.html#N1486F 37
! • RepositoryHandler Handles the methods calls made in context of repositories script block • Methods like mavenCentral() jcenter() ivy() ! http://www.gradle.org/docs/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html 38
! • Delegates to DependencyHandler • Executes the closure in against the DependenciesHandler • methods like compile, testCompile ! ! http://www.gradle.org/docs/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html 39
of its sub-projects. ! artifacts { } Configures the published artifacts for this project. ! buildscript { } Configures the build script classpath for this project. ! configurations { } Configures the dependency configurations for this project. ! dependencies { } Configures the dependencies for this project. ! repositories { } Configures the repositories for this project. ! sourceSets { } Configures the source sets of this project. ! subprojects { } Configures the sub-projects of this project. ! publishing { } Configures the PublishingExtension added by the publishing plugin. 40