找了很久,没有找到满意的。终于找到一个好的办法。
gradle7.x适用。比以前的写法简洁。
发布传统的jar工程
比如okhttp,fastjson等项目,纯java工程。
- 直接创建新文件publish.gradle:
groovy
apply plugin: 'maven-publish'
Properties properties = new Properties()
try {
InputStream inputStream = file('../local.properties').newDataInputStream()
properties.load(inputStream)
} catch (Throwable ignored) {
ignored.printStackTrace()
}
def moduleGroupId = findProperty('group.id')
def mavenName = findProperty('maven.name')
def mavenUploadUrl = findProperty('maven.upload.url')
def mavenUploadUsername = properties.getProperty('username')
def mavenUploadPassword = properties.getProperty('password')
def moduleArtifactId = "okhttp"
def moduleVersion = "4.12.0" //记得每次修改
publishing {
publications {
create(moduleArtifactId, MavenPublication) {
from components.java
groupId "${moduleGroupId}" //看备注
artifactId "${moduleArtifactId}"
version "${moduleVersion}"
}
}
repositories {
maven {
name = mavenName
url = mavenUploadUrl
credentials {
username = mavenUploadUsername
password = mavenUploadPassword
}
}
}
}
然后在jar生成工程的build.gradle里面添加:
groovy
//补充打包条件
java {
withJavadocJar()
withSourcesJar()
}
apply from: "publish.gradle" //引入即可
gradle sync以后,能够得到如下:
点击运行发布。
备注:
其中,groupId "${moduleGroupId}"
是为了公司统一group包名。一些自行新建的仓库如此即可。
但,如果你是想修改某个外部库的源码,传到了公司内部,然后引入使用。最好的办法是注释掉groupId,修改源码,升级版本号即可。这样它会从多个仓库url去取,自然会从你们公司地址取到版本。
注释掉才能保证引入的时候:implement 'com.squareup.okhttp3:okhttp:4.12.0'
这样才不会导致多包问题。
发布aar
android 的aar发布差不多:
脚本文件publish.gradle:
groovy
apply plugin: 'maven-publish'
//读取账号和签名信息文件
Properties properties = new Properties()
try {
InputStream inputStream = file('../local.properties').newDataInputStream()
properties.load(inputStream)
} catch (Throwable ignored) {
ignored.printStackTrace()
}
def moduleGroupId = findProperty('group.id')
def mavenName = findProperty('maven.name')
def mavenUploadUrl = findProperty('maven.upload.url')
def mavenUploadUsername = properties.getProperty('username')
def mavenUploadPassword = properties.getProperty('password')
def moduleArtifactId = name
android {
publishing {
singleVariant("release") {
withSourcesJar()
withJavadocJar()
}
}
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = moduleGroupId
artifactId = moduleArtifactId
version = moduleVersion
}
}
repositories {
maven {
name = mavenName
url = mavenUploadUrl
credentials {
username = mavenUploadUsername
password = mavenUploadPassword
}
}
}
}
}
引入则:
apply from: ".../gradle/publish.gradle"