Android Gradle 7.0 使用maven-publish发布组件

环境

Android Studio Arctic Fox | 2020.3.1 Patch 2
Build #AI-203.7717.56.2031.7678000, built on August 27, 2021
Runtime version: 11.0.10+0-b96-7249189 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.4.0-89-generic

问题1:http协议的仓库不安全

Gradle 7.0默认强制使用https的仓库地址,直接使用的话,会报错如下:

Could not resolve all dependencies for configuration ‘:classpath’.
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository ‘maven(http://...:8081/repository/maven-public/)’ to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.0.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details.

解决1:设置allowInsecureProtocol

通过设置 allowInsecureProtocol 为 true,来支持http的仓库地址。详情见官方说明:UrlArtifactRepository:allowInsecureProtocol

buildscript {
    repositories {
        maven {
            allowInsecureProtocol true
            url 'http://xxx.xx.xx.xx:8081/repository/maven-public/'
        }
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

问题2:私有仓库组件依赖不成功

远程进行依赖时,旧版本可以依赖成功的一个组件,报错:

Failed to resolve: com.xx.xx:utils:1.0.0

1,检查project 的 build.gradle,明明已经设置好了maven地址引用。

buildscript {
    repositories {
        maven {
            allowInsecureProtocol true
            url 'http://xxx.xx.xx.xx:8081/repository/maven-public/'
            credentials {
                username = NEXUS_USERNAME
                password = NEXUS_PASSWORD
            }
        }
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

发现个问题,旧版本一般都有的allprojects {},不见了!!!,莫非是换了人间,经查询果然是!!!

2,找到“新人间”,根目录的settings.gradle

修改前

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}
rootProject.name = "xxx"
include ':demo'
include ':Library'

修改后

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven {
            allowInsecureProtocol true
            url 'http://xxx.xxx.xxx.xxx:8081/repository/maven-public/'
        }
        google()
        mavenCentral()
    }
}
rootProject.name = "xxx"
include ':demo'
include ':Library'

问题3:apply plugin: ‘maven’ 不再支持,原先的上传脚本废了。

原来的脚本

//打包发布

apply plugin: 'maven'

//打包main目录下代码和资源的 task
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

uploadArchives {
    repositories.mavenDeployer {
        name = 'mavenCentralReleaseDeployer'
        repository(url: MAVEN_URL) {
            authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
        }
        snapshotRepository(url: MAVEN_SNAPSHOT_URL) {
            authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
        }
        pom.version = "1.1.2-SNAPSHOT"
        pom.artifactId = "xxx"
        pom.groupId = GROUP_ID
        pom.name = "xxx"
        pom.packaging = 'aar'

    }
}

artifacts {
    archives androidSourcesJar
}

新的Publish脚本,确实费了一番周折,参考了android官网 : Use the Maven Publish plugin 和 gradle官网 :Example 8. Publishing a Java library

//打包发布

apply plugin: 'maven-publish'

//打包main目录下代码和资源的 task
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant. 
                from components.release

                // You can then customize attributes of the publication as shown below.
                groupId = GROUP_ID
                artifactId = 'xxxx'
                version = '1.0.0'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.debug

                groupId = GROUP_ID
                artifactId = 'xxx'
                version = '1.0.0-SNAPSHOT'
            }
        }
        repositories {
            maven {
                name = "nexus"
                allowInsecureProtocol true
                url MAVEN_GROUP_URL
                //nexus3没有开启匿名用户访问的话,添加认证信息
                credentials {
                    username = NEXUS_USERNAME
                    password = NEXUS_PASSWORD
                }
                def releasesRepoUrl = MAVEN_URL
                def snapshotsRepoUrl = MAVEN_SNAPSHOT_URL
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

            }
        }
    }
}

至此,可以正常使用了。

呜谢

Maven Publish Plugin

publishing_maven

MavenArtifactRepository

maven-publish插件的使用

Android使用自己封装的maven-publish插件(maven 和 maven-publish),发布到自己或者公司的私有仓库流程

Android Studio Arctic Fox | 2020.3.1、Gradle 7.0升级记录

Android Studio Arctic Fox gradle 配置

Gradle 7.X Maven publish 适配

gradle7.0之后,android库发布至MavenCentral流程变化。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
m-publish是一个Gradle插件,用于将本地library发布到Apache Maven仓库。通过使用该插件,我们可以将*.aar、*.jar等library发布到仓库中,并通过gradle或者maven进行远程依赖使用。 要使用maven-publish插件,需要在build.gradle中声明插件,并在publishing{}块中进行配置。首先,通过在plugins{}块中添加id 'maven-publish'来声明插件。然后,在publishing{}块中,可以配置group和version属性,以及定义要发布的publication和repository。 例如,通过components.java来指定要发布的library,使用mavenLocal()来指定要发布到的仓库。可以根据需要添加更多的publication和repository配置。 maven-publish插件提供了一些任务,如generatePomFileForPubNamePublication用于创建需要发布的POM文件,并填充一些已知的元数据,例如项目名称、项目版本和依赖项。publishPubNamePublicationToRepoNameRepository用于将指定publication发布到指定repository。publishPubNamePublicationToMavenLocal用于将指定publication发布复制到本地Maven缓存,包括POM文件和其他元数据。 此外,还有一些其他任务,如publish将所有定义的publication发布到所有定义的存储库的聚合任务,而publishToMavenLocal将所有定义的publication复制到本地Maven库中,包括它们的元数据。 例如,如果我们有一个名为myLibrary的publication,并将其发布mavenLocal()仓库,我们可以运行命令publishPubNamePublicationToMavenLocal来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值