Android Plugin DSL Reference 参考文档 :
在 自定义 Gradle 插件 编译后生成的 pom 文件 , 存放在 " build/publications/plugin/pom-default.xml " 文件中 ,
默认生成的 pom 文件内容如下 :
<?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>
pom 文件中 , 除了默认生成的配置外 , 还可以添加自定义 pom 节点属性 ;
pom 配置文件也会随着 源码 , jar 包 , 文档 一同上传到 Maven 仓库中 ;
进入 Maven 官方网站 https://maven.apache.org/ ,
查询 pom 配置参考文档 https://maven.apache.org/pom.html ;
pom 常见的配置如下 :
<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 配置文件中加入如下 Licenses 许可信息配置 ;
<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 节点代码如下 :
// 自定义 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")
}
完整代码如下 :
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