Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

by Maxim Stepanenko


  1. 1. Gradle ( Stepanenko, Architect
  2. 2. Traditional build systems• Make, autotools• Ant (build, deploy), ant + ivy (dependency mgmt.)• Maven (dependency and lifecycle mgmt.)• Can be extended using pluginsPlug-in architectures severely limit the ability for build tools to grow gracefully as projectsbecome more complex. We have come to feel that plug-ins arethe wrong level of abstraction, and prefer language-based toolslike Gradle and Rake instead, because they offer finer-grainedabstractions and more flexibility long term.
  3. 3. Alternative build systems• Gradle (DSL + groovy + Ivy)• Rake (DSL + ruby)• Apache Buildr (DSL + ruby)• Scons (DSL + python)• Simple Build Tool (Scala + DSL)• All this systems based on a true languages: ruby, python etc.3
  4. 4. Brief Gradle benefits• You could script parts of the build that were too difficult to describe through "build by convention".• The general flexibility to define the build and directories the way that seemed to make sense.• The entire conceptualization of the build process is much cleaner. You not only have dependencies between modules but you can also define dependencies on tasks, modules, directories. It is already possible to say that one module depends on the compiled classes of another module as opposed to the output (jar) of the module.• Each project/module can have multiple "source sets". A "source set" defines a named group of source directories, output directories, resource directories.• "how am I going to solve this?" as opposed to "what are the viable options my build tool is going to leave me to achieve this?"• "incremental build“. It understands when things have changed and when they have not and when certain portions of the build are really needed.• Can publish artifacts to Maven repositories and generate accurate POM.• IDE project generation (Eclipse – spring tool suite + greclipse / IntelliJ).
  5. 5. Gradle usage in real world• Hibernate• Grails• Groovy• SpringIntegration• SpringSecurity5
  6. 6. Gradle build file 1apply plugin: javaapply plugin: eclipse‘sourceCompatibility = 1.7targetCompatibility = 1.7version = 1.0jar { manifest { attributes Implementation-Title: ‘Title’, Implementation-Version: version }}repositories { mavenCentral()}dependencies { compile group: org.slf4j, name: slf4j-log4j12, version: 1.7.2 testCompile group: junit, name: junit, version: 4.10}…} 6
  7. 7. Gradle build file 2task simpleCalculation << { def x = 30 def y = 20 if( x > y) { print x > y: + x + " - " + y + " = " + (x - y) } else { print x < y: + y + " - " + x + " = " + (y - x) }}task simple (dependsOn: ‘simpleCalculation’, description: ‘Simple task’){ doFirst { print "doFirst: ${}" + System.getProperty("line.separator") }} 7
  8. 8. Build options• gradle jar• gradle --daemon jar• GRADLE_OPTS="-Dorg.gradle.daemon=true"• gradle --gui• gradle --profile• gradle --dry-run  run tasks without execution8
  9. 9. Plugins• apply plugin: java• apply plugin: eclipse‘• apply plugin: scala‘• apply plugin: application• apply plugin: maven• apply plugin: signing9
  10. 10. Source sets (SS)• Java plugin source sets (main, test)• Provided tasks: compile<SS>Java, process<SS>Resources, <SS>Classes• Add new source set10
  11. 11. 1.api/javaapi/resources2.sourceSets { api main { compileClasspath = compileClasspath + files(api.output.classesDir) }}classes.dependsOn apiClasses3.gradle build 11
  12. 12. Dependency management• Groovy use Ivy under the hood + some syntactic sugar• Support maven and Ivy repositories + custom repositories• Logical groups of dependencies (compile group, testCompile group)• Transitive version conflicts (different sl4j-api versions)configurations.all { resolutionStrategy { failOnVersionConflict() }}gradle dependencies12
  13. 13. Testing• JUnit tests by defaul• TestNG as well, test.useTestNG()• Configurabletest { systemProperty sysProp, value jvmArgs -Xms256m, -Xmx512m debug = true ignoreFailures = true enableAssertions = true maxParallelForks = 4 forkEvery = 10 // create new thread after 10 tests}13
  14. 14. Running, publishing• Run application (apply plugin: ‘application’ )• Startup scripts (for win and linux)• Zip archive (with lib folder)• Publish jar and zip files to repositories (gradle uploadArchives)• Publish to maven repository• Publish signed artifacts (PGP only)14
  15. 15. Publish to local maven repositoryapply plugin: mavenarchivesBaseName = "algorithms"group = com.max.algorithmsversion = 1.0uploadArchives { repositories { mavenDeployer { repository(url: file:./maven) } }} 15
  16. 16. Multi-project builds• DAG full dependency traversation• Hierarchical structure• Flat structure• Possible to have one build file per multi module project• Define dependencies between tasks in different projects• Compile/runtime dependencies between projects16
  17. 17. Mixed languages projects• C++, scala, groovy• Cross compiled source bases• Fast scala compilation (similar to SBT) apply plugin: scala‘ ext.scala_version = 2.9.2 sourceSets.main.scala.srcDir "src/main/java" = [] dependencies { scalaTools group: org.scala-lang, name: scala-compiler, version: scala_version scalaTools group: org.scala-lang, name: scala-library, version: scala_version compile group: org.scalatra, name: scalatra_2.9.1, version: 2.1.0.M1 runtime group: org.scalatra, name: scalatra_2.9.1, version: 2.1.0.M1 }17
  18. 18. 18
  19. 19. Code quality• gradle check• Checkstyle• PMD• FindBugs• JDepend• Sonar• CodeNarc (static analyzer for groovy code)19
  20. 20. Checkstyle results20
  21. 21. Findbugs21
  22. 22. JDepend22
  23. 23. PMD23
  24. 24. Custom taskstask info(type: InfoTask)class InfoTask extends DefaultTask { @TaskAction def info(){ print “Current Gradle version: $project.gradle.gradleVersion” }}24
  25. 25. Custom pluginapply plugin: InfoPluginclass InfoPlugin implements Plugin<Project> { void apply(Project project) { project.task(info) << { println "Running Gradle: $project.gradle.gradleVersion" } }} 25
  26. 26. Continuous integration• Jenkins (gradle plugin)• JetBrains TeamCity• Atlassian Bamboo26