Skip to content

Instantly share code, notes, and snippets.

@informramiz
Created April 26, 2020 15:38
Show Gist options
  • Save informramiz/004463d83d08e60df2a2270c1865decb to your computer and use it in GitHub Desktop.
Save informramiz/004463d83d08e60df2a2270c1865decb to your computer and use it in GitHub Desktop.
Gradle file to publish library to maven repo using maven publish gradle plugin.
/**
* create following variables in your project level build.gradle file's extended block
* ext {
* lib_repo_username = "repo user name"
* lib_repo_password = "repo password"
* lib_repo_url = "lib repo url"
* lib_group_id = "group id"
* lib_artifact_id = "artifact id"
* lib_version_name = "version name"
* }
*
* then in your library level build.gradle file, write
*
* 1. apply plugin: 'maven-publish' (at the top along with other plugins)
* 2. apply from: 'link to this file/maven-publish.gradle' (after android block)
*/
task androidSourcesJar(type: Jar) {
archiveClassifier.set("sources")
from android.sourceSets.main.java.srcDirs
}
project.afterEvaluate {
publishing {
repositories {
maven {
credentials {
username = lib_repo_username
password = lib_repo_password
}
url lib_repo_url
}
}
publications {
maven(MavenPublication) {
groupId lib_group_id
artifactId lib_artifact_id
version lib_version_name
artifact bundleReleaseAar
artifact androidSourcesJar
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 // ignore invalid dependencies
final dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dep.group)
dependencyNode.appendNode('artifactId', dep.name)
dependencyNode.appendNode('version', dep.version)
dependencyNode.appendNode('scope', scope)
if (!dep.transitive) {
// If this dependency is transitive, we should force exclude all its dependencies them from the POM
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
// Otherwise add specified exclude rules
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
dep.properties.excludeRules.each { ExcludeRule rule ->
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
// List all "compile" dependencies (for old Gradle)
configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
// List all "api" dependencies (for new Gradle) as "compile" dependencies
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
// List all "implementation" dependencies (for new Gradle) as "runtime" dependencies
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
}
}
}
}
}
@hannesa2
Copy link

hannesa2 commented Sep 9, 2021

How does this looks like with Gradle 7.2 ?

configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }

With this line I run into

Could not find method addDependency() for arguments [DefaultProjectDependency{dependencyProject='project ':libs:gvr'', configuration='default'}, compile] on object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPom.

Here https://github.com/hannesa2/virocoreCommunity/pull/13/checks?check_run_id=3552520913

@BrainCrumbz
Copy link

Hello. Late try: did anyone overcome this error, by any chance? There's not a lot of info about this around. Thanks

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