发布aar到Maven仓库

本文介绍了如何配置Gradle环境变量,并通过`maven-publish`插件构建AndroidAAR文件,将它发布到Nexus仓库。详细步骤包括创建`.bash_gradle`文件,设置系统环境变量,构建文件的配置,如定义版本号和POM依赖,最后在AndroidStudio终端执行脚本`./build.sh`来完成打包和发布操作。

一. 配置gradle环境变量

1.创建.bash_gradle文件

在这里插入图片描述

2. 执行source ~/.bash_gradle, 然后执行命令 gradle --version,如果配置成功则会输出:

在这里插入图片描述

二.构建文件配置

apply plugin: 'maven-publish'

def buildNo = System.env.BUILD_NUMBER ? System.env.BUILD_NUMBER : 0
def ver = new Date().format('yy.MM.dd') + '.' + buildNo
publishing {
    repositories {
        maven {
            url 'http://nexus.xxxx.product.com/repository/app'

            credentials {
                username 'xxxx'
                password 'xxxx'
            }
        }
    }

    publications {
        maven(MavenPublication) {
            // 上传到maven使用debug包
            artifact "xxxx-debug.aar"
            groupId 'com.xxx.sdk'
            artifactId project.name
            version ver

            pom.withXml {
                // 添加POM依赖, 需要跟dependencies保持一致
                def dependenciesNode = asNode().appendNode('dependencies')
                def dependencyNode

                dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', 'com.google.android.material')
                dependencyNode.appendNode('artifactId', 'material')
                dependencyNode.appendNode('version', '1.0.0')
            }
        }
    }
}

三. 编写脚本代码

在这里插入图片描述

四.执行脚本

在AndroidStudio终端执行./build.sh即可完成编译打包及发布aar到maven仓库的操作。

### 如何将 AAR 文件发布Maven 仓库 发布 AAR 文件到 Maven 仓库通常需要使用 Gradle 插件,如 `maven-publish`,以确保生成的 `.pom` 文件包含完整的依赖信息,从而避免在使用 AAR 时出现编译错误。以下是一个详细的配置步骤。 #### 配置 `build.gradle` 文件 在项目的 `build.gradle` 文件中添加 `maven-publish` 插件,并配置发布任务。以下是一个基本的配置示例: ```groovy plugins { id 'com.android.library' id 'org.gradle.maven-publish' } android { namespace 'com.example.mylibrary' compileSdk 33 defaultConfig { minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.8.0' } publishing { publications { maven(MavenPublication) { groupId = 'com.example' artifactId = 'mylibrary' version = '1.0' artifact("$buildDir/outputs/aar/mylibrary-release.aar") // 生成包含依赖信息的 .pom 文件 pom.withXml { def dependenciesNode = asNode().appendNode('dependencies') configurations.compile.allDependencies.each { if (it instanceof ExternalDependency) { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) } } } } } repositories { maven { // 本地 Maven 仓库路径 url = uri("${project.buildDir}/repo") } } } ``` #### 发布 AAR 文件 配置完成后,可以使用以下命令将 AAR 文件发布到指定的 Maven 仓库: ```bash ./gradlew publish ``` 该命令会生成 AAR 文件并将其发布到 `build/repo` 目录下的本地 Maven 仓库中。 #### 使用 Docker 搭建 Maven 私服 如果需要将 AAR 文件发布到私有 Maven 仓库,可以使用 Docker 搭建 Nexus 私服。以下是一个简单的步骤: 1. **启动 Nexus 容器**: ```bash docker run -d -p 8081:8081 --name nexus sonatype/nexus3 ``` 2. **配置 Maven 仓库**: 登录到 Nexus 管理界面,创建一个新的 Maven 仓库。 3. **修改 `build.gradle` 文件**: 将发布目标指向 Nexus 仓库: ```groovy publishing { repositories { maven { url = "http://localhost:8081/repository/maven-releases/" credentials { username = 'admin' password = 'admin123' } } } } ``` 4. **发布 AAR 文件**: 使用以下命令将 AAR 文件发布到 Nexus 仓库: ```bash ./gradlew publish ``` #### 常见问题解决 如果在发布过程中遇到以下错误: ``` Failed to publish publication 'maven' to repository 'maven3' > Using insecure protocols with repositories, without explicit opt-in, is unsupported. ``` 可以在 `build.gradle` 文件中添加以下配置以允许使用不安全协议: ```groovy repositories { maven { url = "http://localhost:8082/repository/repos0/" allowInsecureProtocol = true } } ``` 这样可以避免因使用 HTTP 协议而导致的发布失败问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值