Intro to Gradle for FRC

Intro to Gradle for FRC

Delivered at StuySplash for FIRST Robotics Challenge Team 694.

3906b007e5c4150115e319a01d2f4ab8?s=128

Jeanne Boyarsky

December 12, 2019
Tweet

Transcript

  1. @jeanneboyarsky 1 Intro to Gradle For FRC Jeanne Boyarsky Saturday

    December 14, 2019 StuySplash speakerdeck.com/boyarsky
  2. @jeanneboyarsky About Me • 11 year FRC volunteer • Professional

    Programmer • Java Champion (about 300 in the world) • Build Tool Expert • And… 2
  3. @jeanneboyarsky Pause for a Commercial 3 Java 11 certs •1Z0-815

    - Out now •1Z0-816 - April ETA
  4. @jeanneboyarsky Volunteer? 4 NYC FRC Regional Co-Volunteer Coordinator firstinspires.org

  5. @jeanneboyarsky Agenda • Quick Java PSA • Intro to Build

    Tools • Intro to Gradle • Using Gradle for FRC • More on Groovy/Gradle 5
  6. @jeanneboyarsky Java Release Cycle 6

  7. @jeanneboyarsky “Java is Still Free” • Free for non-production use

    • For Prod use 20 minute version: https://medium.com/@javachampions/java- is-still-free-2-0-0-6b9aa8d6d244 7
  8. @jeanneboyarsky Agenda • Quick Java PSA • Intro to Build

    Tools • Intro to Gradle • Using Gradle for FRC • More on Groovy/Gradle 8
  9. @jeanneboyarsky What is a Build Tool? • Automated • Creates

    executable • Reproduceable • Codified 9
  10. @jeanneboyarsky Find the build tool? 10 Saw? Band Saw? CNC

    Machine? x Not automated x Automation != Electricity ✔Programming x But doesn’t create executable
  11. @jeanneboyarsky Happy Birthday Make • 1976 • C • Makefile

    • Compiles code • Adds libraries • Can call OS commands 11
  12. @jeanneboyarsky Big 3 Java Build Tools • Ant • Maven

    • Gradle 12
  13. @jeanneboyarsky Quick Comparison 13 Ant Maven Gradle Released 2000 2004

    2007 Build file build.xml pom.xml build.gradle Language XML XML Groovy Dependency mgmt? Not native Yes Yes
  14. @jeanneboyarsky FRC Timeline • 2004-2008 - IFI • 2009-2014 -

    cRIO with Java • Java 1.4 Mobile • 2015-? - RoboRIO with Java • Multi core • Current versions of Java 14
  15. @jeanneboyarsky FRC Timeline • 2010-2018 • Ant • 2019-? •

    Gradle 15
  16. @jeanneboyarsky Agenda • Quick Java PSA • Intro to Build

    Tools • Intro to Gradle • Using Gradle for FRC • More on Groovy/Gradle 16
  17. @jeanneboyarsky Convention over Configuration • Many defaults • Directory structure:

    • src • main • java 17
  18. @jeanneboyarsky Benefits • Less code to build • Fewer decisions

    to make • Faster to switch between teams • Easier to work on open source 18
  19. @jeanneboyarsky Phases 19 Initialization Counts gradle projects (1 Robot) Configuration

    Parses the build script Execution Determines which tasks to execute and does so
  20. @jeanneboyarsky First Install 20

  21. @jeanneboyarsky First Build • Download dependencies from internet • Cached

    21
  22. @jeanneboyarsky Simplest build.gradle apply plugin: ‘java' • compiles into build/classes

    • creates JAR into build/libs All that from one line! 22
  23. @jeanneboyarsky Key Gradle Files 23 File Description build.gradle The build

    script gradlew Gradle wrapper so can run without installing Gradle on Mac/Linux gradlew Same for Windows settings.gradle Sets projects specific config before build runs
  24. @jeanneboyarsky Gradle Directories 24 Directory Description .gradle Cache .wpilib FRC

    specific. Contains metadata. Ex: build year, team number vendordeps JSON files with locations for some dependencies
  25. @jeanneboyarsky JAR • Java Archive • Like zip file •

    Contains compiled code • You depend on these libraries 25
  26. @jeanneboyarsky Dependency • Often hosted on binary repository • ex:

    Maven Central • Some FRC plugins not in Central • vendordeps! 26
  27. @jeanneboyarsky Agenda • Quick Java PSA • Intro to Build

    Tools • Intro to Gradle • Using Gradle for FRC • More on Groovy/Gradle 27
  28. @jeanneboyarsky If not deploying • ./gradlew clean • ./gradlew build

    28
  29. @jeanneboyarsky From command palette > Executing task: ./gradlew deploy -

    PteamNumber=694 --offline - Dorg.gradle.java.home="/Library/Java/ JavaVirtualMachines/jdk-11.0.2.jdk/ Contents/Home" < 29 -P = project property -D = system property
  30. @jeanneboyarsky Build Gradle Walkthru 30

  31. @jeanneboyarsky Build Gradle Walkthru 31

  32. @jeanneboyarsky Build Gradle Walkthru 32

  33. @jeanneboyarsky Build Gradle Walkthru 33

  34. @jeanneboyarsky Build Gradle Walkthru 34

  35. @jeanneboyarsky Build Gradle Walkthru 35

  36. @jeanneboyarsky Build Gradle Walkthru 36

  37. @jeanneboyarsky Build Gradle Walkthru 37

  38. @jeanneboyarsky Settings Gradle Walkthru 38

  39. @jeanneboyarsky Settings Gradle Walkthru 39

  40. @jeanneboyarsky Agenda • Quick Java PSA • Intro to Build

    Tools • Intro to Gradle • Using Gradle for FRC • More on Groovy/Gradle 40
  41. @jeanneboyarsky Different Java for Class? 1) VS Code Settings 41

  42. @jeanneboyarsky Different Java for Class? 2) <HOME>/.gradle/ gradle.properties 42 org.gradle.java.home=/Library/Java/

    JavaVirtualMachines/jdk-11.0.2.jdk/ Contents/Home
  43. @jeanneboyarsky Groovy 43 8 Groovy Java

  44. @jeanneboyarsky Java -> Groovy int lastYear = 2017; int year

    = 2018 def nextYear = 2019 44 Can use Java syntax Without semicolon or type
  45. @jeanneboyarsky Strings def city = 'SF' println 'Here: $city' println

    "Here: $city" println "In ${city.class}" 45 Java String GString Here: $city Here: SF In class java.lang.String
  46. @jeanneboyarsky Multi Line String def name = 'Jeanne' def text

    = """ Name ______ $name """ println text 46 Coming soon to Java with new syntax Name ______ Jeanne
  47. @jeanneboyarsky What does this print? def name = 'Jeanne' def

    text = ’’’ Name ______ $name ’’’ println text 47 Name ______ $name
  48. @jeanneboyarsky Getters vs Properties def animal = 'dog' println animal.getBytes()

    println animal.bytes 48
  49. @jeanneboyarsky == vs equals() def monday ='monday' def nextMonday =

    new String('monday') def tuesday ='tuesday' println monday == null println null == monday println monday == tuesday println monday == nextMonday 49 Java Syntax True!
  50. @jeanneboyarsky What is the Truth? 50 Value Result Null FALSE

    Empty String FALSE Empty List FALSE 1 Character String TRUE
  51. @jeanneboyarsky Elvis has entered the building println Jenkins.instance .getItemByFullName(jobName) ?.lastBuild

    ?.timestamp ?.time 51
  52. @jeanneboyarsky Optional Parens def ch = 'abc'.charAt 1 println ch

    52 Optional when no ambiguity Ambiguity: * Zero params * Within println
  53. @jeanneboyarsky ArrayList++ 53 def list = ['cookie', 'chocolate’] list <<

    'candy' println list.getClass() class java.util.ArrayList println list[1] chocolate ArrayList does what now?
  54. @jeanneboyarsky ArrayList++ 54 println list [cookie, chocolate, candy] println list[-1]

    candy println list.min() candy list.sort() println list [candy, chocolate, cookie]
  55. @jeanneboyarsky Functions incrementBy = 4; def add(num) { num +

    incrementBy } println add(2) 55 no def types optional return optional
  56. @jeanneboyarsky IT (10..1).each{ println "--> $it" } println 'Blast off!'

    56
  57. @jeanneboyarsky Named Params def config = new SelectorConfiguration( name: ‘selector',

    description: ‘packages', ) 57 Only the default constructor exists!
  58. @jeanneboyarsky Alfred vs Edwin File file = new File("src/main/java/frc/robot/RobotMap.java") System.out.println("=========================")

    if(file.text.contains('ROBOT_NAME = "Alfred"')) { System.out.println("Built to ALFRED!!!") } else if(file.text.contains('ROBOT_NAME = "Edwin"')) { System.out.println("Built to EDWIN!!!") } else { System.out.println("[ATTENTION] Look at RobotMap and make sure you have robot name set!") throw new IllegalStateException("Robot Name Not Found"); } System.out.println("=========================") 58
  59. @jeanneboyarsky Book Giveaway 59

  60. @jeanneboyarsky Questions ? 60