Goals
  1. Set up Maven build pipeline for a Java 11 app

  2. Release Maven artifact on GitHub using GitHub actions

Setup Maven

Assuming that we have a project named sample-app released for my hascode GitHub account:

We’re adding some release information to our project’s pom.xml:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hascode</groupId>
  <artifactId>sample-app</artifactId>
  <version>1.0.0-SNAPSHOT</version>bookmark-manager
  <name>sample-app</name>
  <description>hasCode.com Bookmark Manager</description>


  <scm>
    <developerConnection>scm:git:https://github.com/hascode/sample-app.git
    </developerConnection>
  </scm>


  <distributionManagement>
    <repository>
      <id>github</id>
      <name>GitHub</name>
      <url>https://maven.pkg.github.com/hascode/sample-app</url>
    </repository>
  </distributionManagement>


  <properties>
    <java.version>11</java.version>
    <project.scm.id>github</project.scm.id>
  </properties>

  [..]
</project>

Setup GitHub Actions

We’re creating the following directory structure in our project: .github > workflows > release.yml:

.github
└── workflows
    └── release.yml

This is the release.yml:

release.yml
name: Release Maven Artifact

on:
  push:
    branches:
      - release

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout project (1)
        uses: actions/checkout@v2
      - name: Cache local Maven repository (2)
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} (3)
          restore-keys: ${{ runner.os }}-maven-
      - name: Setup Java JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11 (4)
          server-id: github
      - name: Configure Git user
        run: |
          git config user.email "actions@github.com"
          git config user.name "GitHub Actions"
      - name: Publish JAR
        run: ./mvnw -B release:prepare release:perform (5)
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 We’re checking out our project
2 Maven dependencies are cached
3 The OS-identifier and the hashed pom-file are used as cache-key
4 Java 11 is used
5 Final command to perform our release

Pushing a release

We simply need to push changes from the master to the release branch and that is everything needed:

git push origin master:release
Results on GitHub
GitHub Overview
GitHub Releases
GitHub Tags
GitHub Maven Artifacts

Manual Release Process

To trigger a release by clicking a button on GitHub, we simply need to change the action trigger to on: workflow_dispatch.

Afterwards, we may click on ActionsRelease Maven ArtifactRun workflow to start the release process.

For completeness, the corresponding release.yml looks like this:

release.yml
name: Release Maven Artifact

on: workflow_dispatch

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout project
        uses: actions/checkout@v2
      - name: Cache local Maven repository
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-maven-
      - name: Setup Java JDK 17
        uses: actions/setup-java@v1
        with:
          java-version: 17
          server-id: github
      - name: Configure Git user
        run: |
          git config user.email "actions@github.com"
          git config user.name "GitHub Actions"
      - name: Publish JAR
        run: ./mvnw -B release:prepare release:perform
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}