使用gradle构建发布脚本,即可快速方便地发布文件到maven仓库。有2种插件可以选择,一种maven,一种maven-publish。
maven插件内置了一个方法uploadArchives,这个方法会将当前项目编译打包并发布:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
// repository(url: "http://repo.apcos.com/repository/apcos-snapshot/") {
// authentication(userName: "xxxx", password: "xxxx")
// }
repository(url: "file://localhost/F:/Studying/repo/")
pom.project {
groupId = "com.xter.support"
artifactId = "support"
version = "0.7"
packaging 'aar'
developers {
developer {
id 'xter'
name 'Liang'
timezone 'Asia/Shanghai'
}
}
description 'Some Utils of XTER'
}
}
}
}
按照官方文档描述:
To add additional content to the POM, the pom.project builder can be used. With this builder, any
element listed in the Maven POM reference can be added.
pom.project节点中可以添加任何符合Maven POM规范的字段。
如果想将文档注释与源码一起发布的话,可以同级添加artifacts 节点说明需要发布的文档:
task javadoc(type: Javadoc) {
failOnError = false
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
options.addStringOption('encoding','UTF-8')
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
task sourceJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives sourceJar
archives javadocJar
}
上述gradle任务可在IDE中gradle工具栏中找到:
双击执行后,即可在相应位置找到发布的文件了。
这里是发布到F盘目录下了,所以去F盘对应目录下即可找到:
在support-0.7.pom中即可看到一些发布的具体描述信息:
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0modelVersion>
<groupId>com.xter.supportgroupId>
<artifactId>supportartifactId>
<version>0.7version>
<packaging>aarpackaging>
<description>Some Utils of XTERdescription>
<developers>
<developer>
<id>xterid>
<name>Liangname>
<timezone>Asia/Shanghaitimezone>
developer>
developers>
<dependencies>
<dependency>
<groupId>com.android.supportgroupId>
<artifactId>appcompat-v7artifactId>
<version>26.1.0version>
<scope>compilescope>
dependency>
<dependency>
<groupId>com.android.supportgroupId>
<artifactId>designartifactId>
<version>26.1.0version>
<scope>compilescope>
dependency>
dependencies>
project>
maven-publish插件同样内置一个方法,名为publishing,通过配置此方法,可以发布文件:
apply plugin: 'maven-publish'
publishing {
publications {
support(MavenPublication) {
artifact sourceJar
artifact javadocJar
artifact 'build/outputs/aar/support-release.aar'
groupId 'com.xter.support'
artifactId 'support'
version '0.7'
pom.withXml {
asNode().appendNode('description', 'Some Utils of XTER')
def developer = asNode().appendNode('developers').appendNode('developer')
developer.appendNode('id','xter')
developer.appendNode('name','Liang')
developer.appendNode('timezone','Asia/Shanghai')
}
}
}
repositories {
maven {
// credentials {
// username "xxxxx"
// password "xxxxx"
// }
// url "http://repo.apcos.com/repository/apcos-snapshot/"
url "file://F:/Studying/repo/"
}
}
}
这里是上传了本地aar文件及其文档,效果和上一种方法等同。只不过由于这里用的gradle版本较低,只能使用pom.withXml来添加pom更多信息,还是有所不便,根据官方例子是同样可以依闭包形式快速添加节点的。
此时官方gradle最新版本为6.0.1。
完成后同样是执行对应gradle任务即可完成发布。(这里截的另外的项目图,正确的gradle任务应该叫publishSupportPublicationToMavenRepository)
最后远程依赖不必多说,先在根节点的build.gradle中添加对应仓库:
allprojects {
repositories {
// maven { url 'http://repo.apcos.com/repository/apcos-snapshot/'}
maven { url 'file://F:/Studying/repo/'}
}
}
然后在需要的module引用:
dependencies {
implementation 'com.xter.support:support:0.7'
}
至此就可以正常使用了。