Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Last active August 16, 2018 15:19
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 Kolyall/497c07d7c71339b4372627b6f851e40c to your computer and use it in GitHub Desktop.
Save Kolyall/497c07d7c71339b4372627b6f851e40c to your computer and use it in GitHub Desktop.
Maven Publish
ext {
def propertiesFile = file('properties/version/version.properties')
appVersionCode = 1;
appVersionName = "1.0";
if (propertiesFile.canRead()) {
Properties properties = new Properties()
properties.load(new FileInputStream(propertiesFile))
appVersionCode = Integer.parseInt(properties['VERSION_CODE']).intValue();
appVersionName = String.valueOf(properties['VERSION_NAME']);
} else {
throw new Exception("Could not read properties/version/version.properties!")
}
}
if (project.hasProperty("android")) { // Android libraries
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
task javadoc(type: Javadoc) {
// all the sources of the current module
source = android.sourceSets.main.java.srcDirs
// the Android SDK classpath
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
// all the dependencies classpaths
classpath += configurations.compile
classpath += files("build/generated/source/r/release")
classpath += files("build/generated/source/buildConfig/release")
android.libraryVariants.all { variant ->
if (variant.name == 'release') {
owner.classpath += variant.javaCompile.classpath
}
}
// Honestly I do not remember why it's a good idea to exclude these
exclude '**/R.html', '**/R.*.html', '**/index.html', '**/R$*.class', '**/R.java', '**/BuildConfig.java', '**/R.class', '**/BuildConfig.class'
options {
encoding "utf-8"
// Java reference
links "http://docs.oracle.com/javase/7/docs/api/"
links("http://docs.oracle.com/javase/8/docs/api/");
// dependencies API references (I should probably move these in the project or something)
links("http://reactivex.io/RxJava/javadoc/");
links("https://google.github.io/gson/apidocs/");
// Android reference is not standard javadoc so I need to use offline directory
linksOffline("http://d.android.com/reference/", "${android.sdkDirectory}/docs/reference")
// Java 8 javadoc is more strict, This disable that strictness
if (JavaVersion.current().isJava8Compatible()) {
addStringOption('Xdoclint:none', '-quiet')
addStringOption('Xwerror', '-quiet')
}
addStringOption('encoding', 'UTF-8')
addStringOption('charSet', 'UTF-8')
}
}
} else { // Java libraries
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
//added because of gradlew install is fall with error: cannot find symbol annotations
afterEvaluate {
javadoc.classpath += files(android.libraryVariants.collect { variant ->
variant.javaCompiler.classpath.files
})
}
apply plugin: 'com.jfrog.bintray'
version = libraryVersion
// Bintray
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = bintrayRepo
name = bintrayName
desc = libraryDescription
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = allLicenses
publish = true
publicDownloadNumbers = true
version {
desc = libraryDescription
gpg {
sign = true //Determines whether to GPG sign the files. The default is false
passphrase = properties.getProperty("bintray.gpg.password")
//Optional. The passphrase for GPG signing'
}
}
}
}
To check All tasks in gradle use the commend , or open tab `gradle` at right of AndroidStudio
> gradlew tasks
To automatically added to mavenLocal() c:\Users\User\.m2\
apply plugin: 'maven-publish'
> gradlew clean
> gradlew install
To publish on custom repository
> gradlew clean
> gradlew install
> gradlew publishAndroidLibraryPublicationToMavenBuildDirRepository (or publish{taskName}PublicationTo{customMavenName}Repository)
To publish on bintray.com
> gradlew clean
> gradlew install
> gradlew bintrayUpload
To check dependencies in lib:
> gradlew -q app:dependencies > app_dependencies.txt
https://stackoverflow.com/questions/26975818/what-is-scope-under-dependency-in-pom-xml-for
apply plugin: 'com.github.dcendents.android-maven'
group = publishedGroupId // Maven Group ID for the artifact
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
groupId publishedGroupId
artifactId artifact
// Add your description here
name libraryName
description libraryDescription
url siteUrl
// Set your license
licenses {
license {
name licenseName
url licenseUrl
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
apply plugin: 'com.android.library'
apply from: 'appVersion.gradle'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode appVersionCode
versionName appVersionName
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
////added because of gradlew install is fall with error: cannot find symbol annotations
configurations {
javadocDeps
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
//added because of gradlew install is fall with error: cannot find symbol annotations
javadocDeps 'com.android.support:support-annotations:27.1.1'
javadocDeps 'com.android.support:appcompat-v7:27.1.1'
javadocDeps 'com.squareup.okhttp3:okhttp:3.9.1'
}
// Place it at the end of the file
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
}
}
apply from: 'libraryInfo.gradle'
apply from: 'artifactsSetup.gradle'
apply from: 'installSetup.gradle'
apply from: 'bintraySetup.gradle'
apply from: 'mavenCustom.gradle'
ext {
def propertiesFile = file('properties/version/version.properties')
def appVersionName = "1.0";
if (propertiesFile.canRead()) {
Properties properties = new Properties()
properties.load(new FileInputStream(propertiesFile))
appVersionName = String.valueOf(properties['VERSION_NAME']);
} else {
throw new Exception("Could not read properties/version/version.properties!")
}
bintrayRepo = 'maven'//name of Repo on https://bintray.com/USERNAME
bintrayName = 'MODULE_NAME'//name of project on https://bintray.com/USERNAME/maven
publishedGroupId = 'com.PUBLISHED_GROUP_ID'//only this
artifact = 'MODULE_NAME'//should be the same as module name
artifactLocal = 'MODULE_NAME'//should be the same as module name
libraryVersion = appVersionName
libraryName = 'MODULE_NAME'
libraryDescription = 'A wrapper for rx-android-utils on Android'
siteUrl = 'https://github.com/GITUSERNAME/PROJECT_NAME'
gitUrl = 'https://github.com/GITUSERNAME/PROJECT_NAME.git'
developerId = 'GITUSERNAME'
developerName = 'GITUSERNAME'
developerEmail = 'USERNAME@gmail.com'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=D\:\\Android\\sdk
bintray.user={bintray_user}
bintray.apikey={bintray_apikey}
bintray.gpg.password={bintray_password}
apply plugin: 'maven-publish'
apply plugin: 'signing'
publishing {
publications {
androidLibrary(MavenPublication) {
groupId publishedGroupId
artifactId artifactLocal
version libraryVersion
artifact bundleRelease
artifact sourcesJar
artifact javadocJar
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.getAllDependencies().each { dep -> addDependency(dep, "compile") }
// List all "api" dependencies (for new Gradle) as "compile" dependencies
configurations.api.getAllDependencies().each { dep -> addDependency(dep, "compile") }
// // List all "implementation" dependencies (for new Gradle) as "runtime" dependencies
// configurations.implementation.getAllDependencies().each { dep -> addDependency(dep, "runtime") }
}
}
}
repositories {
maven {
// change URLs to point to your repos, e.g. http://my.org/repo
def mavenCustom = uri("D:/modules/maven/repository")
url = mavenCustom
name = 'mavenCustom'
}
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
}
}
allprojects {
repositories {
mavenLocal()
maven {
// change URLs to point to your repos, e.g. http://my.org/repo
def mavenCustom = uri("D:/modules/maven/repository")
url = mavenCustom
name = 'mavenCustom'
}
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
#Tue Sep 05 14:02:16 EEST 2017
VERSION_NAME=1.0.0
VERSION_CODE=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment