默认maven的服务器仓库已经有了,如果没有请自行创建,下面使用的是使用Nexus搭建的Maven本地仓库
1,在要上传的lib module的build.gradle里添加如下:
apply from: file('release.gradle')
2,同目录下创建release.gradle文件,内容如下:
apply plugin: 'maven'
def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}
def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = GROUP
pom.artifactId = POM_ARTIFACT_ID
pom.version = VERSION_NAME
//正式的输出版本用,版本号相同不会重复上传
repository(url: RELEASE_REPOSITORY_URL) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
//下面这个可以注释掉,阶段性开发用,可以重复上传
snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
}
}
}
}
//注释掉下面后,生成的jar/aar文件木有注释。
// type显示指定任务类型或任务, 这里指定要执行Javadoc这个task,这个task在gradle中已经定义
//task androidJavadocs(type: Javadoc) {
// // 设置源码所在的位置
// source = android.sourceSets.main.java.sourceFiles
//}
//
生成javadoc.jar
//task androidJavadocsJar(type: Jar) {
// // 指定文档名称,比如:singlerecord-2.1.7.1-javadoc.jar
// classifier = 'javadoc'
// from androidJavadocs.destinationDir
//}
//
生成sources.jar
//task androidSourcesJar(type: Jar) {
// // 指定文档名称,比如:singlerecord-2.1.7.1-sources.jar
// classifier = 'sources'
// from android.sourceSets.main.java.sourceFiles
//}
//有中文注释时使用
//androidJavadocs {
// options{
// encoding "UTF-8"
// charSet 'UTF-8'
// }
//}
产生相关配置文件的任务
//artifacts {
// archives androidSourcesJar
// archives androidJavadocsJar
//}
//**********上面显示注释不好用的话,用下面的***********//
task androidJavadocs(type: Javadoc) {
failOnError=false
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
//aar包增加源码
archives androidSourcesJar
//aar包增加注释
archives androidJavadocsJar
}
3,release.gradle中的属性值需要在gradle.properties(如果lib module里没有,需要自已创建)里定义:
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# replace with your values
GROUP=com.gome.singlerecord
VERSION_NAME=2.1.7.2
POM_ARTIFACT_ID=singlerecord
SNAPSHOT_REPOSITORY_URL=http://XXX.XX.XX.XXX:XXXX/nexus/content/repositories/snapshots/
RELEASE_REPOSITORY_URL=http://IP:8081/nexus/content/repositories/releases/
NEXUS_USERNAME=XXXX
NEXUS_PASSWORD=XXXXXX
4,脚本写完了,接下来是执行上传library到maven的操作了,如下图:
点击android studio右侧Gradle -->点击弹框中的uploadArchives方法完成上传。
5,接下来就是使用了:
//project build.gradle中添加
allprojects {
repositories {
maven {
//gradle.properties里的RELEASE_REPOSITORY_URL maven仓库地址
url 'http://IP:8081/nexus/content/groups/public/'
}
}
}
//使用的app或者其它module使用时添加:
dependencies {
implementation com.gome.singlerecord:singlerecord:2.1.7.2'
}
鸡汤下:
养成些好习惯,让时光留下痕迹;不急不躁,慢慢沉淀。