πŸ’ΈSave up to $132K/month in CI costs!πŸ‘‰ Try Free✨
Skip to main content
← Back to workflows

How to Set Up Java JDK in GitHub Actions with setup-java

actions-setup-java -
GitHub Action
v4.2.1
1,442
Contributors
Contributor - konradpabjanContributor - dmitry-shibanovContributor - marko-zivic-93
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: 'Usage of setup-java GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '21'

setup-java logo

setup-java

Set up your GitHub Actions workflow with a specific version of Java


What is setup-java?​

The setup-java GitHub Action is a versatile tool for automating Java and Scala project setups in our CI/CD workflows. Here’s how you can use this action to enhance your Java development and deployment processes.

Key Features of setup-java​

  • Java Version Management: Automatically downloads and sets up specified versions of Java, supporting various distributions.
  • Local Java Setup: Allows extracting and caching a custom version of Java from a local file.
  • Build Tools Configuration: Configures environments for publishing with Apache Maven and Gradle.
  • Security Configuration: Supports setting up GPG private keys for secure transactions.
  • Error Handling: Registers problem matchers to easily identify errors in workflow logs.
  • Dependency Management: Enhances performance by caching dependencies managed by Apache Maven, Gradle, and sbt.
  • Maven Toolchains: Supports Maven Toolchains declarations for managing multiple JDK versions.

How to Cache Dependencies with Maven and Gradle in GitHub Actions​

Cache dependencies to speed up build times in Maven and Gradle:

- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-

- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}

How to Configure Publishing with Maven in GitHub Actions​

Configure the runner for publishing artifacts using Maven or Gradle:

- name: Setup Maven Central Repository
run: mvn deploy

- name: Setup Gradle Publish
run: gradle publish

How to Configure GPG Private Key in GitHub Actions​

Set up a GPG private key for signing artifacts securely:

- name: Import GPG Key
uses: actions/setup-java@v5
with:
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
gpg-passphrase: ${{ secrets.GPG_PASSPHRASE }}

How to Register Problem Matchers for Java in GitHub Actions​

Automatically detect and log errors during builds:

- name: Setup problem matchers for Java
uses: actions/setup-java@v5
with:
problem-matchers: 'java'