Skip to content

Instantly share code, notes, and snippets.

@viktoriia-io
Last active November 7, 2023 08:47
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save viktoriia-io/674db15485bd66156a242f7d598ce12e to your computer and use it in GitHub Desktop.
Save viktoriia-io/674db15485bd66156a242f7d598ce12e to your computer and use it in GitHub Desktop.
Maven publishing script for customising pom file generation
apply plugin: 'maven-publish'
afterEvaluate {
publishing {
publications {
maven(MavenPublication) {
groupId project.ext.pomGroupID
artifactId project.name
version project.ext.pomVersion
artifact(bundleReleaseAar)
pom.withXml {
final dependenciesNode = asNode().appendNode('dependencies')
ext.addDependency = { Dependency dep, String scope ->
if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
return // invalid dependencies should be ignored
final dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('artifactId', dep.name)
if (dep.version == 'unspecified') {
dependencyNode.appendNode('groupId', project.ext.pomGroupID)
dependencyNode.appendNode('version', project.ext.pomVersion)
System.println("${project.ext.pomGroupID} ${dep.name} ${project.ext.pomVersion}")
} else {
dependencyNode.appendNode('groupId', dep.group)
dependencyNode.appendNode('version', dep.version)
System.println("${dep.group} ${dep.name} ${dep.version}")
}
dependencyNode.appendNode('scope', scope)
// Some dependencies may have types, such as aar, that should be mentioned in the POM file
def artifactsList = dep.properties['artifacts']
if (artifactsList != null && artifactsList.size() > 0) {
final artifact = artifactsList[0]
dependencyNode.appendNode('type', artifact.getType())
}
if (!dep.transitive) {
// In case of non transitive dependency, all its dependencies should be force excluded from them POM file
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
// For transitive with exclusions, all exclude rules should be added to the POM file
final exclusions = dependencyNode.appendNode('exclusions')
dep.properties.excludeRules.each { ExcludeRule rule ->
final exclusionNode = exclusions.appendNode('exclusion')
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
}
}
}
repositories {
maven {
url 'your_maven_url'
credentials {
username your_user_name
password your_password
}
}
}
}
}
task cleanBuildPublishLocal(type: GradleBuild) {
tasks = ['clean', 'build', 'publishToMavenLocal']
}
task cleanBuildPublish(type: GradleBuild) {
tasks = ['clean', 'build', 'publish']
}
@Pjumpod
Copy link

Pjumpod commented Aug 30, 2021

Where I need to put this code in gradle?
I have to put them in the Project Gradle or new file?

@kot331107
Copy link

kot331107 commented Oct 11, 2021

@Pjumpod somewhere into a separate file on the same level as your module's gradle script. You can use the name maven-publish.gradle for it as author did. Then don't forget to link it in your module's gradle with apply from: 'maven-publish.gradle'

@tareq3
Copy link

tareq3 commented Jun 30, 2022

Thanks for the GIST. One of my dependencies(for example: X) needs a custom repository to be declared. Can I add a custom repository into the generated pom.xml? So that users don't have to add X's repository into their project?

@VISAL-NY
Copy link

VISAL-NY commented Nov 7, 2023

i use as you but just modify
repositories {
maven {
url 'https://repo.maven.apache.org/maven2/'
}
}

but when i publish got problem Failed to publish publication 'maven' to repository 'maven'

Could not PUT 'https://repo.maven.apache.org/maven2/com/bill24/24paymentsdk/24paymentsdk/1.0.0/24paymentsdk-1.0.0.aar'. Received status code 503 from server: client read error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment