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.

Faster java ee builds with gradle [con4921]


Published on

JavaOne 2016
It is time to move your Java EE builds over to Gradle! Gradle continues to gain momentum across the industry. In fact, Google is now pushing Gradle for Android development. Gradle draws on lessons learned from both Ant and Maven and is the next evolutionary step in Java build tools. This session covers the basics of switching existing Java EE projects (that use Maven) over to Gradle and the benefits you will reap, such as incremental compiling, custom distributions, and task parallelization. You’ll see demos of all the goodies you’ve come to expect, such as integration testing and leveraging of Docker. Switching is easier than you think, and no refactoring is required.

Published in: Software
  • Be the first to comment

Faster java ee builds with gradle [con4921]

  2. 2. About Ryan Cuprak • @ctjava • • •
  3. 3. Introducing Gradle • Open source build automation system • Apache 2.0 License • Builds upon Apache Ant and Maven • First released in 2007 • Uses a Groovy-based DSL (not XML) • Uses directed acyclic graph to determine build order • Supports multiple languages: Java, C++, C, etc. • Rich plug-in architecture • Convention over configuration but easily customized/adapted
  4. 4. Introducing Gradle • Build file can be versioned like dependencies. Ever run Ant 1.9 file with Ant 1.6? • Background daemon reduces build-time • Supports incremental builds • Built-in profiling support • Build projects in parallel and some tasks* • Built-in Ant/Maven integration • Supported central repositories: • Maven Central • Jcenter • Ivy
  5. 5. Build System Evolution Ant Maven Gradle2004 2009 2000
  6. 6. Gradle versus Maven Feature Gradle Maven Fully configurable DAG ✅ ❌ Task Exclusion ✅ ❌ Dry Run ✅ ❌ Advanced Task Ordering ✅ ❌ Custom Distributions ✅ ❌ Repository Aware Cache ✅ ❌ Version Conflict Resolution ✅ ❌ File Based Dependencies ✅ ❌ Build Comparison ✅ ❌ Custom Dependency Scopes ✅ ❌ ReplaceByRules ✅ ❌
  7. 7. Why Gradle?
  8. 8. Questions 1. Do you need to learn Groovy? No (Good idea) 2. Do you need to completely refactor your code base? No 3. Do you need additional IDE plugins? Maybe 4. Do you need to change your build process? Depends 5. Do you need to port your entire build system over? No – can port over individual modules 6. Can you embed custom Ant logic? Yes
  9. 9. Questions… 7. Must all dependencies originate from a repository? No 8. Can artifacts be pushed to a repository? Yes 9. Can Jenkins initiate Gradle builds? Yes
  10. 10. Why Gradle for Java EE? • Java EE projects are: • Large • Complex • Contain many dependencies • Ant lacks dependency management • Large Ant files are a nightmare to debug • Maven isn’t flexible • Custom plugins aren’t the solution • Evolving slowly
  11. 11. Installation • Installation similar to Ant/Maven • Download and install from • Set environment variables: • GRADLE_HOME • PATH (to GRADLE_HOME/bin) • gradle = ant = mvn
  12. 12. Key Gradle Files Build file build.gradle Configuration settings settings.gradle Local settings ~/.gradle/ Local repository (project)/.gradle build.gradle = pom.xml = build.xml
  13. 13. Gradle Daemon • Gradle daemon is enabled by default • Disable for continuous build environments! • Displaying status gradle –status • Stopping daemon: gradle –stop • Disabling daemon: • Add org.gradle.daemon=false to ~/.gradle
  14. 14. Project Creation To start a new project: • gradle init – creates a new project • Uses pom.xml if present. • Import multi-model projects • Optionally specify –type <type> • java-library • scala-library • groovy-library • basic (default) – no src directories created. • Central repository defaults to jcenter()
  15. 15. Project Creation… gradle init --type java-library Default Project Layout src main resources test java resources build.gradle settings.gradle java project
  16. 16. Initial Gradle File
  17. 17. Command line – listing tasks gradle –q tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Build tasks ----------- assemble - Assembles the outputs of this project. build - Assembles and tests this project. buildDependents - Assembles and tests this project and all projects that depend on it. buildNeeded - Assembles and tests this project and all projects it depends on. classes - Assembles classes 'main'. clean - Deletes the build directory. jar - Assembles a jar archive containing the main classes. testClasses - Assembles classes 'test'.
  18. 18. Tasks Continued… Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Documentation tasks ------------------- javadoc - Generates Javadoc API documentation for the main source code.
  19. 19. Tasks Continue… Help tasks ---------- components - Displays the components produced by root project 'scratch'. [incubating] dependencies - Displays all dependencies declared in root project 'scratch'. dependencyInsight - Displays the insight into a specific dependency in root project 'scratch'. help - Displays a help message. model - Displays the configuration model of root project 'scratch'. [incubating] projects - Displays the sub-projects of root project 'scratch'. properties - Displays the properties of root project 'scratch'. tasks - Displays the tasks runnable from root project 'scratch'. Verification tasks ------------------ check - Runs all checks. test - Runs the unit tests.
  20. 20. Sample Gradle gradle build src build scratch-1.0-SNAPSHOT.jar libs project Output
  21. 21. Projects versus Tasks Project 1 Project 2 Project 3 Task 1 Task 3 Task 2 Depends on Task 1 Task 2 Depends on Task 1 Task 3 Task 2 Depends on Task 3 Depends on
  22. 22. Understanding Gradle Build File org.gradle.api.Project apply(options: Map<String,?>) buildscript(config: Closure) dependencies(config: Closure) configurations(config: Closure) getDependencies() getConfigurations() getAnt() getName() getDescription() getGroup() getPath() getVersion() getLogger() setDescription(description: String) setVersion(version: Object) file(path: Object) task(args: Map<String,?>,name: String) Within a Gradle file, project implicit. Above could be written as: project.apply plugin: ‘java’
  23. 23. Understanding Gradle Tasks org.gradle.api.Task dependsOn(tasks: Object…) doFirst(action: Closure) doLast(action: Closure) getActions() getInputs() getOutputs() getAnt() getDescription() getEnabled() getGroup() setDescription(description: String) setEnabled(enabled: boolean) setGroup(group: String) Tasks are built on the Task object.
  24. 24. Defining Tasks
  25. 25. Task Dependencies
  26. 26. Grouping Tasks
  27. 27. Group Tasks Custom Group
  28. 28. Plugins Plugin ID Automatically Applies Works With Description java java-base Java compilation/testing application java,distribution ear java Java EE Support maven java,war Maven publishing war java Assembles WAR files java-library- distribution java, distribution Support for tar/zip distributions for Java library. idea java Generates IDEA files eclipse java,groovy, scala Generates Eclipse files Standard: Third party:
  29. 29. Multi-Module Projects admin-web model myApp web mobile-app build.gradle admin-web model myApp web mobile-app build.gradle master Hierarchical Layout Flat Layout
  30. 30. Hierarchical Layout: Example Project ctjava build.gradle settings.gradle ctcore build.gradle settings.gradle ctweb build.gradle settings.gradle migrate build.gradle settings.gradle
  31. 31. Hierarchical Layout: Top Level build.gradle gradle.settings ctjava
  32. 32. Hierarchical Layout: Second Level • gradle.settings for ctcore/migrate/ctweb: = 'ctjava’ • migrate/ctweb dependencies on ctcore: compile project (':ctcore’)
  33. 33. IDE Support IDE Separate Plugin Java EE Support IDEA (free) No Yes IDEA (paid) No Yes NetBeans Yes Depends Eclipse Yes Yes
  34. 34. Eclipse Gradle Support
  35. 35. NetBeans Gradle Support Multi-project Java EE projects not recognized.
  36. 36. [WAR] NetBean Web App – Ant Build File System File System
  37. 37. [WAR] NetBeans Web App - Gradle Java Source directories WAR Plugin Local JARs
  38. 38. [WAR] NetBeans Web App – Gradle… Web resource directory Java EE Dependencies Local JAR
  39. 39. WAR Plugin Configuration Configuration Description from Adds a file-set to the root of the archive webInf Adds a file-set to the WEB-INF dir. classpath Adds a file-set to the WEB-INF/lib dir webAppDirName The name of the web application source directory, relative to the project directory. webXml Copies a file to WEB-INF/web.xml
  40. 40. JavaScript Minification Minification Output Google Minifier
  41. 41. JavaScript Minification… Extend JavaExec Task to invoke Minifier
  42. 42. JavaScript Minification… gradle -PjsOptimize=true build
  43. 43. Generating JPA Meta-Model Create custom plugin to run Java Annotation Processor: implementation-class=JavaAptPlugin
  44. 44. Custom Annotation Processor
  45. 45. Custom Annotation Processor
  46. 46. Custom Annotation Processor: Build Plugin Exclude everything but JPA entities
  47. 47. EAR Projects Project Output
  48. 48. EAR Project
  49. 49. Provided Scope – Non-WAR Projects • providedCompile is a configuration on WAR plugin. • Non-WAR projects must add a custom scope.
  50. 50. jaxb Code Generation POJOxsd
  51. 51. Generating JAX-WS Client • Generate JAX-WS client for WSDL using wsimport • Plugin: • Generated source code: • Build/generated-src/wsimport
  52. 52. Generating JAX-WS Client
  53. 53. Generating JAX-WS Client Generated Source Code
  54. 54. Docker • Build Docker images from project output: • Transmode/gradle-docker - • Build/publish docker files from build script – not Dockerfile • bmuschko/gradle-docker-plugin - • docker-remote-api – interacts with Docker via remote API • docker-java-application – creates/pushes docker images for java applications • Run Docker containers during build • palantir/gradle-docker - • docker – building and pushing docker images • docker-compose - populating placeholders in a docker-compose template • docker-run – starting/stopping/status on named images
  55. 55. Building Docker Images
  56. 56. Simple Docker Example – Run Container Available Tasks: • dockerRun • dockerStop • dockerRunStatus • dockerRemoveContainer
  57. 57. Docker & Testing • Launch Postgresql Docker container before unit tests execute • Test cleanup: • Leave container running if any tests fail • Destroy container if tests succeed
  58. 58. Docker & Testing
  59. 59. Docker Database Testing
  60. 60. Parameter Substitution: persistence.xml
  61. 61. Parameter Substitution: build.gradle
  62. 62. Testing with Arquillian/Selenium
  63. 63. Misc • View Dependencies: gradle -q ctweb:dependencies • Build GUI: gradle –gui • Profiling: gradle –profile • Dry-run gradle –m build
  64. 64. Q&A