Maven仓库上传组件 2020-11-09

Android Gradle 插件 3.6.0 及更高版本支持 Maven Publish Gradle 插件, 可让您将构建工件发布到 Apache Maven 代码库。Android Gradle 插件会为应用或库模块中的每个构建变体工件创建一个组件,您可以使用它来自定义要发布到 Maven 代码库的发布内容。

Android 插件所创建的组件取决于模块是否使用应用或库插件,如下表所述。

Android Gradle 插件 发布内容工件 组件名称
com.android.library AAR components.variant
com.android.application APK 和可用的 ProGuard 或 R8 映射文件的 ZIP components.variant_apk
com.android.application Android App Bundle (AAB) components.variant_aab

以下代码示例为 AAR 库的发布和调试构建变体创建了发布内容。每项发布内容都会应用匹配的组件,并自定义所生成 POM 的属性,如 Maven 坐标。

// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
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 = 'com.example.MyLibrary'
                artifactId = 'final'
                version = '1.0'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.debug

                groupId = 'com.example.MyLibrary'
                artifactId = 'final-debug'
                version = '1.0'
            }
        }
    }

参考:

  1. https://developer.android.google.cn/studio/build/maven-publish-plugin
  2. https://docs.gradle.org/current/userguide/publishing_maven.html

下面是自己使用的脚本:(自带pom.xml,包含依赖关系,只包含Implements类型依赖)

直接执行:module的gradle任务列表中publishing类型下的publish任务

apply plugin: 'maven-publish'
task generateSourcesJar(type: Jar) {
    group = 'jar'
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            fuck(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release
                // 上传源码
                artifact generateSourcesJar
                // You can then customize attributes of the publication as shown below.
                groupId = 'com.example.MyLibrary'
                artifactId = 'final'
                version = '1.0'
            }

        }

        repositories {
            maven {
                // change to point to your repo, e.g. http://my.org/repo
                url = "$buildDir/repo"

//                url = "http://my.org/repo"
//                credentials {
//                    username 'chengzhen'
//                    password maven_password
//                }
            }
        }
    }

你可能感兴趣的:(Maven仓库上传组件 2020-11-09)