// pom文件中声明依赖,从而传递到使用方
pom.withXml { a ->
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
// 避免出现空节点或 artifactId=unspecified 的节点
if (it.group != null && (it.name != null && "unspecified" != it.name) && it.version != null) {
println "dependency=${it.toString()}"
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', 'implementation')
完整代码示例:
apply plugin: 'maven-publish'
task generateSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "sources"
publishing {
//maven配置参数
repositories {
maven {
url = MAVEN_URL
credentials {
username MAVEN_USER_NAME
password MAVEN_PWD
publications {
//名字可以随意,如果有多渠道,整段再下面多复制一个
Publish(MavenPublication) {
groupId = GROUP_ID//公司域名
//def projectName = project.getName()
artifactId = "hmi"//该aar包的名称
version = VERSION//版本号
// 必须有这个 否则不会上传AAR包
afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }
// 必须是多渠道,需要指定aar路径
//afterEvaluate { artifact("build/outputs/aar/app-debug.aar") }
// 上传source,这样使用方可以看到方法注释
artifact generateSourcesJar
// pom文件中声明依赖,从而传递到使用方
pom.withXml { a ->
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
// 避免出现空节点或 artifactId=unspecified 的节点
if (it.group != null && (it.name != null && "unspecified" != it.name) && it.version != null) {
println "空节点导致自定义gropid失效"
println "dependency=${it.toString()}"
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', 'implementation')
前后pom对比
未做依赖传递前
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven.sample</groupId>
<artifactId>hmi</artifactId>
<version>0.0.005</version>
<packaging>aar</packaging>
</project>
做依赖传递后
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven.sample</groupId>
<artifactId>hmi</artifactId>
<version>0.0.006</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>androidx.core</groupId>
<artifactId>core-ktx</artifactId>
<version>1.8.0</version>
<scope>implementation</scope>
</dependency>
<dependency>
<groupId>androidx.appcompat</groupId>
<artifactId>appcompat</artifactId>
<version>1.3.0</version>
<scope>implementation</scope>
</dependency>
<dependency>
<groupId>com.google.android.material</groupId>
<artifactId>material</artifactId>
<version>1.6.1</version>
<scope>implementation</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.6.21</version>
<scope>implementation</scope>
</dependency>
</dependencies>
</project>
附另外一种方式
withXml {
def dependenciesNode = asNode().appendNode('dependencies')
project.configurations.all { configuration ->
def name = configuration.name
if (name != "implementation" && name != "compile" && name != "api") {
return
println(configuration)
configuration.dependencies.each {
println(it)
if (it.name == "unspecified" || it.version == 'unspecified') {
// 忽略无法识别的
return
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (name == "api" || name == "compile") {
dependencyNode.appendNode("scope", "compile")
} else { // implementation
dependencyNode.appendNode("scope", "runtime")
在此做个笔记
在上一篇文章里,已经介绍了如果搭建maven私有仓库以及各种仓库的用途。
上传aar到maven私有仓库,有两种方式,一种是直接使用nexus3提供的上传功能,将aar上传到仓库。另一种是使用gradle脚本上传。
使用nexus3上传aar
先登录到nexus,点击Upload,选择一个仓库,如上图。
点击Browse会弹出选择文件窗口,选择要上传的aar后,填写其它信息。
Extension是文件拓展名。
主要是填写Group ID、Artifact ID、Version。
这三个参数是maven对组
publications {
aarSDk(MavenPublication) {
artifact "build/outputs/aar/adsdk-debug.aar"