-
Notifications
You must be signed in to change notification settings - Fork 0
Add Maven publishing support for GitHub Packages #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: hossain-khan <[email protected]>
lib module jar as github package.
hossain-khan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay! looks good, just need to add the workflow later.
|
|
||
| // Configure JAR to include sources | ||
| val sourcesJar by tasks.registering(Jar::class) { | ||
| archiveClassifier.set("sources") | ||
| from(sourceSets.main.get().allSource) | ||
| duplicatesStrategy = DuplicatesStrategy.EXCLUDE | ||
| } | ||
|
|
||
| // Publishing configuration | ||
| publishing { | ||
| publications { | ||
| register<MavenPublication>("maven") { | ||
| groupId = "hossain.dev" | ||
| artifactId = "json5kt" | ||
| version = "1.0.0" | ||
|
|
||
| from(components["java"]) | ||
| artifact(sourcesJar) | ||
|
|
||
| pom { | ||
| name.set("JSON5 Kotlin") | ||
| description.set("A robust JSON5 parser and serializer for Kotlin/JVM that extends JSON with helpful features like comments, trailing commas, and unquoted keys while maintaining full backward compatibility with JSON.") | ||
| url.set("https://github.com/hossain-khan/json5-kotlin") | ||
|
|
||
| licenses { | ||
| license { | ||
| name.set("MIT License") | ||
| url.set("https://opensource.org/licenses/MIT") | ||
| } | ||
| } | ||
|
|
||
| developers { | ||
| developer { | ||
| id.set("hossain-khan") | ||
| name.set("Hossain Khan") | ||
| email.set("[email protected]") | ||
| } | ||
| } | ||
|
|
||
| scm { | ||
| connection.set("scm:git:git://github.com/hossain-khan/json5-kotlin.git") | ||
| developerConnection.set("scm:git:ssh://github.com:hossain-khan/json5-kotlin.git") | ||
| url.set("https://github.com/hossain-khan/json5-kotlin") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| repositories { | ||
| maven { | ||
| name = "GitHubPackages" | ||
| url = uri("https://maven.pkg.github.com/hossain-khan/json5-kotlin") | ||
| credentials { | ||
| username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") | ||
| password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN") | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do I trigger maven publish to github package using this definition. When would I do it - what is the best practices?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To trigger publishing to GitHub Packages using this Gradle definition, you would use the following command from your terminal in the project root:
./gradlew publishThis command will execute the publishing block defined in your build.gradle.kts file and upload your artifact (including your main JAR and sources JAR) to the GitHub Packages Maven repository specified in the configuration.
How Authentication Works
- The credentials for GitHub Packages (username and password/token) are read from Gradle properties (
gpr.userandgpr.key) or environment variables (USERNAMEandTOKEN). - For best security and automation, set your
TOKENenvironment variable to a GitHub personal access token (withwrite:packagesandread:packagesscope).
When and How to Publish (Best Practices)
-
Release Process:
- Only publish on release branches or tags, not for every commit.
- Consider automating with GitHub Actions—trigger on push to
main,master, or tag creation.
-
Version Management:
- Update the
versionfield in your publishing configuration to reflect semantic versioning and avoid overwriting packages.
- Update the
-
CI/CD Automation:
- Use GitHub Actions to run
./gradlew publishafter running tests and checks. - Store your GitHub token as a secret in your repository, and use it in your workflow.
Example GitHub Actions step:
- name: Publish to GitHub Packages run: ./gradlew publish env: USERNAME: ${{ github.actor }} TOKEN: ${{ secrets.GITHUB_TOKEN }}
- Use GitHub Actions to run
-
Local Publishing (for testing):
- You can run
./gradlew publishlocally with your environment variables set to test the process.
- You can run
Recap
- Trigger publishing:
./gradlew publish - Best time to publish: after successful tests, typically on release/tag commits
- Automate with CI/CD for consistency and reliability
- Manage authentication via environment variables or secrets
Let me know if you want an example workflow file or more details!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Please provide example workflow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly! Here is an example GitHub Actions workflow to publish your Maven package to GitHub Packages using the Gradle configuration you provided.
Save this as .github/workflows/publish.yml in your repository:
name: Publish to GitHub Packages
on:
push:
tags:
- 'v*' # Triggers on version tags like v1.0.0
workflow_dispatch: # Allows manual trigger
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17' # Match your project's Java version
- name: Cache Gradle packages
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
- name: Grant execute permission for Gradle wrapper
run: chmod +x ./gradlew
- name: Publish to GitHub Packages
env:
USERNAME: ${{ github.actor }}
TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew publishHow this works:
- Triggers when you push a tag like v1.0.0, or manually.
- Checks out code, sets up Java, and prepares Gradle.
- Runs ./gradlew publish, using the built-in GITHUB_TOKEN for authentication.
Best Practices:
- Only publish on tags to avoid pushing development versions.
- Use semantic versioning in your tags (e.g. v1.2.3).
- Optionally, restrict publishing to main or release branches if preferred.
Let me know if you want it to also publish on branch pushes, or if you need help customizing further!
This PR implements Maven publishing for the
libmodule to GitHub Packages, enabling the library to be consumed as a dependency via Gradle dependency management.Changes Made
maven-publishplugin tolib/build.gradle.ktsgroupId:hossain.devartifactId:json5ktversion:1.0.0Publishing Tasks Available
After these changes, the following publishing tasks are now available:
Usage
Once published, the library can be consumed by adding the GitHub Packages repository and dependency:
repositories { maven { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/hossain-khan/json5-kotlin") credentials { username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN") } } } dependencies { implementation("hossain.dev:json5kt:1.0.0") }Testing
Fixes #12.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.