Skip to content

Instantly share code, notes, and snippets.

@hu553in
Last active November 17, 2025 09:33
Show Gist options
  • Select an option

  • Save hu553in/5e37cbff0d1e70b267754f9ef9824ec1 to your computer and use it in GitHub Desktop.

Select an option

Save hu553in/5e37cbff0d1e70b267754f9ef9824ec1 to your computer and use it in GitHub Desktop.
Gradle script for publishing JAR to GitLab package registry with custom POM file building
// Uses Gradle internal API - works on Gradle 7.6
import groovy.json.JsonSlurper
import org.gradle.api.internal.tasks.userinput.NonInteractiveUserInputHandler
import org.gradle.api.internal.tasks.userinput.UserInputHandler
plugins {
id 'maven-publish'
}
class Artifact {
String groupId
String artifactId
String version
}
class Repository {
String id
String url
}
class Registry {
String url
String deployToken
}
class Config {
String jar
Artifact artifact
List<Artifact> dependencies
List<Repository> repositories
Registry registry
}
def readConfigFilePathTask = tasks.register('readConfigFilePath') {
def userInputHandler = services.get(UserInputHandler.class)
def mustReadConfigFilePath = userInputHandler !instanceof NonInteractiveUserInputHandler
&& gradle.startParameter.taskNames.contains('publish')
ext.configFilePath = mustReadConfigFilePath
? userInputHandler.askQuestion("Enter JSON config file path", "config.json")
: null
}
def config
tasks.register('readConfig') {
dependsOn readConfigFilePath
def configFilePath = readConfigFilePathTask.get().configFilePath
if (configFilePath != null) {
def jsonFile = file(configFilePath)
def parsedJson = new JsonSlurper().parseText(jsonFile.text)
config = new Config()
config.jar = parsedJson.jar
def artifact = parsedJson.artifact
config.artifact = new Artifact()
config.artifact.groupId = artifact.groupId
config.artifact.artifactId = artifact.artifactId
config.artifact.version = artifact.version
def dependencies = parsedJson.dependencies
if (dependencies != null) {
config.dependencies = new ArrayList<>()
dependencies.each { d ->
def dependencyArtifact = new Artifact()
dependencyArtifact.groupId = d.groupId
dependencyArtifact.artifactId = d.artifactId
dependencyArtifact.version = d.version
config.dependencies.add(dependencyArtifact)
}
}
config.repositories = new ArrayList<>()
parsedJson.repositories.each { r ->
def repository = new Repository()
repository.id = r.id
repository.url = r.url
config.repositories.add(repository)
}
def registry = parsedJson.registry
config.registry = new Registry()
config.registry.url = registry.url
config.registry.deployToken = registry.deployToken
}
}
publish.dependsOn readConfig
publishing {
publications {
if (config != null) {
jar(MavenPublication) {
def artifactConfig = config.artifact
groupId = artifactConfig.groupId
artifactId = artifactConfig.artifactId
version = artifactConfig.version
artifact new File(config.jar)
pom.withXml {
def rootNode = asNode()
def dependencies = config.dependencies
if (dependencies != null) {
def dependenciesNode = rootNode.appendNode("dependencies")
dependencies.each { d ->
def dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", d.groupId)
dependencyNode.appendNode("artifactId", d.artifactId)
dependencyNode.appendNode("version", d.version)
}
}
def repositoriesNode = rootNode.appendNode("repositories")
config.repositories.each { r ->
def id = r.id
def url = r.url
def repositoryNode = repositoriesNode.appendNode("repository")
repositoryNode.appendNode("id", id)
repositoryNode.appendNode("url", url)
def distributionManagementNode = rootNode.appendNode("distributionManagement")
def distributionManagementRepositoryNode = distributionManagementNode.appendNode("repository")
distributionManagementRepositoryNode.appendNode("id", id)
distributionManagementRepositoryNode.appendNode("url", url)
def snapshotRepositoryNode = distributionManagementNode.appendNode("snapshotRepository")
snapshotRepositoryNode.appendNode("id", id)
snapshotRepositoryNode.appendNode("url", url)
}
}
}
}
}
repositories {
if (config != null) {
maven {
url config.registry.url
credentials(HttpHeaderCredentials) {
name = "Deploy-Token"
value = config.registry.deployToken
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
}
}
{
"jar": "",
"artifact": {
"groupId": "",
"artifactId": "",
"version": ""
},
"dependencies": [
{
"groupId": "",
"artifactId": "",
"version": ""
}
],
"repositories": [
{
"id": "gitlab-maven",
"url": ""
}
],
"registry": {
"url": "",
"deployToken": ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment