Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Accelerating Maven Builds From Snail's Pace ๐ŸŒ t...

Accelerating Maven Builds From Snail's Pace ๐ŸŒ to Rocket Speed ๐Ÿš€ (JSpringย 2024)

Are you tired of watching Maven builds crawl at a snailโ€™s pace, wasting precious development time? Spending too much time at the coffee machine, or fighting battles with wooden swords under the excuse โ€œmy codeโ€™s compilingโ€?

Join us to learn how to supercharge your Maven build! Weโ€™ll cover three main mechanisms that you can apply in your project. Learn how each speeds up your build, when they provide the biggest gains, and what pitfalls await.

So take the next step in boosting your developer productivity by learning practical tips to decrease context switching and increase development speed and feedback cycle. Your journey from snailโ€™s pace to rocket speed begins today!

Maarten Mulders

June 13, 2024
Tweet

More Decks by Maarten Mulders

Other Decks in Technology

Transcript

  1. ACCELERATING MAVEN BUILDS FROM SNAIL'S PACE TO ROCKET SPEED #Maven

    #SnailToRocket Maarten Mulders (@mthmulders)
  2. , licensed under CC BY-NC 2.5, adopted to match the

    dark background and not hurt your eyes. "Compiling" by Randall Munroe #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  3. (Hans Dockter, Gradle Inc.) โ€œ We havenโ€™t seen a company

    yet where investing into more ef cient builds was not leading to a signi cant return on investment. The most successful software teams on the planet are the ones with an ef cient build infrastructure. Quantifying the Costs of Builds #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  4. HOW TO ACCELERATE? โฑ Measure ฬŽ Analyse a Improve #Maven

    #SnailToRocket Maarten Mulders (@mthmulders)
  5. MEASURE YOUR BUILD MEASURE YOUR BUILD MEASURE YOUR BUILD MEASURE

    YOUR BUILD MEASURE YOUR BUILD #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  6. THE OPENTELEMETRY MAVEN EXTENSION Time execution recorder for Maven Log

    time taken by each Mojo in the build lifecycle Comes as a "Plugin on steroids": replaces or hooks into Maven core features Maven extension #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  7. INSTALL THE EXTENSION 1. Create .mvn/extensions.xml in your project 2.

    Register the extension 3. Start an OpenTelemetry collector (such as Jaeger "all-in-one") โ†’ <extensions> <extension> <groupId>io.opentelemetry.contrib groupId> <artifactId>opentelemetry maven extension artifactId> <version>1.36.0-alpha version> extension> extensions> Simpler alternatives #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  8. ACTIVATE THE EXTENSION Run Maven as you did before, but

    add -Dotel.traces.exporter=otlp When done, open OpenTelemetry #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  9. PARALLEL TEST EXECUTION PARALLEL TEST EXECUTION PARALLEL TEST EXECUTION PARALLEL

    TEST EXECUTION PARALLEL TEST EXECUTION #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  10. WHAT IS IT? Parallel execution of tests inside a module.

    #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  11. WHEN TO APPLY If you have idle cores or tests

    waiting for I/O. 1. Executes large number of tests in parallel 2. Executes long-running tests in parallel #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  12. โš  CAVEATS 1. Tests must be parallelizable Non-thread safe tests

    will make the build will fail miserably. a Fix the tests a Add @Execute(SAME_THREAD) (JUnit speci c) 2. Can bottleneck the build Use resources that would've built another Maven module. a Disable parallel test execution for speci c modules Applies to parallel builds - more on that later. #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  13. HOW TO EXECUTE TESTS IN PARALLEL? 1. Through Sure re

    2. Through JUnit #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  14. 1. USING MAVEN'S SUREFIRE PLUGIN Specify what to run in

    parallel using the parallel parameter: 1. none 2. methods 3. classes 4. classesAndMethods 5. all #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  15. 1. USING MAVEN'S SUREFIRE PLUGIN (CTD.) Command line: mvn -Dparallel=classesAndMethods

    verify . In pom.xml : <plugin> <groupId>org.apache.maven.plugins groupId> <artifactId>maven suref re plugin artifactId> <version>3.2.5 version> <conf guration> <parallel>classesAndMethods parallel> conf guration> plugin> #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  16. PROS AND CONS Advantages: Supports both JUnit 4 and TestNG.

    Maintained by the Maven core team Disadvantages: All tests in a module need to be thread-safe. Con guration can be repetitive / verbose. Does not apply to JUnit Jupiter. #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  17. 2. USING JUNIT JUPITER 1. Using src/test/resources/junit-platform.properties 2. Using system

    properties through argLine parameter 3. Using Java annotations #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  18. Using src/test/resources/junit-platform.properties # activation of parallel test execution junit.jupiter.execution.parallel.enabled =

    true # tests per class (but classes in sequence) junit.jupiter.execution.parallel.mode.default = concurrent # classes in parallel junit.jupiter.execution.parallel.mode.classes.default = concurrent # both options can be combined! #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  19. Using system properties through argLine parameter <plugin> <groupId>org.apache.maven.plugins groupId> <artifactId>maven

    suref re plugin artifactId> <version>3.2.5 version> <conf guration> <argLine> -Djunit.jupiter.execution.parallel.enabled=true -Djunit.jupiter.execution.parallel.mode.default=concurrent argLine> conf guration> plugin> #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  20. Using Java annotations @Execution(CONCURRENT) class MyServiceTest { test methods are

    now being executed in parallel } #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  21. PROS AND CONS Advantages: Finer granularity of test executions Run

    single tests selectively in sequence Maintained by the JUnit Jupiter team Great IDE support w/ property les and annotations Disadvantages: Speci c to JUnit Jupiter, not in TestNG, Spock and others Harder to control from the command line #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  22. MAVEN DAEMON MAVEN DAEMON MAVEN DAEMON MAVEN DAEMON MAVEN DAEMON

    #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  23. WHAT IS IT? Provides faster Maven builds using a daemon

    process. #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  24. HOW DOES IT HELP? 1. Keeps the JVM and plugins

    "warm". 2. Runs multi-threaded by default without cluttering output. 3. Improved parallelism compared to "vanilla" Maven. Uses to build in topological order. Takari Smart builder #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  25. WHEN TO APPLY? 1. Large project. 2. Many plugins. 3.

    "Heavy" plugins. #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  26. โš  CAVEATS 1. Multi-threaded by default Non-threadsafe plugins may make

    build may fail miserably. a Circumvent by specifying -T 1 a Address the root cause: x the plugin(s) 2. Re-implements some parts of Maven Parsing .mvn/maven.config has some bugs: , . apache/maven-mvnd#913 apache/maven-mvnd#917 #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  27. HOW? 1. Install Maven Daemon 2. Retrain nger gymnastics: mvn

    โ†’ mvnd #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  28. HOW? (CTD.) A few new tricks: Use mvnd --status to

    inspect details of the daemon process. Add -Dmvnd.buildTime=true for simple timing report. Add -Dsmartbuilder.profiling=true for advanced timing report. #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  29. PARALLELIZING YOUR BUILD PARALLELIZING YOUR BUILD PARALLELIZING YOUR BUILD PARALLELIZING

    YOUR BUILD PARALLELIZING YOUR BUILD ! MAVEN MODULES EVERYWHERE ! #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  30. EVALUATION PROS Faster builds when there are enough modules with

    few, distinct dependencies (Almost) no need for ArchUnit or alike Easier to exclude transient dependencies via Maven CONS Could hinder navigation between classes and modules Needs awareness of runtime vs compile dependencies May not be suitable for libraries #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  31. TO DO OR NOT TO DO? As always: it depends!

    Pro ts vs. added complexity For libraries: impact for consumers? For applications: does it impact architecture? #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  32. DEMO This is what happens when you do not have

    any long-running tests: SHIRO W/ 62 MODULES (2:01 MIN) SHIRO W/ 58 MODULES (1:59 MIN) #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  33. MAVEN BUILD CACHE EXTENSION MAVEN BUILD CACHE EXTENSION MAVEN BUILD

    CACHE EXTENSION MAVEN BUILD CACHE EXTENSION MAVEN BUILD CACHE EXTENSION #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  34. HOW DOES THE BUILD CACHE WORK? Reduces build time by

    skipping parts of the build which don't need to be executed. 1. Only execute parts of the build which changed. 2. Build subtrees of a multi-module project in isolation. 3. Normalize the version to avoid rebuilding across forks. 4. Project state restoration (e.g. artifacts) #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  35. 1. WHICH PARTS HAVE CHANGED? A. MODIFICATIONS TO THE POM

    Changing dependencies, modifying plugins or executions, will invalidate the cache. B. MODIFICATION OF SOURCE CODE Use content ngerprinting to invalidate cache when source code changes. #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  36. WHEN TO USE THE CACHE EXTENSION? 1. You use Maven

    3.9 or Maven 4. 2. Your project does not rely on 'quirks'. 3. You want to speed up 1. local developer build, and/or 2. CI builds (PRs), and/or 3. want to share cache with co-workers. 4. You are willing to invest time to make the cache portable. #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  37. INSTALL THE EXTENSION 1. Create .mvn/extensions.xml in your project. 2.

    Register the extension by adding the following content: 3. (Optional) extend the default cache con guration <extensions> <extension> <groupId>org.apache.maven.extensions groupId> <artifactId>maven build cache extension artifactId> <version>1.2.0 version> extension> extensions> #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  38. โš  CAVEATS 1. Con gure relevant input les 2. Add

    plugin parameters for reconciliation 3. Filter non-essential les (IDE, etc) 4. Expect your tests not to run 5. Remote cache not used (OS, pro les...) 6. Cache eviction #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  39. HONOURABLE MENTIONS HONOURABLE MENTIONS HONOURABLE MENTIONS HONOURABLE MENTIONS HONOURABLE MENTIONS

    We did not cover: Reduce logging (Logback, Log4J, mvn -q ) โ€” try it! Christian Stein's "junit5-looming" (showcase): JUnit 5 test engine with virtual thread support Virus and threat protection #Maven #SnailToRocket Maarten Mulders (@mthmulders)
  40. WHAT DID WE LEARN? WHAT DID WE LEARN? WHAT DID

    WE LEARN? WHAT DID WE LEARN? WHAT DID WE LEARN? 1. Measure your builds 2. Parallel test execution per module 3. Using the Maven Daemon 4. Feed mvnd with architecture improvements 5. Maven Build Cache Extension #Maven #SnailToRocket Maarten Mulders (@mthmulders)