Search code examples
mavengithubworkflowreleaseversioning

How to automate versioning of Maven project in GitHub?


I am trying to make auto- or semi-auto versioning into GitHub.

I looked into the automation release workflow possibilities and it doesn't seem like anything standard for GitHub.

I want to have an automatic update of version in pom.xml whenever I create a tag on GitHub or whenever I merge into the master branch.


Solution

  • I found a workaround solution with Github Actions that can be extended further. Whenever I release and I manually specify release notes and changelog, so this release version can be taken from github.event.release.tag_name

    mvn -B versions:set -DnewVersion=${{ github.event.release.tag_name }} -DgenerateBackupPoms=false
    

    The reference to the actual GitHub workflow is here.

    .github/workflows/deploy.yml at the time of writing:

    name: Publish package to the Maven Central Repository
    on:
      release:
        types: [created]
    
    jobs:
      publish:
        runs-on: ubuntu-latest
        steps:
          - name: Check out Git repository
            uses: actions/checkout@v2
          - name: Install Java and Maven
            uses: actions/setup-java@v1
            with:
              java-version: 8
          - if: github.event.release
            name: Update version in pom.xml (Release only)
            run: mvn -B versions:set -DnewVersion=${{ github.event.release.tag_name }} -DgenerateBackupPoms=false
          - name: Release Maven package
            uses: samuelmeuli/action-maven-publish@v1.4.0
            with:
              maven_profiles: deploy, verify
              gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
              gpg_passphrase: ${{ secrets.GPG_PASSPHRASE }}
              nexus_username: ${{ secrets.OSSRH_USERNAME }}
              nexus_password: ${{ secrets.OSSRH_TOKEN }}