Tips for Gradle publishing on GitHub Action to GitHub Package
2022-09-17tl;dr If you follow the official guide to setup GitHub Action to publish Java packages to GitHub packages (Apache Maven Registry) and get stuck, this maybe the right place for answer.
Recently I have setup a GitHub Action (the CD/CI pipeline for GutHub) that publish my very first Java package to GitHub packages (Apache Maven Registry) with Gradle. While the official guide (the guide) provides the necessary information for GitHub Actions and GitHub Packages, it missed some for Gradle and CD/CI, which may causes some troubles for those who not familiar with those topics.
Tips
Following are some tips for publishing Java package to GitHub packages (Apache Maven Registry) with Gradle.
Missing publication for publishing block in build.gradle
After following the guile to add GitHub Package repository to publishing task, although status of the job says it's success, there is no package published to GitHub Packages with UP-TO-DATE
messsage for publishing task.
fig 1. example publishing task in build.gradle
from the guide showing only targeting repoistory
publishing {
...
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/anthonychwong/ValueBox"
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
}
}
}
}
It turns out that, without the publication block, publishing task don`t know what to publish. For details, check publishing in Gradle doc.
fig 2. add back the publications block in publishing task to tell Gradle what to publish
publishing {
publications {
gpr(MavenPublication) {
groupId "${group}"
artifactId "value-box"
version "${version}"
from components.java
}
}
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/anthonychwong/ValueBox"
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
}
}
}
}
Becareful about system environment variables
At one point, the publishing task complaining about access right problem. However, permission is likely not the cause, since GitHub Action from the guide indeed providing token with proper rights to publish already.
fig 3. GitHub Action example that already obtain token with required permissions
# ...
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# ...
In my case, there is a typo in environment variable name in build.gradle
causing failure in grabbing the proper user name. However, some said that quoting and special characters in username may cause the problem (here).
Bring in the gradle-wrapper.jar
This one is rather straight forward. Google the error message "Could not find or load main class org.gradle.wrapper.GradleWrapperMain" yields the directly solution, e.g. commit the gradle-wrapper.jar
to the repo.
Supprisingly, that jar is actually a binary which optimized to commit to the code base, and no offcial direct download link to it to avoid committing the binary.
Closing
It is understandable that GitHub docs only for GitHub stuffs, while, at the sametime, not everyone can be familiar with all required items before start. Hope that these few tips are helpful to them.
If you like this article, remember to show your support by buy me a book.