首页 > 其他分享 >maven-publish 使用发布 andorid aar 到本地仓,项目仓或module 仓

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

时间:2022-11-22 14:24:27浏览次数:43  
标签:Maven module maven POM publish File println def

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()}"
    }
}


标签:Maven,module,maven,POM,publish,File,println,def
From: https://www.cnblogs.com/lizhanqi/p/16914969.html

相关文章

  • go module常见问题
    1.如何兼容GOPATH的旧项目,支持modulemodule名取github.com/****/projectNamegomodinitgithub.com/fwhezfwhez/errorxgomodtidy2.golang.org/x拉取不到怎么办使用r......
  • Pytorch入门(4)—— Tensor和Module的保存与加载
    参考:动手学深度学习注意:由于本文是jupyter文档转换来的,代码不一定可以直接运行,有些注释是jupyter给出的交互结果,而非运行结果!!文章目录​​1.读写Tensor​​​​2.读写......
  • Maven
    一.Maven作用maven可以自动化构建项目(清理、编译、测试、报告、打包、安装、部署),管理依赖二.Maven中的概念1.POM(Projectobjectmodel)<packaging>jar</packaging>......
  • maven正确的集成命令-U -B 等
    在持续集成服务器上使用怎样的mvn命令集成项目,这个问题乍一看答案很显然,不就是 mvncleaninstall 么?事实上比较好的集成命令会稍微复杂些,下面是一些总结:不要忘了clean:......
  • Why the “MAVEN2_CLASSPATH_CONTAINER will not be exported or published”
    OncertainoccasionsEclipsewillpresentyouwiththefollowingwarning:Classpathentryorg.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINERwillnotbeexport......
  • 用nexus建立maven的私服
    最近研究了下Maven的知识,多数都是老文章,网上的也是互相粘贴,苦了初学者,经过2天的琢磨,终于搭建出了eclipse3.4+maven2+nexus的开发环境,写出心得,只供初学者参考,老鸟们多给点意......
  • nexus-maven-repository-index.zip手动下载与设置
    问题描述:在启动eclipse的时候,在maven控制台经常会看到更新nexus-maven-repository-index.zip,用eclipse更新速度会很慢,甚至有不能完成下载的情况;问题解决:1.在你的eclipse......
  • Creating Dynamic Web Project using Maven in Eclipse
    WhileusingMavenasbuildtoolinourproject,IfounditverydifficulttocreateaDynamicWebProjectwhichsupports​​Mavendependencies​​andcanexec......
  • maven2创建一个eclipse工程,设置M2_REPO
    用maven创建一个支持eclipse的工程非常简单,在mvn的项目目录下:mvneclipse:eclipse即可。再打开eclipse->import->General:ExistingProjectsintoWorkspace->包含该工程......
  • cloud-consumer-order80 微服务消费者订单Module模块
    1、建cloud-provider-payment80012、改POM<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http:......