Skip to content

Instantly share code, notes, and snippets.

@reactivedroid
Last active October 18, 2023 09:27
Show Gist options
  • Save reactivedroid/bb4cf4bf41f304883667ad694194dbf2 to your computer and use it in GitHub Desktop.
Save reactivedroid/bb4cf4bf41f304883667ad694194dbf2 to your computer and use it in GitHub Desktop.
Publish via Github Packages
/** Add your github credentials in `github.properties` file
github.username= <Your github username>
github.token = <Your Github Personal Access Token with read/write package permission>
This will be used when you are publishing the package locally as a fallback
**/
def mavenPropertiesFile = rootProject.file("github.properties")
def githubProperties = new Properties()
githubProperties.load(new FileInputStream(mavenPropertiesFile))
publishing {
publications {
aar(MavenPublication) {
groupId = versions.group_id
version = <your library version name>
artifactId = project.getName()
// Tell maven to prepare the generated "*.aar" file for publishing
artifact("$buildDir/outputs/aar/${project.getName()}-${getVersionName()}.aar")
//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
// for dependencies and exclusions
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.withType(ModuleDependency) { ModuleDependency dp ->
// Ensure dependencies such as fileTree are not included in the pom.
if (dp.name != 'unspecified') {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dp.group)
dependencyNode.appendNode('artifactId', dp.name)
dependencyNode.appendNode('version', dp.version)
}
// for exclusions
if (dp.excludeRules.size() > 0) {
def exclusions = dependencyNode.appendNode('exclusions')
dp.excludeRules.each { ExcludeRule ex ->
def exclusion = exclusions.appendNode('exclusion')
exclusion.appendNode('groupId', ex.group)
exclusion.appendNode('artifactId', ex.module)
}
}
}
}
}
}
repositories {
maven {
name = "<GitHub Package Name>"
url = uri("https://maven.pkg.github.com/<Org name/username>/<repo_name>")
credentials {
username = System.getenv("git_user") ?: githubProperties['github.username']
password = System.getenv("git_token") ?: githubProperties['github.token']
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment