Kotlin version implements Gradle plugin

Kotlin version implements Gradle plugin

Link to this article: https://blog.csdn.net/feather_wch/article/details/131746478

  • Create a Gradle project and select Kotlin/JVM as the project SDK. Suppose the name of the project is hello-plugin.
  • Create a new source set in the project, such as plugin, to store the source code and resources of the plugin. Create a plugin directory under the src directory, and create a kotlin directory and a resources directory in it.
  • Create a class that implements the Plugin interface in the plugin source set, such as HelloPlugin. Create a HelloPlugin.kt file in the src/plugin/kotlin/com/example directory with the following content:
package com.example

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Exec

// 实现Plugin接口
class HelloPlugin : Plugin<Project> {
    
    

    // 重写apply方法
    override fun apply(project: Project) {
    
    
        // 在项目中添加一个hello任务,用于打印一句问候语
        project.tasks.register("hello", Exec::class.java) {
    
    
            it.commandLine("echo", "Hello from plugin!")
        }
    }
}
  • Create a resources/META-INF/gradle-plugins directory in the plugin source set to store the property files of the plugin. Create a hello.properties file in this directory with the following contents:
# 指定插件类的全限定名
implementation-class=com.example.HelloPlugin
  • Create a settings.gradle.kts file in the project root directory to configure the project name and version number. The content is as follows:
// 配置项目名称
rootProject.name = "hello-plugin"
// 配置项目版本号
rootProject.version = "1.0.0"
  • Create a build.gradle.kts file in the project root directory to configure the build logic of the project. Here, you need to add some dependencies like gradleApi() and kotlin("stdlib"), and some tasks like jar and publishToMavenLocal. The content is as follows:
plugins {
    
    
    // 应用java插件
    java
    // 应用kotlin插件
    kotlin("jvm") version "1.6.0"
    // 应用maven-publish插件
    `maven-publish`
}

// 配置源集
sourceSets {
    
    
    // 获取plugin源集
    val plugin by getting {
    
    
        // 设置编译输出目录
        output.setClassesDirs(files("$buildDir/classes/plugin"))
        // 设置资源输出目录
        output.setResourcesDir("$buildDir/resources/plugin")
        // 设置编译输入目录
        java.srcDir("src/plugin/kotlin")
        // 设置资源输入目录
        resources.srcDir("src/plugin/resources")
    }
}

// 配置依赖项
dependencies {
    
    
    // 添加gradleApi依赖项,用于访问Gradle API
    implementation(gradleApi())
    // 添加kotlin标准库依赖项,用于编译Kotlin代码
    implementation(kotlin("stdlib"))
}

// 配置jar任务,用于打包插件类和资源为jar文件
tasks.jar {
    
    
    // 设置jar文件名为项目名称-版本号.jar,比如hello-plugin-1.0.0.jar
    archiveFileName.set("${
      
      project.name}-${
      
      project.version}.jar")
    // 设置jar文件所在目录为build/libs目录
    destinationDirectory.set(file("$buildDir/libs"))
    // 从plugin源集中获取类和资源,并添加到jar文件中
    from(sourceSets["plugin"].output)
}

// 配置publishing扩展,用于发布插件到Maven仓库
publishing {
    
    
    // 配置发布到本地Maven仓库的方式
    repositories {
    
    
        mavenLocal()
    }
    // 配置发布的内容
    publications {
    
    
        // 创建一个MavenPublication类型的publication,并命名为pluginJar
        create<MavenPublication>("pluginJar") {
    
    
            // 设置groupId为com.example
            groupId = "com.example"
            // 设置artifactId为项目名称
            artifactId = project.name
            // 设置version为项目版本号
            version = project.version.toString()
            // 设置发布的artifact为jar任务生成的jar文件
            artifact(tasks.jar)
        }
    }
}
  • Run the publishToMavenLocal task in the IDE to package the plugin into a jar file and publish it to the local Maven repository. You can find the released files in the ~/.m2/repository/com/example/hello-plugin/1.0.0 directory, including hello-plugin-1.0.0.jar and hello-plugin-1.0.0.pom.
  • In another Gradle project, you can use the plugins block to apply the plugin you developed, such as plugins { id("hello") version "1.0.0" }. In this way, the functions provided by the plug-in can be used, such as running the hello task and printing a greeting.

Guess you like

Origin blog.csdn.net/feather_wch/article/details/131746478