How to automate versioning of Maven project in GitHub? - maven

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.

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 }}

Related

How to release maven project from Gitlab without Snapshot?

.gitlab.yml has following
stages:
- test
- publish
- deploy
- manual
# Test all commits regardless of branch in a WildFly 10 instance
test:wf10:
stage: test
script:
- mvn clean test -U -Dprofile.wildfly10 -Dwildfly10.servergroup=masterdata-web
tags:
- test
# Publish tagged release to Artifactory (WildFly 10)
publish-release:wf10:
stage: publish
only:
- /^v-.*$/
script:
- mvn clean deploy -B -U -P wildfly10 -Dmaven.test.skip=true
tags:
- test
# Manually deploy any build to WildFly 10 servergroup Masterdata-web
deploy-test:wf10:
stage: manual
environment:
name: test
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=masterdata-web
tags:
- test
when: manual
# Manually deploy tagged release to WildFly 10 servergroup Masterdata-web
deploy-qa:wf10:
stage: manual
environment:
name: qa
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=masterdata-web
tags:
- qa
when: manual
# Automatically deploy build staged for release to WildFly 10 servergroup Masterdata-web
deploy-release-qa:wf10:
stage: deploy
environment:
name: qa
only:
- master
except:
- /^v-.*$/
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=masterdata-web
tags:
- qa
when: manual
# Automaticaly deploy tagged release to WildFly 10 servergroup Masterdata-web
deploy-release-prod:wf10:
stage: deploy
environment:
name: prod
only:
- /^v-.*$/
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=terp
tags:
- prod
# Manually generate project report for WildFly 10, report is stored for 2 weeks
generate-report:wf10:
stage: manual
script:
- mvn clean test site:site -B -U -P wildfly10
- mv target/site ./
artifacts:
name: report-wf10
expire_in: 2 weeks
paths:
- site
tags:
- test
when: manual
# Manual deploy tagged release to WildFly 10 servergroup servicedesk in PROD
deploy-prod:wf10:
stage: deploy
environment:
name: prod
#only:
# - /^v-.*$/
script:
- mvn clean package -B -U wildfly:undeploy wildfly:deploy -Dprofile.wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=terp
tags:
- prod
when: manual
For release every time, I need to run following commands by check out branch:
mvn release:prepare -Darguments=-Dprofile.wildfly10 -DignoreSnapshots=true
mvn release:clean
git push --follow-tags
Then it relaeases without SNAPSHOT
If i create manual branch it always append SNAPSHOT.
Can you suggest how to create manual pipeline which deploys without SNapshot and from Gitlab only?
Maven is doing exactly what you have ordered it to do. The command like:
mvn clean deploy -B -U -P wildfly10 -Dmaven.test.skip=true
orders maven to compile, package and deploy artifacts to remote repository, and when doing that maven picks up target version from pom.xml, thus to build and deploy artifacts with specific version you need to tell maven what version to use explicitly or implicitly, for example, when you are doing:
mvn release:prepare -Darguments=-Dprofile.wildfly10 -DignoreSnapshots=true
git push --follow-tags
maven-release-plugin creates two commits in SCM: the first one with "release" version in pom.xml (accompanied by tag/branch), the second one with "snapshot" version, and the first commit triggers your publish job. That explains why creating branch manually does not makes sense.
There are a couple of options to implement release management in maven project using GitLab:
create pipeline forms in GitLab to request information about releaseVersion and developmentVersion, in that case maven release command would be something like mvn --batch-mode -Prelease release:clean release:prepare -DreleaseVersion=${releaseVersion} DdevelopmentVersion=${developmentVersion} (most likely extra setup will required to allow maven to push changes)
let maven to choose versions automatically (or implement version policies), alternatively you make take advantage of using my extension: version-policy-extension
use Maven CI Friendly Versions - in that case you will need to pass ${revision} from GitLab
implement external tools (scripts, docker images) to automate that

In github actions, mvn install says that pom.xml does not exist

Here is my repository: https://github.com/klashdevelopment/klashitemsystem
I'm trying to get the GitHub action in there to work (i am brand new to GitHub actions) What I want to do in my action is: when there is a push, run mvn install and release the jar file to releases. I'm not too sure how to do this, and I tried to smash together a few other people's GitHub actions to do what I want. When I run this, however, it says that no pom.xml exists (when it does exist in the repository) I even tried setting the working directory. Anyone know why?
Error: The goal you specified requires a project to execute but there is no POM in this directory (D:\a\klashitemsystem\klashitemsystem). Please verify you invoked Maven from the correct directory. -> [Help 1]
Workflow:
steps:
- name: Set up JDK
uses: actions/setup-java#v1
with:
java-version: 17
- name: Packaging Jar
id: packaging-jar
shell: bash
run: |
cd .
mvn clean install
You need to check out your code. Add this step:
steps:
- name: Check out Repository
uses: actions/checkout#v3
- name: Set up JDK
uses: actions/setup-java#v3
with:
java-version: 17
- name: Packaging Jar
id: packaging-jar
shell: bash
run: mvn clean install

Github Workflow Maven Caching - Multi-Module Project

I am working on integrating Sonar for a multi-module Maven Project which has 3 modules. I need to speed up the Maven Build Process by enabling caching. Sonar Reports are working correctly but still the caching is not happening. Below here find the Github Workflow File with the configurations which I want to cache the dependencies only relevant to the cloud-pci-ui module
Module Structure
Maven Caching Error
name: Analyze pull request
on:
# Trigger analysis when open edit, synchronize or re-opene a pull request.
pull_request:
types: [opened, edited, synchronize, reopened]
env:
# Sets environment variable
AWS_REGION : us-east-1
jobs:
buildAndAnalysePR:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- uses: actions/setup-java#v2
with:
distribution: 'adopt'
java-version: '8'
cache: 'maven'
- name: Cache local Maven repository
uses: actions/cache#v2
with:
path: ~/cloud-pci-ui/.m2/repository/
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
- name: Build PCI-API
run: |
cd cloud-pci-api
mvn clean install -DskipTests

Github Action for mvn deploy parent pom fails

I am trying to create a parent/central pom which I plan to use across all my project implementations as a common point for version management of all dependencies.
To make this central dependency pom available to other projects, I want to create a git package so that other projects can import this central dependency pom from git package.
Currently while running github action to publish package, I get the following error.
Error: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project central-java-dependency: Failed to deploy artifacts: Could not find artifact com.dishant:central-java-dependency:pom:1.0.0 in github (https://maven.pkg.github.com/dishantkamble) -> [Help 1]
Error:
Error: To see the full stack trace of the errors, re-run Maven with the -e switch.
Error: Re-run Maven using the -X switch to enable full debug logging.
Error:
Error: For more information about the errors and possible solutions, please read the following articles:
Error: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Error: Process completed with exit code 1.
Below is my action yml
name: Java CI with Maven
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up JDK 11
uses: actions/setup-java#v1
with:
java-version: 11
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Publish to GitHub Packages Apache Maven
run: mvn deploy
env:
GITHUB_TOKEN: ${{ github.token }}
My pom contains distributionManagement as follows
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Dishant Apache Maven Packages</name>
<url>https://maven.pkg.github.com/dishantkamble</url>
</repository>
</distributionManagement>
Can someone help to get this central dependency pom deployed to github packages so that my other projects can successfully import this pom as parent dependency.
I was able to fix this with the following two changes.
maven.yml
name: Java CI with Maven
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up JDK 11
uses: actions/setup-java#v1
with:
java-version: 11
- name: Build with Maven
run: mvn -B package --file pom.xml install
- name: Publish to GitHub Packages Apache Maven
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml -DskipTests
env:
GITHUB_TOKEN: ${{ github.token }}
pom.xml
update existing distribution management with the repository name
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Dishant Apache Maven Packages</name>
<url>https://maven.pkg.github.com/dishantkamble/central-java-dependency</url>
</repository>
</distributionManagement>

Maven build in github continuous integration action

I am using Github actions to set up a continuous integration workflow for a Maven app. The Maven app depends on some external dependencies as well as a couple other repositories that we built. In my Github action's .yaml file, I've included the line "mvn install". When I make a commit, the action begins to run the .yaml file as expected. The build mostly works, and can find external maven dependencies from maven central. However, the build cannot find the two other private repositories. These repositories are saved on the same github account as the repo. for this app.
Downloading from central: https://repo.maven.apache.org/maven2/com/<repo>/1.3/<repoName>-1.3.pom
[WARNING] The POM for com.<repo>/<reponame>.core:jar:1.3 is missing, no dependency information available
Here is my github/workflow action's yaml file...
name: Build1
on:
push:
branches:
- '*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
- name: Build
run: mvn install

Resources