Skip to content

Instantly share code, notes, and snippets.

@hernandazevedo
Last active December 10, 2019 11:53
Show Gist options
  • Save hernandazevedo/835c156c35090d614849fa6df9959251 to your computer and use it in GitHub Desktop.
Save hernandazevedo/835c156c35090d614849fa6df9959251 to your computer and use it in GitHub Desktop.
/**
* Publish Local Helper
*
* @Author Robert Pösel
* @Version 1.4* @Date 24.4.2019
* https://gist.github.com/Robyer/a6578e60127418b380ca133a1291f017
*/
apply plugin: 'maven-publish'
ext {
localProp = new Properties()
fileName = 'local.properties'
if (project.rootProject.file(fileName).exists()) {
localProp.load(new FileInputStream(rootProject.file(fileName)))
}
groupName = "br.yourpackage.com"
publishUsername = System.env.PUBLISH_USERNAME != null ? System.env.PUBLISH_USERNAME : localProp["publishUsername"] ?: ""
publishPassword = System.env.PUBLISH_PASSWORD != null ? System.env.PUBLISH_PASSWORD : localProp["publishPassword"] ?: ""
destinationRepoUrl = System.env.PUBLISH_URL != null ? System.env.PUBLISH_URL : localProp["destinationRepoUrl"] ?: ""
}
project.afterEvaluate {
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
android.libraryVariants.all { variant ->
if (variant.name == 'release') {
if (variant.hasProperty('javaCompileProvider')) {
// Android 3.3.0+
owner.classpath += variant.javaCompileProvider.get().classpath
} else {
owner.classpath += variant.javaCompile.classpath
}
}
}
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
if (!project.ext.properties.containsKey("artifactId")) {
ext.artifactId = project.name
}
publishing {
repositories {
maven {
credentials {
username publishUsername
password publishPassword
}
url destinationRepoUrl
}
}
publications {
maven(MavenPublication) {
artifactId project.artifactId
groupId groupName
version "${project.getVersion()}"
artifact bundleReleaseAar
//TODO fix the generation of javadoc for kotlin or use dokka
// artifact androidJavadocsJar
artifact androidSourcesJar
pom.withXml {
final dependenciesNode = asNode().appendNode('dependencies')
ext.addDependency = { 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 not transitive, we should force exclude all its dependencies 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 exclusionsNode = dependencyNode.appendNode('exclusions')
dep.properties.excludeRules.each { rule ->
final exclusionNode = exclusionsNode.appendNode('exclusion')
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
// 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") }
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment