Creating an open source repo is very simple to do. There’s only a few configurations here and there to when setting up your github repo. I’ve found some challenges in creating a real release where someone could just put your library as a dependency in their build.gradle file. Here was my process of getting my project on Jitpack.
Note all of the following are build.gradle.kts files. You’ll need to adjust the lines if you’re using build.gradle instead.
First is configuring your project to generate a jar artifact. This needs to be done in the build.gradle.kts file:
tasks.withType<Jar> {
archiveBaseName.set("artifact-name")
from(sourceSets.main.get().output)
}
Next is to configure your group to match your github repo like so:group = "com.github.myusername"
version = "1.0.0"
Make sure to have the maven publish plugin:plugins {
java
`maven-publish`
}
And add the following block to configure maven-publish:publishing {
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
from(components["java"])
}
}
}
Lastly, you must point to where the jar is by creating a jitpack.yml file with the following:build:
publish:
artifacts:
paths:
- build/libs/*.jar
Normally you’d be done with those settings. Create a release in github using a tag, create a Jitpack account, look up your public git repo url that has those settings in the build.gradle.kts, and click Get It. Wait for your build to succeed, and now you have an artifact that can be listed as a dependency!
I had 2 additional issues with my project. First was the fact that I was adding additional local jars to my project that needed to be packaged with my artifact. I ended up using a shadow jar by doing the following.
Add the shadow jar to the plugins block:id("com.github.johnrengelman.shadow") version "8.1.1"
Remove the jar task and replace it with the following:tasks {
shadowJar {
archiveBaseName.set("artifact-name")
archiveClassifier.set("")
from(sourceSets.main.get().output)
}
jar {
enabled = false
}
build {
dependsOn(shadowJar)
}
}
Remove the from call in publications and replace it with the following:artifact(tasks.shadowJar.get())
The last issue I was having was my .java files weren’t in my source set, so I had to add the following to build.gradle.kts file:sourceSets {
main {
java {
setSrcDirs(listOf("src/main/kotlin"))
}
}
}
With that, I was able to successfully create a release of my public repo, have Jitpack create an artifact, and use it by adding it as a line in my dependencies.
Here is a link to my repo which I went through this process with. It’s a text-to-speech function specifically for libGDX.
Leave a Reply