Skip to content

Instantly share code, notes, and snippets.

@Shvet
Forked from viktoriia-io/maven_publish.gradle
Created January 5, 2022 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shvet/fe7183b91dc71d53cbea5ad8bfcea506 to your computer and use it in GitHub Desktop.
Save Shvet/fe7183b91dc71d53cbea5ad8bfcea506 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']
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment