前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android Gradle 插件】将自定义 Gradle 插件上传到自建 Maven 仓库 ④ ( 默认生成的 pom 文件 | Maven 中的 pom 配置 | 自定义 pom 文件节点 )

【Android Gradle 插件】将自定义 Gradle 插件上传到自建 Maven 仓库 ④ ( 默认生成的 pom 文件 | Maven 中的 pom 配置 | 自定义 pom 文件节点 )

作者头像
韩曙亮
发布2023-03-30 17:18:54
1.4K0
发布2023-03-30 17:18:54
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

Android Plugin DSL Reference 参考文档 :

一、默认生成的 pom 文件


在 自定义 Gradle 插件 编译后生成的 pom 文件 , 存放在 " build/publications/plugin/pom-default.xml " 文件中 ,

在这里插入图片描述
在这里插入图片描述

默认生成的 pom 文件内容如下 :

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- This module was also published with a richer model, Gradle metadata,  -->
  <!-- which should be used instead. Do not delete the following line which  -->
  <!-- is to indicate to Gradle or any Gradle module metadata file consumer  -->
  <!-- that they should prefer consuming it instead. -->
  <!-- do_not_remove: published-with-gradle-metadata -->
  <modelVersion>4.0.0</modelVersion>
  <groupId>kim.hsl.plugin</groupId>
  <artifactId>plugin</artifactId>
  <version>0.1</version>
  <dependencies>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib</artifactId>
      <version>1.5.0</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

二、Maven 中的 pom 配置


pom 文件中 , 除了默认生成的配置外 , 还可以添加自定义 pom 节点属性 ;

pom 配置文件也会随着 源码 , jar 包 , 文档 一同上传到 Maven 仓库中 ;

进入 Maven 官方网站 https://maven.apache.org/ ,

在这里插入图片描述
在这里插入图片描述

查询 pom 配置参考文档 https://maven.apache.org/pom.html ;

在这里插入图片描述
在这里插入图片描述

pom 常见的配置如下 :

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>
 
  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>
 
  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>
 
  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

三、自定义 pom 文件节点


现在向 pom 配置文件中加入如下 Licenses 许可信息配置 ;

代码语言:javascript
复制
<licenses>
  <license>
    <name>Apache License, Version 2.0</name>
    <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
    <comments>A business-friendly OSS license</comments>
  </license>
</licenses>

参考文档 : https://maven.apache.org/pom.html#Licenses

自定义 pom 节点代码如下 :

代码语言:javascript
复制
            // 自定义 pom 节点
            pom.withXml {
                /*  添加如下节点
                    <licenses>
                      <license>
                        <name>Apache License, Version 2.0</name>
                        <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
                        <distribution>repo</distribution>
                        <comments>A business-friendly OSS license</comments>
                      </license>
                    </licenses>
                 */

                // 先创建 root 根节点
                def root = asNode()

                // 向根节点中添加 <licenses> 节点
                // 向 <licenses> 节点中添加 <license> 节点
                def licensesNode = root
                        .appendNode("licenses") // <licenses> 节点
                        .appendNode("license")  // <license> 节点

                // 为 <license> 节点 配置 name 节点属性
                licensesNode.appendNode("name", "Apache License, Version 2.0")
                // 为 <license> 节点 配置 url 节点属性
                licensesNode.appendNode("url", "https://www.apache.org/licenses/LICENSE-2.0.txt")
                // 为 <license> 节点 配置 distribution 节点属性
                licensesNode.appendNode("distribution", "repo")
                // 为 <license> 节点 配置 comments 节点属性
                licensesNode.appendNode("comments", "A business-friendly OSS license")
            }

完整代码如下 :

代码语言:javascript
复制
plugins {
    id 'java-library'
    id 'kotlin'
    id 'groovy'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation gradleApi()
    implementation localGroovy()
    implementation fileTree(dir: 'libs', includes: ['*.jar'])
}

// 指定自定义 Gradle 插件的分组
group 'kim.hsl.plugin'

// 指定自定义 Gradle 插件的版本号
version '0.1'

// 自定义 Gradle 插件的名称 , 默认为工程名
// 也可以在 publishing / publications 脚本块中 自己指定


// 用于将 插件上传到 远程仓库 或者 本地仓库 中
apply plugin: 'maven-publish'

// 自定义源码打包任务
// 自定义 Jar 类型的 Gradle 任务
// 将源码打包到 jar 包中
task sources2Jar(type: Jar) {
    // 指明要打的 jar 包名称
    // 最终打包的名称是 plugin-0.1-sources.jar
    baseName 'plugin'
    // 指定分类器 , 与其它 jar 包进行区分
    classifier 'sources'
    // 设置打包哪些文件
    // 这里设置的是 main 目录下的所有文件
    from sourceSets.main.allSource
}

// 自定义文档打包任务
// 自定义 Jar 类型的 Gradle 任务
// 将文档打包到 jar 包中
task document2Jar(type: Jar, dependsOn: [javadoc, groovydoc]) {
    // 指明要打的 jar 包名称
    // 最终打包的名称是 plugin-0.1-doc.jar
    baseName 'plugin'
    // 指定分类器 , 与其它 jar 包进行区分
    classifier 'doc'
    // 设置打包哪些文件
    // 这里设置的是 javadoc 和 groovydoc 任务的输出目录
    from javadoc.destinationDir, groovydoc.destinationDir
}

// 配置 工程工件 对应的 jar 包产出 配置
// 这里将 文档打包 和 源码打包 后的 jar 包作为输出
artifacts {
    archives sources2Jar
    archives document2Jar
}

// 发布到 远程/本地仓库 相关配置
publishing {
    publications {
        // plugin 函数是随意命名的函数
        plugin(MavenPublication) {
            // 配置上传内容
            // components.java 是打包的 jar 包
            from components.java

            // 指定自定义 Gradle 插件名称
            artifactId 'plugin'

            // 上传源码
            artifact sources2Jar
            // 上传文档
            artifact document2Jar

            // 自定义 pom 节点
            pom.withXml {
                /*  添加如下节点
                    <licenses>
                      <license>
                        <name>Apache License, Version 2.0</name>
                        <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
                        <distribution>repo</distribution>
                        <comments>A business-friendly OSS license</comments>
                      </license>
                    </licenses>
                 */

                // 先创建 root 根节点
                def root = asNode()

                // 向根节点中添加 <licenses> 节点
                // 向 <licenses> 节点中添加 <license> 节点
                def licensesNode = root
                        .appendNode("licenses") // <licenses> 节点
                        .appendNode("license")  // <license> 节点

                // 为 <license> 节点 配置 name 节点属性
                licensesNode.appendNode("name", "Apache License, Version 2.0")
                // 为 <license> 节点 配置 url 节点属性
                licensesNode.appendNode("url", "https://www.apache.org/licenses/LICENSE-2.0.txt")
                // 为 <license> 节点 配置 distribution 节点属性
                licensesNode.appendNode("distribution", "repo")
                // 为 <license> 节点 配置 comments 节点属性
                licensesNode.appendNode("comments", "A business-friendly OSS license")
            }
        }
    }
}

执行 Gradle 面板中的 publishingPluginPublicationToMavenLocal 任务 ,

在这里插入图片描述
在这里插入图片描述

查看本地 Maven 仓库 , 生成的 pom 配置如下 :

在这里插入图片描述
在这里插入图片描述

自定义 Gradle 插件 - GitHub 地址 : https://github.com/han1202012/Android_UI

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、默认生成的 pom 文件
  • 二、Maven 中的 pom 配置
  • 三、自定义 pom 文件节点
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档