ここでは、『dev-xconnecting: build.gradleにプラグインを定義する』でbuild.gradle内に定義したプラグインを、Gradleのカスタムプラグインとしてビルドしてみる。
まず、Groovy用のGradleプロジェクトを作成する(手順は『dev-xconnecting: Eclipse + Gradle STS SupportでGroovy用のGradleプロジェクトを作成』を参照)。
次に、以下のファイルを作成する。
src
└─main
├─groovy
│ Greeting.groovy
│ GreetingTask.groovy
│
└─resources
└─META-INF
└─gradle-plugins
greeting.properties
Greeting.groovyは以下の通り。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example | |
import org.gradle.api.Plugin | |
import org.gradle.api.Project | |
class Greeting implements Plugin<Project> { | |
void apply(Project target) { | |
target.task('greet', type: GreetingTask) | |
} | |
} |
GreetingTask.groovyは以下の通り。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example | |
import org.gradle.api.DefaultTask; | |
import org.gradle.api.tasks.TaskAction; | |
class GreetingTask extends DefaultTask { | |
def greeting = 'Hello' | |
@TaskAction | |
def greet() { | |
println greeting | |
} | |
} |
package文やimport文が追加されている以外は、『dev-xconnecting: build.gradleにプラグインを定義する』で作成したクラスと違いはない。
greeting.propertiesにはプラグインの完全修飾クラス名を指定する。
implementation-class=com.example.Greeting
このプロパティファイルの名前(greeting)がプラグイン名となる(
apply plugin
する際に、この名前を使用する)。プラグインをビルドしてMavenのローカルリポジトリにデプロイするためのbuild.gradleは、以下の通り。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'groovy' | |
apply plugin: 'eclipse' | |
apply plugin: 'maven' | |
group = 'com.example' | |
archivesBaseName = 'greet' | |
version = '1.0-SNAPSHOT' | |
dependencies { | |
compile gradleApi() | |
groovy localGroovy() | |
} | |
uploadArchives { | |
repositories { | |
mavenDeployer { | |
repository(url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath) | |
} | |
} | |
} |
9-12行目でプラグインのビルド時に必要となる外部依存関係を定義している。
gradle build
すると、build/libs内にプラグインのjarファイルが作成される。さらに、gradle uploadArchives
で、このjarファイルがMavenのローカルリポジトリにデプロイされる。作成したプラグインを使用するためのbuild.gradleは、以下の通り。なお、buildscriptプロパティには、ビルドスクリプト自身の外部依存関係(ex. カスタムプラグインのjarファイル)を記述する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'eclipse' | |
apply plugin: 'maven' | |
apply plugin: 'greeting' | |
buildscript { | |
repositories { mavenLocal() } | |
dependencies { classpath 'com.example:greet:1.0-SNAPSHOT' } | |
} | |
task hi(type:com.example.GreetingTask){ | |
greeting = 'Hi' | |
} |
上記build.gradleからタスクを呼び出した結果は、以下の通り。
>gradle -q greet
Hello
>gradle -q hi
Hi
0 件のコメント:
コメントを投稿