第 7 部分:撰寫外掛程式
版本 8.7
學習撰寫和套用 Gradle 外掛的基礎知識。
步驟 1. 開發外掛
讓我們將自訂的 LicenseTask
與我們的 plugin 連結。
使用以下 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. 後續步驟
我們建議您瀏覽使用者手冊的各個章節。
後續步驟: 建構結構 >>