第 7 部分:编写插件
版本 8.7
学习编写和应用 Gradle 插件的基础知识。
步骤 1. 开发插件
让我们将自定义 LicenseTask
与我们的插件关联起来。
使用以下 Plugin
代码更新 LicensePlugin
(不要更改文件中的任何其他内容)
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
的文件,并向其中添加以下文本
license.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. 运行自定义任务
最后,是时候运行新任务了。
首先,让我们检查一些源代码
app/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
现在检查相同的源代码,其中应包含许可证标题
app/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. 后续步骤
我们建议您通读用户手册的每个部分。
后续步骤: 构建结构 >>