Introduction: Automating Java Development with GitHub Actions
Modern Java development is unthinkable without automation. Manual builds, testing, and deployments are not only a waste of time but also a source of critical errors. CI/CD (Continuous Integration / Continuous Deployment) solves this problem by turning routine operations into automated processes. One of the most popular tools for implementing CI/CD is GitHub Actions.
GitHub Actions is built directly into GitHub, making it an ideal choice for Java projects hosted on this platform. It allows you to create powerful pipelines that run on every push, pull request, or on a schedule. In this article, we will break down how to set up a complete CI/CD cycle for a Java application: from build and testing to deployment. You will learn how to write efficient YAML configurations, use dependency caching, and integrate popular tools such as Maven and Gradle.
1. GitHub Actions Basics for Java: Workflow, Jobs, and Steps
Before writing code, it is important to understand the architecture of GitHub Actions. Each pipeline is defined in a .github/workflows/*.yml file. The main components are:
- Workflow — this is the pipeline itself. It contains one or more jobs.
- Jobs — logical blocks that run on a single runner (virtual machine). Jobs can depend on each other.
- Steps — specific commands within a job: setting up the JDK, running Maven, publishing artifacts.
For Java projects, the key step is setting up the Java Development Kit (JDK). GitHub Actions provides a ready-made action actions/setup-java that supports all popular JDK distributions (Oracle, OpenJDK, Adoptium).
name: Java CI/CD Pipeline
on: push: branches: [ "main" ] pull_request: branches: [ "main" ]
jobs: build: runs-on: ubuntu-latest
steps: - uses: actions/checkout@v4 - name: Set up JDK 17 uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' - name: Build with Maven run: mvn clean install --file pom.xmlIn this example, we created a basic workflow that runs on push or pull request to the main branch. It uses Ubuntu, sets up JDK 17 (Temurin distribution), and executes a Maven build. This is the minimum set for Continuous Integration.
2. Build and Testing: Maven, Gradle, and Caching
Building is the heart of CI/CD. However, simply running mvn install is not enough. In large projects, downloading dependencies can take minutes. To speed up the process, use caching. GitHub Actions allows you to cache the ~/.m2/repository directory (for Maven) or ~/.gradle/caches (for Gradle).
Here is an example with caching for Maven:
jobs: build-and-test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Cache Maven dependencies uses: actions/cache@v3 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} restore-keys: | ${{ runner.os }}-maven-
- name: Set up JDK 17 uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin'
- name: Build and run tests run: mvn clean verify --file pom.xml
- name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: test-results path: target/surefire-reports/Note the if: always() flag in the test results upload step. It ensures that the artifact