Skip to content
$ lynuxpro
← Maven & Build Tools

LESSON 1 OF 16 · BEGINNER · 14 MIN

What build tools do

Free previewLesson 1 of the 2 free lessons in this Pro course.Unlock all 16

HOW IT WORKSFrom source code to a published artifact

javacSource code.java filesCompilejavac to .class
1/6

Everything starts as source code. The compiler turns .java files into .class bytecode.

Every Java pipeline you will ever support starts the same way: a build tool turns source code into a tested, versioned artifact that the rest of the pipeline can ship. As a DevOps engineer you rarely write the Java, but you own the build: you make it fast, repeatable and secure, and you are the one paged when it breaks at 2 a.m. This lesson shows the problem a build tool solves by first doing the work by hand.

In simple words

  • Build tool: a program that turns source code into something you can run or ship: it downloads libraries, compiles, tests and packages.
  • Maven: the classic build tool for Java. Projects follow a standard layout and are described in a pom.xml file.
  • Gradle / npm: Gradle is a faster, script-based Java build tool. npm is the build and package tool for JavaScript.
  • Artifact: the file a build produces, such as a .jar or .war file.

The job, step by step

Turning code into something deployable is always the same chain of steps:

source code ──► compile ──► test ──► package ──► publish ──► deploy .java files    .class files  JUnit   .jar/.war   Artifactory  server, container, Kubernetes
OUTPUTterminalutf-8 · 2L
  • Compile: turn .java source into .class bytecode the JVM can run.
  • Resolve dependencies: download every library the code uses (and the libraries those use) at the right versions.
  • Test: run the unit tests and stop if any fail.
  • Package: zip the classes and resources into one artifact, usually a .jar (or .war for older web servers).
  • Publish: upload the artifact, with a version number, to a repository such as Artifactory or Nexus so other teams and later pipeline stages can use exactly that build.

A build tool does all of this from one command, the same way on every laptop and every CI runner.

Try it by hand first

The practice terminal has a small Java service, shop-api, in your home directory. Look at what is inside:

cd ~/shop-apifind src -name "*.java"
SHELLbashutf-8 · 2L
src/main/java/com/acme/shop/App.javasrc/main/java/com/acme/shop/PriceCalculator.javasrc/test/java/com/acme/shop/PriceCalculatorTest.javasrc/test/java/com/acme/shop/CheckoutIT.java
OUTPUTterminalutf-8 · 4L

PriceCalculator.java uses nothing but plain Java, so the Java compiler javac can build it on its own:

javac -d /tmp/out src/main/java/com/acme/shop/PriceCalculator.javals /tmp/out/com/acme/shop
SHELLbashutf-8 · 2L
PriceCalculator.class
OUTPUTterminalutf-8 · 1L

Now compile the whole application the same way:

javac -d /tmp/out src/main/java/com/acme/shop/*.java    # fails: the libraries are missing
SHELLbashutf-8 · 1L
src/main/java/com/acme/shop/App.java:3: error: package com.fasterxml.jackson.core does not existimport com.fasterxml.jackson.core.JsonProcessingException;                                 ^src/main/java/com/acme/shop/App.java:4: error: package com.fasterxml.jackson.databind does not existimport com.fasterxml.jackson.databind.ObjectMapper;                                     ^src/main/java/com/acme/shop/App.java:5: error: package org.slf4j does not existimport org.slf4j.Logger;                ^3 errors
OUTPUTterminalutf-8 · 10L

App.java uses Jackson (to write JSON) and SLF4J (for logging). Those libraries are not part of Java. To build by hand you would have to:

  1. Find the right Jackson and SLF4J jars and download them, plus every jar they need (Jackson databind needs jackson-core and jackson-annotations, for a start).
  2. Pass all of them on a long -cp classpath to javac.
  3. Compile the tests separately with JUnit and Mockito (and their dependencies) on the classpath.
  4. Run the tests with the JUnit launcher.
  5. Build a jar with the right manifest, and repeat it all on every machine, keeping versions in sync by hand.

That is fragile, slow and different on every machine. This is exactly what build tools were invented for.

The same thing with Maven

Maven reads one file, pom.xml, which lists the project's name, version and dependencies. Everything else follows conventions. One command does the whole chain:

mvn package
SHELLbashutf-8 · 1L
[INFO] Scanning for projects...[INFO][INFO] -------------------------< com.acme:shop-api >--------------------------[INFO] Building shop-api 1.0.0-SNAPSHOT[INFO]   from pom.xml[INFO] --------------------------------[ jar ]---------------------------------[INFO] Downloaded from central: https://repo.maven.apache.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.18.1/jackson-databind-2.18.1.jar (1.6 MB at 9.0 MB/s)...[INFO] --- compiler:3.13.0:compile (default-compile) @ shop-api ---[INFO] Compiling 2 source files with javac [debug release 21] to target/classes...[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0...[INFO] Building jar: /home/lynuxpro/shop-api/target/shop-api-1.0.0-SNAPSHOT.jar[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------
OUTPUTterminalutf-8 · 17L

In one command Maven downloaded the libraries from Maven Central (the public repository of Java libraries), compiled the code, compiled and ran the tests, and built the jar. Everything it produces goes in one folder, target/:

ls targetjava -jar target/shop-api-1.0.0-SNAPSHOT.jar
SHELLbashutf-8 · 2L
classes  jacoco.exec  original-shop-api-1.0.0-SNAPSHOT.jar  shop-api-1.0.0-SNAPSHOT.jar  surefire-reports  test-classes14:31:07.412 [main] INFO com.acme.shop.App -- Starting shop-apiOrder total: 99{"orderId":42,"total":99}
OUTPUTterminalutf-8 · 4L

Run mvn package a second time and notice how much quicker it is: the libraries are already cached in ~/.m2/repository, and the compiler sees nothing has changed.

Why this matters to DevOps

Without a build toolWith a build tool
"Works on my machine"Same result on every laptop and runner
Library versions copied around by handVersions declared once in pom.xml
Someone forgets to run the testsTests run on every build, failures stop it
Artifacts named app-final2.jarVersioned artifacts like shop-api-1.4.2.jar
No idea which libraries are insideA full dependency tree you can scan for CVEs

Your responsibilities around the build usually include:

  • CI pipelines that call the build tool (mvn verify, mvn deploy).
  • Caching the dependency cache so pipelines don't download the internet every run.
  • Artifact repositories (Artifactory, Nexus) and the credentials to publish to them.
  • Security: scanning dependencies, generating an SBOM, pinning versions.
  • Speed: parallel builds, build caches, only building what changed.

Maven, Gradle and npm

Three build tools cover most of what you will meet:

ToolEcosystemBuild fileStyle
MavenJava, Kotlinpom.xml (XML)Convention over configuration, fixed lifecycle
GradleJava, Kotlin, Androidbuild.gradle.kts (code)Flexible task graph, incremental and cached
npmJavaScript, TypeScriptpackage.jsonScripts plus a dependency lock file

They share the same ideas, so learning one deeply makes the others easy: declare dependencies, resolve them from a repository, run a standard set of steps, produce a versioned artifact. This course goes deep on Maven (still the most common build tool in enterprise Java) and covers Gradle and npm in lesson 13.

Where the build sits in a pipeline

The shop-api project already has a GitLab pipeline. Look at it:

cat .gitlab-ci.yml
SHELLbashutf-8 · 1L
image: maven:3.9-eclipse-temurin-21 variables:  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"  MAVEN_CLI_OPTS: "--batch-mode --errors --no-transfer-progress -s ci/settings.xml" cache:  key: maven-$CI_COMMIT_REF_SLUG  paths:    - .m2/repository stages: [build, test, publish] build:  stage: build  script:    - mvn $MAVEN_CLI_OPTS -DskipTests package...
NORMALyamlutf-8 · 18L

Every line here is a decision about the build: which Java image, where the dependency cache lives, which settings file holds the Artifactory credentials, and which Maven command runs in which stage. By the end of this course you will understand and be able to tune every one of them.

Key points

  • A build tool compiles, resolves dependencies, tests, packages and publishes from one command.
  • Doing it by hand fails as soon as the code uses a library: you would have to find and wire every jar yourself.
  • Maven's single source of truth is pom.xml; its output goes in target/; downloaded libraries are cached in ~/.m2/repository.
  • DevOps owns the build's speed, caching, repositories, credentials and security.
  • Maven, Gradle and npm follow the same ideas with different file formats.