maven-publish 使用发布 andorid aar 到本地仓,项目仓或module 仓

apply plugin: 'maven-publish'
println "<------${project.name}mount mavenpush-------->"
/*切勿定义:groupId,artifactId,version 作为变量
否则导致自定义 publishing中赋值失效.
导致发布出来的groupid是项目的名,artifactId是当前的module名,version 是unspecified ,深刻的教训
def groupId = "top.lizhanqi"
def artifactId = "ext"
def version = "1.0.0"
*/
def mGroupId =  "top.lizhanqi"
def mArtifactId = "ext"
def mVersion = "1.0.0"
/**
 * 当前插件使用方法:
 * 第一步:复制当前文件到moudle下
 * 第二步:修改gropid ,
 * 第三部:引入apply from: 'maven-push.gradle'
 * 第四步:例如:
 *  1.点击publishi 即可发布到所有仓
 *  2.publishReleasePublicationToMavenLocalRepository:发布Release的产物到本地Maven仓
 *  3.publishReleasePublicationToProjecetMavenRepository:发布Release的产物到项目的Maven仓
 */
/*使用文档
*官方:https://docs.gradle.org/current/userguide/publishing_maven.html
*androids官方:https://developer.android.google.cn/studio/build/maven-publish-plugin#groovy
*参考:https://blog.csdn.net/m0_64365896/article/details/121746445?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-121746445-blog-120828221.pc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-121746445-blog-120828221.pc_relevant_3mothn_strategy_recovery&utm_relevant_index=1
*参考:https://zhuanlan.zhihu.com/p/147376559
*/
/**
 * 常见task任务作用:
 * 1.generatePomFileForPubNamePublication — GenerateMavenPom
 * Creates a POM file for the publication named PubName, populating the known metadata such as project name, project version, and the dependencies. The default location for the POM file is build/publications/$pubName/pom-default.xml.
 * 翻译:为名为PubName的发布创建POM文件,填充已知元数据,如项目名称、项目版本和依赖项。POM文件的默认位置是build/publications/$pubName/POM-default.xml。
 *  理解:为发布任务创建初始化pom文件
 *  ------------------------------------------------------------------------
 * 2.publishPubNamePublicationToRepoNameRepository — PublishToMavenRepository
 * Publishes the PubName publication to the repository named RepoName. If you have a repository definition without an explicit name, RepoName will be "Maven".
 * 翻译:将PubName发布发布到名为RepoName的存储库。如果您有一个没有显式名称的存储库定义,RepoName将是“Maven”。
 * 理解:把任务发布到Maven存储库,如果不定义存储库名称,默认是"Maven"
 *  ------------------------------------------------------------------------
 *  3.publishPubNamePublicationToMavenLocal — PublishToMavenLocal
 * Copies the PubName publication to the local Maven cache — typically $USER_HOME/.m2/repository — along with the publication’s POM file and other metadata.
 * 翻译:将PubName发布复制到本地Maven缓存-通常为$USER_HOME/.m2/repository-以及出版物的POM文件和其他元数据。
 *  理解:把任务发布到的maven仓,不指定仓库地址则发布到本地仓库(.m2/repository下),包含pom文件和其他常规文件
 *  ------------------------------------------------------------------------
 * 4.publish
 * Depends on: All publishPubNamePublicationToRepoNameRepository tasks
 * 取决于:所有publishSubNamePublicationToRepoNameRepository任务
 * An aggregate task that publishes all defined publications to all defined repositories. It does not include copying publications to the local Maven cache.
 * 将所有已定义的发布发布到所有已定义存储库的聚合任务。它不包括将发布复制到本地Maven缓存。
 *   ------------------------------------------------------------------------
 * 5.publishToMavenLocal
 * Depends on: All publishPubNamePublicationToMavenLocal tasks
 * 取决于:所有publishPubNamePublicationToMavenLocal任务
 * Copies all defined publications to the local Maven cache, including their metadata (POM files, etc.).
 * 将所有定义的发布复制到本地Maven缓存,包括其元数据(POM文件等)。
 *
 */
    publishing {
        publications { PublicationContainer publicationContainer ->
            //正式版
            Release(MavenPublication) {
                groupId = mGroupId
                artifactId = mArtifactId
                version = mVersion
                // 依赖 bundleReleaseAar 这是构建生产渠道的aar包任务,并上传其产出的aar ,推荐
                afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }

	   //依赖渠道方式
		//from components.release
		//这种方式需要把afterEvaluate写在publishing外层否则没有文件,且pom 自定义会出现节点重复问题,不推荐

                //也可以指定上传的AAR包,但是需要先手动生成aar
                // artifact "$buildDir/outputs/aar/${project.name}-release.aar"

                // 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')
                        }
                    }
                }
            }
        }

        // 配置maven仓库
        repositories { RepositoryHandler handler ->
            //本地maven仓
            handler.mavenLocal()
            //module仓库
            maven {
                allowInsecureProtocol true//非https参考需要设置
                name = "MoudleMaven" // 仓库名可自定义
                println "module dir"+new File(projectDir, "repo");
                url = new File(projectDir, "repo").toURL()
            }
            //project仓库
            maven {
                name = "ProjectMaven" //仓库名可自定义
                url = new File(rootDir, "repo").toURL()
            }
        }
    }

// 根据指定的源码路径方式生产jar,这种方式在gradle中动态构建插入的代码例如:BuildConfig 等代码无法打包进去.
task generateSourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier 'sources'
}

tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }

// 本地maven仓库方式
def MAVEN_LOCAL = new File(System.getProperty("user.home"), File.separator + ".m2" + File.separator + "repository" + File.separator);
//本地maven仓发布后发布一份到本项目maven仓
task syncCopyMaven(type: Copy, dependsOn: assemble) {
    group 'maven'
    doFirst {
        println("仓库同步" + file(projectMaven) + "\t---->>\t" + file(localMaven))
    }
    def localMaven = new File(System.getProperty("user.home"), File.separator + ".m2" + File.separator + "repository" + File.separator);
    def projectMaven = new File(projectDir, "repo")
    //设置拷贝的文件   qw
    from(projectMaven)
    //打进jar包后的文件目录
    into(localMaven)
    //将classes.jar放入build/libs/目录下
    //include ,exclude参数来设置过滤
    //(我们只关心classes.jar这个文件)
//    include('classes.jar')
    doLast {
        println("仓库同步完成")
    }
}
afterEvaluate {
    //获取maven 仓形式的依赖
    def a= project.configurations*.
            dependencies*.
            findAll { it instanceof ExternalDependency }.
            flatten()
    //
    def p= project.configurations*.
            dependencies*.
            findAll { it instanceof ProjectDependency }.
            flatten()
    println  "依赖如下:${a}"
    println  "依赖如下:${p}"
    getAllprojects().forEach{
        println  "导入的全部项目:${it.getName()}"
    }
}


posted @ 2022-11-22 14:07  烟花易冷心易碎  阅读(1115)  评论(0编辑  收藏  举报