In this article we are going to cover How to Build Java Project with Maven using GitHub Actions


  • A Java project with a pom.xml file configured for Maven.
  • A GitHub repository for your project.

Step #1:Create a .github/workflows directory

If it doesn’t exist, create a new directory named .github/workflows in the root of your GitHub repository. This directory will store your workflow YAML files.

Step #2:Create a workflow YAML file

Create a new file named maven.yml (or any desired name) inside the .github/workflows directory. This file will define the workflow steps.

Step #3:Add the workflow code

Paste the following code into the maven.yml file, replacing placeholders with your project’s details:

name: Build with Maven

    branches: [ main ]  # Change "main" to your main branch name if different
  pull_request:  # Optional: Trigger on pull requests

    runs-on: ubuntu-latest  # Or another desired runner OS

      - uses: actions/checkout@v3  # Checkout your code from the repository

      - name: Set up JDK 17  # Change version if needed
        uses: actions/setup-java@v3
          java-version: '17'
          cache: 'maven'  # Optionally cache Maven packages

      - name: Build with Maven
        run: mvn package -DskipTests  # Optionally add -DskipTests to skip tests

      # Uncomment these steps if you want to run tests:
      # - name: Run tests with Maven
      #   run: mvn test

Explanation of above workflow

  • name: The name displayed for the workflow in the GitHub Actions UI.
  • on: Specifies when the workflow should be triggered (e.g., on push to the main branch or pull requests).
  • jobs: Defines the jobs within the workflow. In this case, we have a single job named build.
    • runs-on: The operating system runner for the job.
    • steps: A list of steps to be executed in the job.
      • uses: actions/checkout@v3: Checks out your code from the GitHub repository.
      • uses: actions/setup-java@v3: Sets up the desired JDK version (change 17 if needed). Optionally enables caching of Maven packages (cache: 'maven').
      • run: mvn package -DskipTests: Runs the Maven package goal to build the project. Optionally, add -DskipTests to skip tests during the build.
        • Uncomment the steps for mvn test to run tests after building.

Step #4:Commit and push changes

  • Commit the maven.yml file and any other changes to your local repository.
  • Push the changes to your GitHub repository.

Step #5:Running the Workflow in GitHub Actions

Once you push the changes, GitHub Actions will automatically trigger the workflow and build your project using Maven. You can view the build status and logs in the GitHub Actions tab under your repository.


In this article we have covered How to Build Java Project with Maven using GitHub Actions.

Related Articles:

Push Docker Image to DockerHub Using GitHub Actions


Building and testing Java with Maven github action official page

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *