2 min de lecturacode

Automating Java Builds with Maven and GitHub Actions CI/CD

A Step-by-Step Guide to Continuous Integration and Deployment

Automating Java Builds with Maven and GitHub Actions CI/CD

Continuous Integration and Continuous Deployment (CI/CD) streamline development workflows, improve software quality, and accelerate delivery. This tutorial guides you through setting up automated Java builds using Maven and GitHub Actions.

Prerequisites

  • A Java project with Maven configured (pom.xml).
  • GitHub repository.

Step 1: Setting Up Your GitHub Actions Workflow

In your repository, create the following directory structure:

.github/workflows/

Create a new file named maven-ci.yml within this directory:

name: Maven CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'

      - name: Build with Maven
        run: mvn clean install

Step 2: Understanding the Workflow

  • Trigger: Activated on push or pull request to the main branch.
  • Checkout: Retrieves the source code from your repository.
  • JDK Setup: Installs Java 21 from Temurin.
  • Maven Build: Executes Maven goals (clean install) to compile, test, and package your Java application.

Step 3: Add Maven Test Reports

Enhance your workflow by generating and uploading test reports:

- name: Run tests and upload reports
  run: |
    mvn test
  continue-on-error: true

- name: Upload test report
  uses: actions/upload-artifact@v4
  with:
    name: test-report
    path: target/surefire-reports/

Step 4: Deploy Artifacts (Optional)

To automatically deploy artifacts, include the deployment steps:

- name: Deploy package
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  run: mvn deploy

Ensure your pom.xml is configured for deployment (e.g., GitHub Packages).

Step 5: Committing and Running Your Workflow

Commit your workflow file and push the changes:

git add .github/workflows/maven-ci.yml
git commit -m "Add Maven CI GitHub Actions workflow"
git push

Navigate to the Actions tab on your GitHub repository to view and monitor your pipeline.

Advantages of Automating Java Builds

  • Rapid feedback on code integration.
  • Early identification of bugs and issues.
  • Automated testing and deployment.
  • Improved reliability and consistency.

Pricing

GitHub Actions offers a generous free tier suitable for most projects:

  • Free Tier:
    • 2,000 minutes per month (private repositories).
    • Unlimited minutes for public repositories.
    • Free usage of GitHub-hosted runners for most common tasks.
  • Additional Minutes:
    • $0.008 per minute for Linux runners.
    • $0.016 per minute for Windows runners.
    • $0.08 per minute for macOS runners.

For detailed and up-to-date pricing, visit GitHub Actions Pricing.

Conclusion

Using GitHub Actions with Maven provides an efficient and robust CI/CD pipeline, significantly improving your development lifecycle. Start implementing these practices today and streamline your Java application delivery.

Further Reading

Autor

Jaime de Arcos

Solutions architect y desarrollador backend.

Ver trayectoria completa

Comentarios