1.在项目根目录下新建uploadConfig.properties配置文件:
USERNAME=username
PASSWORD=password
GROUP_ID=com.test.library
MAVEN_REPO_SNAPSHOT_URL=http://192.168.86.130:8081/repository/{snapshot仓库名}/
MAVEN_REPO_RELEASE_URL=http://192.168.86.130:8081/repository/{release仓库名}/
TYPE=aar
在项目根目录的build.gradle文件中加载配置文件:
def parseLocalProperties(){
File file = rootProject.file('uploadConfig.properties')
if(file.exists()){
InputStream inputStream = rootProject.file('uploadConfig.properties').newDataInputStream();
Properties properties = new Properties()
properties.load(inputStream)
if(properties.containsKey("MAVEN_REPO_SNAPSHOT_URL")){
ext.MAVEN_REPO_SNAPSHOT_URL = properties.getProperty("MAVEN_REPO_SNAPSHOT_URL")
ext.MAVEN_REPO_RELEASE_URL = properties.getProperty("MAVEN_REPO_RELEASE_URL")
ext.GROUP_ID = properties.getProperty("GROUP_ID")
ext.TYPE = properties.getProperty("TYPE")
ext.USERNAME = properties.getProperty("USERNAME")
ext.PASSWORD = properties.getProperty("PASSWORD")
}
}
}
allprojects {
...
parseLocalProperties()
}
2. 在要上传maven的module下面新建上传脚本:
apply plugin: 'maven'
apply plugin: 'signing'
def VERSION="1.0.0"
def ARTIFACT_ID="test"
configurations {
deployerJars
}
repositories {
mavenCentral()
}
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment {
MavenDeployment deployment -> signing.signPom(deployment)
}
pom.version = VERSION
pom.artifactId = ARTIFACT_ID
pom.groupId = project['GROUP_ID']
repository(url: project['MAVEN_REPO_RELEASE_URL']) {
// maven授权信息
authentication(userName: project['USERNAME'], password: project['PASSWORD'])
}
snapshotRepository(url: project['MAVEN_REPO_SNAPSHOT_URL']) {
authentication(userName: project['USERNAME'], password: project['PASSWORD'])
}
}
}
}
// 生成sources.jar
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
// 产生相关配置文件的任务
artifacts {
archives androidSourcesJar
}
// 进行数字签名
signing {
required { gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
3.在要上传maven的module下面的build.gradle中应用脚本:
apply from: 'maven_push.gradle'
4.刷新同步一下gradle,右边的gradle任务当中多了一个upload任务:
双击运行或者右键run即可。
上传成功后就可以使用了:
implementation "com.test.library:test:1.0.0"
记得把仓库地址配置到repositories当中。
上传过程中还有个问题就是,如果你要上传的module还依赖了本地其它module,那么需要把本地依赖改成远程依赖后再上传:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//implementation project(':libraryProjectA')
implementation "com.test.lib:test:1.0.0"
}
把implementation project方式引用的统统都要换成远程依赖的方式,不然最终使用的时候会报错,找不到你本地依赖的库。这也就意味着你可能要先上传本地依赖的那个库,拿到maven依赖后再上传依赖它的库。
另外就是在上传的时候,如果你是有多个模块要传,注意当前传哪个module就在哪个module中的gradle应用上传脚本,上传完了把apply那句注释掉,其它的不用上传的module都注释掉这句,否则当你执行upload任务时所有的模块都会给你传一遍。