了解编写和应用 Gradle 插件的基础知识。

在本节中,您将:

  • 将自定义任务添加到插件

  • 将插件应用到子项目

  • 使用插件

步骤 0. 开始之前

  1. 您在第 1 部分中初始化了 Java 应用程序。

  2. 您从第 2 部分了解了 Gradle 构建生命周期。

  3. 您在第 3 部分中添加了一个子项目和一个单独的构建。

  4. 您在第 4 部分中查看了设置文件。

  5. 您在第 5 部分中编写了构建脚本。

  6. 您在第 6 部分中创作了一个任务。

步骤 1. 开发插件

让我们将我们的自定义LicenseTask与我们的插件联系起来。

LicensePlugin使用以下代码更新Plugin(不要更改文件中的其他任何内容):

gradle/license-plugin/plugin/src/main/kotlin/license/LicensePlugin.kt
class LicensePlugin: Plugin<Project> {
    override fun apply(project: Project) {
        project.tasks.register("license", LicenseTask::class.java) { task ->
            task.description = "add a license header to source code"   // Add description
            task.group = "from license plugin"                         // Add group
        }
    }
}
gradle/license-plugin/plugin/src/main/kotlin/license/LicensePlugin.groovy
class LicensePlugin implements Plugin<Project> {
    void apply(Project project) {
        project.tasks.register("license", LicenseTask) { task ->
            task.setDescription("add a license header to source code")  // Add description
            task.setGroup("from license plugin")                        // Add group
        }
    }
}

步骤2.添加license.txt文件

添加一个名为license.txt项目根目录的文件,并向其中添加以下文本:

许可证.txt
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

步骤 3. 应用插件

将插件应用到app子项目(如果尚未完成):

app/build.gradle.kts
plugins {
    application
    id("com.tutorial.license") // Apply custom plugin
}
app/build.gradle
plugins {
    id 'application'
    id('com.tutorial.license') // Apply custom plugin
}

通过列出子项目中的可用任务来确保正确应用插件app

$ ./gradlew :app:tasks

------------------------------------------------------------
Tasks runnable from project ':app'
------------------------------------------------------------

...

From license plugin tasks
-------------------------
license - add a license header to source code

步骤 4. 运行自定义任务

最后,是时候运行新任务了。

首先,让我们检查一些源代码:

应用程序/src/main/java/authoring/tutorial/App.java
package authoring.tutorial;

import com.gradle.CustomLib;

public class App {
    public String getGreeting() {
        return "CustomLib identifier is: " + CustomLib.identifier;
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

接下来,让我们使用以下命令运行任务./gradlew :app:license

$ ./gradlew :app:license

> Task :license-plugin:plugin:compileKotlin UP-TO-DATE
> Task :license-plugin:plugin:compileJava NO-SOURCE
> Task :license-plugin:plugin:pluginDescriptors UP-TO-DATE
> Task :license-plugin:plugin:processResources UP-TO-DATE
> Task :license-plugin:plugin:classes UP-TO-DATE
> Task :license-plugin:plugin:jar UP-TO-DATE

> Configure project :app

> Task :app:license

BUILD SUCCESSFUL in 410ms
5 actionable tasks: 1 executed, 4 up-to-date

现在检查相同的源代码,其中应包含许可证标头:

应用程序/src/main/java/authoring/tutorial/App.java
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package authoring.tutorial;

import com.gradle.CustomLib;

public class App {
    public String getGreeting() {
        return "CustomLib identifier is: " + CustomLib.identifier;
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

恭喜,您已完成本教程!

步骤 4. 后续步骤

我们建议仔细阅读用户手册的每个部分。

下一步: 构建构建>>