Skip to content

Instantly share code, notes, and snippets.

@2xsaiko
Last active May 13, 2018 17:01
Show Gist options
  • Save 2xsaiko/88c1e4d28ef89661501f19e19f832a03 to your computer and use it in GitHub Desktop.
Save 2xsaiko/88c1e4d28ef89661501f19e19f832a03 to your computer and use it in GitHub Desktop.
Gradle Publish to Maven repository hosted on Git

Instructions

Put the publish.gradle.kts file next to your build.gradle.

Edit the RepoURL and RepoBranch properties. (each time you change these, make sure to delete the build/maven folder!)

Add apply from: 'publish.gradle.kts' (Groovy) or apply { from("publish.gradle.kts") } (Kotlin) to your build.gradle.

Test if everything works by executing the updateLocalRepo task, there should now be a git repository in build/maven.

Execute the publish task to publish! It'll handle everything git-related for you.

// Git Publish v1.0.4
// by therealfarfetchd
import org.gradle.jvm.tasks.Jar
// Set this to whatever Git repository you want. You must be able to push to it.
val RepoURL = "ssh://[email protected]/therealfarfetchd/maven.git"
val RepoBranch = "master"
val LocalRepoURL = File(buildDir, "maven")
buildscript {
val kotlin_version: String? by extra
repositories { mavenCentral() }
dependencies { classpath(kotlin("gradle-plugin", kotlin_version ?: "1.2.41")) }
}
plugins { java }
apply { plugin("maven-publish") }
val sourcesJar by tasks.creating(Jar::class) {
classifier = "sources"
from(the<JavaPluginConvention>().sourceSets["main"].allSource)
}
configure<PublishingExtension> {
repositories {
maven { url = LocalRepoURL.toURI() }
}
(publications) {
"mavenJava"(MavenPublication::class) {
from(components["java"])
artifact(sourcesJar)
}
}
}
tasks {
"setupLocalRepo"(Exec::class) {
group = "git"
onlyIf { !LocalRepoURL.exists() }
LocalRepoURL.parentFile.mkdirs()
commandLine = listOf("git", "clone", RepoURL, "-b", RepoBranch, LocalRepoURL.path)
}
"updateLocalRepo"(Exec::class) {
group = "git"
dependsOn("setupLocalRepo")
workingDir = LocalRepoURL
commandLine = listOf("git", "pull")
}
"addFilesLocal"(Exec::class) {
group = "git"
dependsOn("setupLocalRepo")
workingDir = LocalRepoURL
commandLine = listOf("git", "add", ".")
}
"commitFilesLocal"(Exec::class) {
group = "git"
dependsOn("addFilesLocal")
workingDir = LocalRepoURL
commandLine = listOf("git", "commit", "-m", "Build ${project.name}")
}
"updateRemoteRepo"(Exec::class) {
group = "git"
dependsOn("commitFilesLocal")
workingDir = LocalRepoURL
commandLine = listOf("git", "push")
}
"publish" {
dependsOn("updateLocalRepo")
finalizedBy("updateRemoteRepo")
mustRunAfter("updateLocalRepo")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment