Building and testing a Maven project
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A CI job must do on a clean Linux machine exactly what you do on your laptop. What does the job need so it builds ShopLite, runs the MySQL tests and leaves evidence when something fails?
actions/checkout and actions/setup-java
actions/setup-java is a ready-made step that downloads a JDK, puts it on the PATH and sets JAVA_HOME on the runner. Maven then compiles and tests with that JDK.
The runner starts empty, so the job first needs your code and a JDK. These two steps provide both.
steps: - uses: actions/checkout@v5 - uses: actions/setup-java@v5 with: distribution: temurin java-version: '25'actions/checkout clones your repository at the commit being built. distribution: temurin picks Eclipse Temurin, the same OpenJDK build as the Docker images, and java-version: '25' picks the latest Java 25 release.
Quote the version, so YAML keeps it as text (a number like 21.0 would lose its zero). ShopLite compiles with release 17 from pom.xml, so a newer JDK on the runner is fine.
./mvnw -B verify and the Permission denied error
The executable bit is the permission that lets Linux run a file as a program. Git stores it in the file mode: 100755 is executable, 100644 is a normal file.
Linux runs a script only if Git recorded it as executable. This program reads git ls-files lines and reports each file's mode.
Example 1 · Is mvnw executable in Git? Reading git ls-files
import java.util.List;
public class GitModeDemo { // Lines as printed by: git ls-files -s mvnw mvnw.cmd pom.xml static void check(String line) { String[] parts = line.split("\\s+"); String mode = parts[0]; String file = parts[parts.length - 1]; boolean executable = mode.equals("100755"); System.out.printf("%-9s mode %s -> %s%n", file, mode, executable ? "executable on Linux" : "NOT executable on Linux"); }
public static void main(String[] args) { List<String> before = List.of( "100644 3a0c8b9e2f1d4c5b6a7e8f9d0c1b2a3e4f5d6c7b 0\tmvnw", "100644 9f8e7d6c5b4a3f2e1d0c9b8a7f6e5d4c3b2a1f0e 0\tmvnw.cmd", "100644 1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c 0\tpom.xml"); System.out.println("Before:"); before.forEach(GitModeDemo::check);
System.out.println("After git update-index --chmod=+x mvnw:"); check("100755 3a0c8b9e2f1d4c5b6a7e8f9d0c1b2a3e4f5d6c7b 0\tmvnw"); }}Before: mvnw mode 100644 -> NOT executable on Linux mvnw.cmd mode 100644 -> NOT executable on Linux pom.xml mode 100644 -> NOT executable on Linux After git update-index --chmod=+x mvnw: mvnw mode 100755 -> executable on Linux
What this code does
- Git stores one permission per file:
100644is a normal file and100755is executable. A Linux runner checks outmvnwwith the mode Git recorded, so100644gives "Permission denied". mvnw.cmdis the Windows script and does not need the bit. Aftergit update-index --chmod=+x mvnwand a commit, every Linux and macOS checkout can run./mvnw.
Example 2 · The Permission denied failure and the fix
Runs in: the shoplite-api Git repository and its GitHub Actions run
# On your laptop (any OS), once:git update-index --chmod=+x mvnwgit commit -m "build: make mvnw executable"git push(before the fix, in the Actions log) Run ./mvnw -B verify /home/runner/work/_temp/9b1c2d3e.sh: line 1: ./mvnw: Permission denied Error: Process completed with exit code 126. (after the fix) Run ./mvnw -B verify [INFO] Scanning for projects... [INFO] Building shoplite-api 0.0.1-SNAPSHOT
What this code does
- Exit code 126 means "found but cannot execute". The fix changes only the file mode in Git, not its content;
git statusshows it as a mode change. - A
chmod +x mvnwstep in the workflow would also work, but every new workflow or teammate on Linux would meet the same error again.
./mvnw -B verify runs the Maven Wrapper, so CI uses the exact Maven version recorded in .mvn/wrapper/maven-wrapper.properties, the same as your laptop. verify runs compile, unit tests (Surefire), packaging and integration tests (Failsafe). -B is batch mode: no progress bars or colour codes, and it never waits for input.
Projects created on Windows often commit mvnw without the bit. A run: chmod +x mvnw step also works, but it only hides the problem for that one workflow.
Predict the output
Will ./mvnw run?
Read the line from git ls-files -s, then pick the answer.
Round 1 of 3
The Linux runner executes ./mvnw -B verify. What happens?
Testcontainers in CI: Docker on ubuntu-latest
ubuntu-latest is the label of GitHub's hosted Linux runner. The machine has Docker Engine installed and running, so Testcontainers tests can start Linux containers such as mysql:8.4 with no extra set-up.
ShopLite's integration tests start a real MySQL container. On ubuntu-latest that works like on your laptop: the first MySqlTestcontainersConfig start pulls mysql:8.4, Ryuk cleans up, and the whole machine is thrown away at the end.
Other runners are a poor fit for Java jobs: macOS runners have no Docker, and Windows runners run Windows containers, not the Linux mysql:8.4 image. Never enable Testcontainers reuse in CI; each run must start clean.
Fix the error
Pick the right runner
Choose the line that replaces the runner.
The integration tests fail. Which line replaces line 3?
Error
Could not find a valid Docker environment. Please see logs and check configuration
Test reports and the jar: actions/upload-artifact
An artifact is a file that a job saves to the workflow run, so you can download it from the run's page after the runner is gone.
ShopLite's build-test job now saves two artifacts: the test reports every time, and the jar only when the build passed.
Example 3 · .github/workflows/ci.yml: the build-test job
Runs in: a GitHub repository with the shoplite-api project
jobs: build-test: runs-on: ubuntu-latest # has Docker for Testcontainers timeout-minutes: 20 steps: - uses: actions/checkout@v5 - uses: actions/setup-java@v5 with: distribution: temurin java-version: '25' - name: Build and test (unit + MySQL Testcontainers) run: ./mvnw -B verify - name: Upload test reports if: always() # also when the tests failed uses: actions/upload-artifact@v4 with: name: test-reports path: | target/surefire-reports/ target/failsafe-reports/ retention-days: 7 - name: Upload jar uses: actions/upload-artifact@v4 with: name: shoplite-api-jar path: target/shoplite-api-*.jarRun ./mvnw -B verify ... INFO org.testcontainers.DockerClientFactory -- Connected to docker: Server Version: 28.0.4 Operating System: Ubuntu 24.04.3 LTS INFO tc.mysql:8.4 -- Pulling docker image: mysql:8.4. Please be patient; this may take some time but only needs to be done once. INFO tc.mysql:8.4 -- Container mysql:8.4 started in PT9.8S ... [INFO] Tests run: 41, Failures: 0, Errors: 0, Skipped: 0 [INFO] BUILD SUCCESS Run actions/upload-artifact@v4 With the provided path, there will be 46 files uploaded Artifact test-reports has been successfully uploaded!
What this code does
- Docker is already running on the Ubuntu runner, so the Testcontainers tests just work; the image is pulled on every run because each runner is new (the caching lesson speeds up the Maven side).
- Reports are uploaded with
if: always(); the jar upload has noif, so it runs only when the build passed.timeout-minutesstops a stuck run from burning 6 hours of minutes.
actions/upload-artifact takes a name and a path (folders and patterns allowed). The Surefire and Failsafe reports live in target/, and if: always() saves them even when the tests fail.
The test failure itself still fails the build: Maven exits with a non-zero code, the step turns red, and so does the whole check.
Predict the output
Is the artifact uploaded?
The test step fails. Decide for each upload step.
Round 1 of 2
The tests fail. Is the report uploaded?
Reading a failed run
The Results block is the summary Maven prints near the end of a test run. It lists each failing test as Class.method:line with the assertion message.
A red run prints a long log. This sample log and a small Java reader show how to find the failing test fast.
Example 4 · A red run: the log and how to read it
Runs in: the Actions tab of the shoplite-api repository
Search the failed step's log for: <<< FAILURE! or [ERROR] Failures:[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.41 s <<< FAILURE! -- in com.shoplite.order.OrderTotalsIT [ERROR] com.shoplite.order.OrderTotalsIT.orderTotalIsSumOfLineTotals -- Time elapsed: 0.052 s <<< FAILURE! org.opentest4j.AssertionFailedError: expected: 3097.00 but was: 3596.00 ... [INFO] Results: [INFO] [ERROR] Failures: [ERROR] OrderTotalsIT.orderTotalIsSumOfLineTotals:61 expected: 3097.00 but was: 3596.00 [INFO] [ERROR] Tests run: 41, Failures: 1, Errors: 0, Skipped: 0 [INFO] BUILD FAILURE [ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:3.5.6:verify (default) on project shoplite-api: There are test failures. Error: Process completed with exit code 1.
What this code does
- The Results block names the class, method and line (
OrderTotalsIT.orderTotalIsSumOfLineTotals:61) and the assertion: the total is 499 too high, so one line was counted twice. - The failure comes from Failsafe's
verifygoal, and Maven's exit code 1 turns the step and the check red. Download thetest-reportsartifact for the full report of every test.
Example 5 · Finding failing tests in a log with Java
import java.util.List;
public class FailedTestFinderDemo { public static void main(String[] args) { String log = """ [INFO] Results: [INFO] [ERROR] Failures: [ERROR] OrderTotalsIT.orderTotalIsSumOfLineTotals:61 expected: 3097.00 but was: 3596.00 [ERROR] Errors: [ERROR] ProductApiIT.createsProduct:88 DataIntegrity Duplicate entry 'MOB-T001' for key 'products.uq_products_sku' [INFO] [ERROR] Tests run: 41, Failures: 1, Errors: 1, Skipped: 0 """; List<String> failing = log.lines() .filter(l -> l.startsWith("[ERROR] ")) .map(l -> l.substring("[ERROR] ".length()).split(" ")[0]) .toList(); String summary = log.lines().filter(l -> l.contains("Tests run:")).findFirst().orElse("");
System.out.println("Failing tests (Class.method:line):"); failing.forEach(t -> System.out.println(" " + t)); System.out.println("Summary: " + summary.substring(summary.indexOf("Tests run:"))); }}Failing tests (Class.method:line): OrderTotalsIT.orderTotalIsSumOfLineTotals:61 ProductApiIT.createsProduct:88 Summary: Tests run: 41, Failures: 1, Errors: 1, Skipped: 0
What this code does
- Maven indents each failing test with three spaces after
[ERROR], so one filter finds them all. A failure is a broken assertion; an error is an unexpected exception, here a duplicate SKU from test data that was not unique. - Run each one locally with
./mvnw -B verify -Dit.test=OrderTotalsITbefore changing anything.
In a red run, open the failed step and search the log (the magnifier at the top) for <<< FAILURE! or [ERROR] Failures:.
Then reproduce it locally with ./mvnw -B verify, or just one class with -Dit.test=OrderTotalsIT for integration tests (-Dtest=... for unit tests). If it passes locally and fails in CI, compare versions (JDK, image tags) and look for order-dependent tests or time zones: the runner uses UTC, not IST.
Order of execution
Debug a red run
Tap the steps in the order a developer takes them.
Next step
Check your understanding
12 quick questions