A presentation at DEVNEXUS in in Atlanta, GA, USA by Brian Demers
Kay Gaensler CC BY-NC-SA 2.0 https://www. ickr.com/photos/gaensler/5465098412 Maven 102 fl Best Practices Brian Demers Open Source Developer BrianDemers bdemers
Topics • Maven Demysti cation • Speed up your build • Things to Commit or Omit • Memes fi @BrianDemers | bdemers
Who is this guy? @BrianDemers | bdemers
source: Silicon Valley @BrianDemers | bdemers
VS @BrianDemers | bdemers
VS @BrianDemers | bdemers
VS @BrianDemers | bdemers
Developer Productivity Engineering @BrianDemers | bdemers
@BrianDemers | bdemers
Dependency Hell Last Commit: 2010-06-28 @BrianDemers | bdemers
Dependencies are a tree $ mvn dependency:tree junit-jupiter +- junit-jupiter-api | +- opentest4j | +- junit-platform-commons | - apiguardian-api +- junit-jupiter-params - junit-jupiter-engine source: pngitem.com - Anastasiya Alferova @BrianDemers | bdemers - junit-platform-engine
scans.gradle.com @BrianDemers | bdemers
Your Application Dependencies Your code @BrianDemers | bdemers
@BrianDemers | bdemers
Maven Build Lifecycle • • • • • • • • • • • • validate initialize generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources generate-test-resources process-test-resources @BrianDemers | bdemers • • • • • • • • • • • test-compile process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy
Maven Build Lifecycle Phase process-resources compile process-test-resources test-compile test package install deploy @BrianDemers | bdemers Plugin Goal resources:resources compiler:compile resources:testResources compiler:testCompile surefire:test jar:jar install:install deploy:deploy
Maven Build Lifecycle Phase process-resources compile process-test-resources test-compile test package verify install deploy @BrianDemers | bdemers Plugin Goal resources:resources compiler:compile resources:testResources compiler:testCompile surefire:test jar:jar pmd:check, checkstyle:check, jacoco:check install:install deploy:deploy
@BrianDemers | bdemers
Plugin Con guration fi @BrianDemers | bdemers
Plugin Con guration fi @BrianDemers | bdemers
Plugin Con guration (example) $ mvn buildnumber:create
[INFO] Storing buildNumber: 6231815 $ mvn buildnumber:create
[INFO] Storing buildNumber: 623181597908f815cb9af206a389e739d80435a5 fi @BrianDemers | bdemers
Custom Maven Plugins @Mojo(name = “roll”, defaultPhase = LifecyclePhase.VERIFY) public class DiceMojo extends org.apache.maven.plugin.AbstractMojo { @Parameter(property = “expression”, defaultValue = “1d20”) private String expression; public void execute() throws MojoExecutionException, MojoFailureException { getLog().info(“Dice eval: ” + Dice.roll(expression)); } } @BrianDemers | bdemers
Commit or Omit! @BrianDemers | bdemers
Dependency Ranges source: xkcd.com/2247 @BrianDemers | bdemers
Dependency Ranges @BrianDemers | bdemers Range Meaning (,1.0] x <= 1.0 (,1.0) x < 1.0 [1.0] x == 1.0 [1.0,) x >= 1.0 (1.0,) x > 1.0 (1.0,2.0) 1.0 < x < 2.0 [1.0,2.0] 1.0 <= x <= 2.0 (,1.0],[1.2,) x <= 1.0 or x >= 1.2. Multiple sets are comma-separated (,1.1),(1.1,) x != 1.1
Pin Versions https://area-b.com/blog/2019/05/04/1210/ @BrianDemers | bdemers
Separate Unit and Integration Tests @BrianDemers | bdemers
Use Pro les headshots: xkcd.com fi @BrianDemers | bdemers
Test Jars T S E T @BrianDemers | bdemers
Uberjars Stop using the Maven Shade Plugin @BrianDemers | bdemers
Source jars E C R U O S @BrianDemers | bdemers
@BrianDemers | bdemers
@BrianDemers | bdemers
Build Cache @BrianDemers | bdemers
Parallel Builds @BrianDemers | bdemers
Multiple Threads Project-A Project-B api model impl service app core utility webapp @BrianDemers | bdemers
Project-B model service core webapp @BrianDemers | bdemers utility
Test w/ Multiple Threads JVM OneTest.java @BrianDemers | bdemers TwoTest.java ThreeTest.java FourTest.java
Test w/ Multiple JVMs (forkCount) @BrianDemers | bdemers JVM JVM OneTest.java ThreeTest.java TwoTest.java FourTest.java
Test w/ Multiple JVMs & Multiple Threads JVM OneTest.java @BrianDemers | bdemers JVM TwoTest.java ThreeTest.java FourTest.java
source: https://levelup.gitconnected.com/be-careful-with-java-parallel-streams-3ed0fd70c3d0 @BrianDemers | bdemers
“The future is always beginning now.” –Mark Strand @BrianDemers | bdemers
Daemon Builds are faster If you keep the JVM running @BrianDemers | bdemers
4.0.0 @BrianDemers | bdemers
Questions? Thank you! BrianDemers bdemers Learn more & get free swag
Know enough about Maven to get by, but not enough to thrive? Then this talk is for you. We will review the basics and then dive into the best practices for both Maven single and multi-module projects. You will also walk away with enough knowledge to troubleshoot your builds. Apache Maven is still the defacto build tool in the Java world.