Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Faster Java EE Builds with Gradle

941 vues

Publié le

Presentation given at ConFoo 2017 covering Gradle and Java EE. Includes recipes for working with Docker, Arquillian, Node.js (and WebPack), etc.

Publié dans : Logiciels
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Faster Java EE Builds with Gradle

  1. 1. Faster Java EE Builds with Gradle RYAN CUPRAK
  2. 2. About  Ryan Cuprak  @ctjava  rcuprak@gmail.com  http://www.cuprak.info  https://www.linkedin.com/in/rcuprak
  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 ✅ ❌ Finalizers ✅ ❌ Custom Dependency Scopes ✅ ❌ ReplaceByRules ✅ ❌ https://gradle.org/maven-vs-gradle
  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 gradle.org  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/gradle.properties 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.  Imports multi-model projects  Optionally specify –type <type>  java-library  scala-library  groovy-library  basic (default) – no src directories created.  Central repository defaults to jcenter() https://bintray.com/bintray/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 Output 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 build.gradle 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) Project is an implicit object. 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. Grouping 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: https://docs.gradle.org/current/userguide/standard_plugins.html Third party: https://plugins.gradle.org
  29. 29. Multi-Module Projects
  30. 30. Multi-Module Projects admin-web model myApp web mobile-app build.gradle gradle.properties admin-web model myApp web mobile-app build.gradle gradle.properties master Hierarchical Layout Flat Layout
  31. 31. Hierarchical Layout: Example Project ctjava build.gradle settings.gradle ctcore build.gradle settings.gradle ctweb build.gradle settings.gradle migrate build.gradle settings.gradle
  32. 32. Hierarchical Layout: Top Level build.gradle gradle.settings ctjava
  33. 33. Hierarchical Layout: Second Level  gradle.settings for ctcore/migrate/ctweb: rootProject.name = 'ctjava’  migrate/ctweb dependencies on ctcore compile project (':ctcore’)
  34. 34. IDE Support
  35. 35. IDE Support IDE Separate Plugin Java EE Support IDEA (free) No Yes IDEA (paid) No Yes NetBeans Yes Depends Eclipse Yes Yes
  36. 36. Eclipse Gradle Support  http://download.eclipse.org/buildship/updates/e46/r eleases/1.0
  37. 37. NetBeans Gradle Support Multi-project Java EE projects not recognized.
  38. 38. IntelliJ Support
  39. 39. Legacy Project
  40. 40. NetBeans EE Project to Gradle File System Project Representation
  41. 41. NetBeans EE Project to Gradle Java Source directories WAR Plugin Local JARs
  42. 42. NetBeans EE Project to Gradle Web resource directory Java EE Dependencies Local JAR Continued…
  43. 43. 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
  44. 44. Recipes
  45. 45. JavaScript Minification Minification Output Google Minifier
  46. 46. JavaScript Minification…  Extend JavaExec Task to invoke Minifier
  47. 47. JavaScript Minification… gradle -PjsOptimize=true build
  48. 48. Node/Webpack Integration Node Gradle Plugin https://github.com/srs/gradle-node-plugin Supports: NodeJS, Yarn, Grunt, Gulp
  49. 49. Generating JPA Meta-Model Create custom plugin to run Java Annotation Processor: implementation-class=JavaAptPlugin
  50. 50. Custom Annotation Processor Custom annotation processor
  51. 51. Custom Annotation Processor Custom annotation processor
  52. 52. Custom Annotation Processor: Build Plugin Exclude everything but JPA entities Custom annotation processor
  53. 53. EAR Projects Project Output Contents of EAR
  54. 54. EAR Project EAR plugin
  55. 55. Provided Scope – Non-WAR Projects • providedCompile is a configuration on WAR plugin. • Non-WAR projects must add a custom scope.
  56. 56. jaxb Code Generation POJOxsd
  57. 57. Generating JAX-WS Client  Generate JAX-WS client for WSDL using wsimport  Plugin:  https://plugins.gradle.org/plugin/me.seeber.wsimport  Generated source code:  build/generated-src/wsimport
  58. 58. Generating JAX-WS Client https://plugins.gradle.org/plugin/me.seeber.wsimport
  59. 59. Generating JAX-WS Client Generated Source Code
  60. 60. Docker  Build Docker images from project output:  Transmode/gradle-docker - http://tinyurl.com/k7o7nab  Build/publish docker files from build script – not Dockerfile  bmuschko/gradle-docker-plugin - http://tinyurl.com/hg4q6jr  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 - http://tinyurl.com/hpw853h  docker – building and pushing docker images  docker-compose - populating placeholders in a docker-compose template  docker-run – starting/stopping/status on named images
  61. 61. Building Docker Images
  62. 62. Simple Docker Example – Run Container Available Tasks: • dockerRun • dockerStop • dockerRunStatus • dockerRemoveContainer
  63. 63. Docker & Testing  Launch PostgreSQL Docker container before unit tests execute  Test cleanup:  Leave container running if any tests fail  Destroy container if tests succeed
  64. 64. Docker & Testing
  65. 65. Docker Database Testing
  66. 66. Parameter Substitution: persistence.xml
  67. 67. Parameter Substitution: build.gradle
  68. 68. Testing with Arquillian/Selenium
  69. 69. Misc  View Dependencies: gradle -q ctweb:dependencies  Build GUI: gradle –gui  Profiling: gradle –profile  Dryrun gradle –m build
  70. 70. Reasons to Convert  Incremental compilation  Better dependency management/control  Customizable without needing plugins  Supports multiple languages/platforms  Build system can be versioned
  71. 71. Q&A

×