So you want a predictable Java build that does not waste everyone's morning. Good news, GitHub Actions and Maven get along just fine when you set up a workflow that checks out code, picks a JDK, caches dependencies, runs tests and then packages or uploads artifacts. This short guide gives a practical workflow you can copy and adapt without sacrificing your sanity.
CI speed is mostly about not downloading the internet over and over. Cache Maven dependencies to skip repeated jar downloads. Locking the Java runtime avoids those mysterious failures that only show up on Monday mornings when the universe is already tired. Caching plus a consistent JDK yields faster and more reproducible continuous integration runs for Java projects.
.github/workflows
so contributors do not play hide and seek~/.m2/repository
if you prefer explicit controltest
or package
This example is compact and uses setup-java's maven cache option for convenience. Tweak java-version and goals to match your project.
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
cache-dependency-path: '**/pom.xml'
- name: Build with Maven
run: mvn -B -V test package
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: target/*.jar
If you like full control or have a multi module repo you can use actions/cache to store the local Maven repository. Use a key that includes runner os, java version and a hash of your pom files to prevent stale dependencies.
- uses: actions/cache@v3
with:
path: ~/.m2/repository
key: $-maven-$
restore-keys: |
$-maven-
mvn -B
for non interactive runs and consistent logsIf dependencies are not restored check the key and the paths. Make sure your pom files are included in the hash when you base the cache key on them. If tests pass locally but fail in CI check the Java version and system locale. Small differences can produce dramatic Monday level surprises.
Follow these steps and your GitHub Actions workflow will give faster, clearer CI feedback for Java projects. You will also earn the quiet respect of anyone who has waited for a build while clutching a cold cup of coffee.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.