Skip to content

Instantly share code, notes, and snippets.

@viktoriia-io
Last active November 7, 2023 08:47
Show Gist options
  • Select an option

  • Save viktoriia-io/674db15485bd66156a242f7d598ce12e to your computer and use it in GitHub Desktop.

Select an option

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']
}
@kot331107

kot331107 commented Oct 11, 2021

Copy link
Copy Markdown

@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

tareq3 commented Jun 30, 2022

Copy link
Copy Markdown

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?

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