Vous êtes sur la page 1sur 2

Gradle Cheat Sheet -i ログレベルをinfoにする 標準 outputs TaskOu

tputs
タスクの出力物。未更新タスクスキッ
プ(UP-TO-DATE)用
-q Gradleのログメッセージを抑制し、タスクによ
java sourceSets SourceS ソースセット(デフォルトでmainと
Javaプロジェクトの標準レイアウト る出力のみを表示させる
etConta java)
-a マルチプロジェクトのビルド時に、依存するプ iner
+ project ロジェクトのビルドを行わないようにする
java sourceComp JavaVer コンパイル時に使用するJavaのバージョ
+ src -m 空実行。実行されるタスクの順番を調べるため atibility sion ン(1.7など)
+ main
に使う java manifest Manifes マニフェスト
+ java
+ resources --daemon デーモンモードでビルドを実行する t
+ test --offline 常にキャッシュされた依存モジュールを使う
+ java 拡張プロパティ
+ resources よく使うタスク
- build.gradle // Project
- gradle.properties まったくプラグインを適用していない場合でも使えるヘルプタスク ext {
- settings.gradle springVersion = "3.1.0.RELEASE"
タスク名 説明 emailNotification = "build@master.org"
build.gradle のテンプレート tasks タスク一覧を出力 }
dependencies 依存関係一覧を出力
// plugins projects サブプロジェクト一覧を出力(マルチプロジェ // 他のオブジェクト
sourceSets.all { ext.purpose = null } // 1. プロパティを追加
apply plugin: "java" クトを使っている場合に使う)
apply plugin: "maven" sourceSets.main.purpose = "production" // 2. プロパティに値をセット
properties プロパティ一覧を出力
apply plugin: "groovy" プラグインを適用して使えるようになるタスク プロパティを追加せず、直接値をセットしても(1.6時点では)エラーにならないが、
apply plugin: "application"
非推奨である(以下の警告が出力される)。拡張プロパティを追加する時は、extを使
プラグ うことを推奨。
// default tasks
イン タスク名 説明
defaultTasks 'clean', 'build' Deprecated dynamic property: "purpose" on "source set 'main'", value: "production".
java jar JARファイルを作成
// properties java assemble JAR(やWARやEAR)を作成 よく使う処理
sourceCompatibility = '1.7' java test ソースコードをテスト
def defaultEncoding = 'UTF-8' java check テストし、検証タスクを実行する。testタスク copy
[ に依存
compileJava,
java javadoc JavaDocを生成 // 方法1: Copyタスクを使う場合
compileTestJava,
javadoc java build すべてのアーカイブ作成、テスト実行、検証タ task myCopy(type: Copy) << {
]*.options*.encoding = defaultEncoding スクを実行 from 'src/*.txt'
java clean プロジェクトのビルドディレクトリを削除 into 'dest'
// repositories // include '**/*.html'
war war WARファイルを作成
repositories { // exclude '**/*.jsp'
ear ear EARファイルを作成 // rename { String fileName -> fileName.replace('-staging-', '') }
mavenCentral()
mavenLocal() java & install アーティファクトをリポジトリに登録する }
maven { url "http://maven.seasar.org/maven2" } maven
maven { url "http://repository.jboss.org/nexus/content/groups/public-jboss/" } // 方法2: Project.copy() メソッドを使う場合
maven { url "http://repository.apache.org/content/groups/public" } タスクのカスタマイズ task myCopy2 << {
maven { url "http://download.java.net/maven/glassfish" } copy {
} from 'src/*.txt'
// プロパティ: 個別に
into buildDir
fooTask {
// configurations }
fooTaskProperty = "xxx"
configurations { }
fooMethod "yyy"
doc // extra configuration } 単なるコピーだけならCopyタスクの方がよい。 Project.copy() メソッドは他のタスク
}
の中に組み込んで使うことが多い。
// プロパティ: まとめて
// dependencies
dependencies {
[barTask, bazTask]*.barbazProperty = "zzz" mkdir
compile 'org.codehaus.groovy:groovy-all:2.0.5' // Gradle 起動時
// runtime fooTask { } task myMkdir << {
testCompile 'junit:junit:4.11' file('tmp').mkdir()
// testRuntime // タスクの最初に実行 }
aTask.doFirst { }
// extra configuration unzip
doc 'g:m:v@zip' // @ext // タスクの最後に実行
aTask.doLast { } task myUnzip << {
// providedCompile(War plugin) aTask << { } copy {
// providedRuntime(War plugin)
タスクにどのようなプロパティがあるかを調べるには、そのタスクのTask Typeを辿れ from zipTree('aaa.zip')
}
into buildDir
ばよい。
}
// tasks 例1: javaプラグインで追加されるcleanタスクのTask TypeはDelete。
}
例2: warプラグインで追加されるwarタスクのTask TypeはWar。
大元であるProjectのプロパティやメソッドが呼ばれる。 例えばプラグインを適用する
applyはProject.apply()の呼び出し。 sourceCompatibilityのようにプラグイン(javaプ よく使うプロパティ tar
ラグイン)を適用することで 追加されるプロパティもある。
task myTar(type: Tar) {
build.gradle の分割 プラグ プロパティ compression = Compression.GZIP // NONE/GZIP/BZIP2
イン 名 型 説明 from 'content'
apply from: "gradle/foo.gradle" 標準 rootProject Project ルートプロジェクト
標準 rootDir File プロジェクトのルートディレクトリ destinationDir = file('dest') // default: project.distsDir = "build/distributions"
コマンドラインオプション 標準 buildDir File ビルドディレクトリ
}

標準 inputs TaskInp タスクの入力物。未更新タスクスキッ 外部コマンド実行


uts プ(UP-TO-DATE)用
オプション 説明
// 方法1: Copyタスクを使う場合 attributes "Main-Class" : "foo.bar.Main" test.jvmArgs '-XX:-UseSplitVerifier'
task myExec(type: Exec) << { attributes 'Class-Path': manifestClasspath [checkstyleMain, checkstyleTest, findbugsMain, findbugsTest]*.ignoreFailures = true
commandLine 'echo', 'hello' } [checkstyleTest, findbugsTest]*.excludes = ['**/*']
} from (configurations.compile.resolve().collect { it.isDirectory() ? it : fileTree(it) }) { // checkStyleMain {
exclude '**/*.jar' // configFile = file('config/checkstyle/checkstyle.xml')
// 方法2: groovy の execute() メソッドを使う場合 } // }
task myCopy << { destinationDir = file("build/distribution")
['echo', 'hello'].execute() }
タスク
} • check
実行可能FatJar • coberturaMain
ファイルコレクション レポート
jar { • build/reports/checkstyle/main.xml
// ファイル from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } • build/reports/findbugs/main.xml
File configFile = file('src/config.xml') manifest.mainAttributes("Main-Class" : "foo.bar.Main") • build/reports/cobertura/coverage.xml
}
// ファイルコレクション
依存関係の管理
FileCollection collection = files('src/file1.txt', new File('src/file2.txt'), ['src/file3.txt']) War
dependencies {
// ファイルツリー compile 'org.springframework:spring-core:2.5'
apply plugin: "war"
FileTree tree = fileTree(dir: 'src', include: '**/*.java') // アーティファクトオンリー記法
configurations {
compile 'org.gradle.test.classifiers:service:1.0:jdk15@jar'
ファイルコレクションは、コピー元ファイル、ファイル依存関係などの指定で使われる。 moreLibs
// 推移的な依存関係の除外
}
compile 'org.hibernate:hibernate:3.0.5') {
タスクの定義 war {
transitive = true
from 'src/main/webapp'
}
基本的な作り方 classPath configurations.moreLibs
}
// ローカルのファイル依存関係
compile fileTree(dir: 'libs', include: '*.jar')
task hello << { Ear }
println "hello!"
}
apply plugin: "ear" 依存関係のキャッシュ
// 拡張タスクプロパティ dependencies {
task myTask { deploy project(':war') dependencies に記述した依存するサードパーティのアーティファクト(依存モジュー
ext.myProperty = "myValue" earlib 'log4j:log4j:1.2.15@jar' ル)は 以下の優先順位でキャッシュされる。
} }
1. ${GRADLE_USER_HOME}/cache
war {
2. ${USER_HOME}/.gradle/cache
タスク型Task typeを使う場合 appDirName 'src/main/app' Mavenキャッシュと管理方法が異なるので、そのまま Maven リポジトリとして公開は
} できない、
task archive (type: Zip) { マルチプロジェクト
from "src" 警告
// "build/distributions/xxx.zip" ルートプロジェクトの parent/build.gradle
} Jenkins Gradle Plugin 1.22 では GRADLE_USER_HOME は Jenkins の ワークスペースがセットされるため、
allprojects { task hello << {task -> println "root and all sub projects" } } カスタムワークスペースを使ってワークスペースを共有しない限り、 ジョブ間でキャッシュを共有できない
Task typeはCopy, Zip, Tar, JavaDocあたりが頻出。
で、1.23を使うのがよい。
subprojects { hello << {println "all sub projects" } }
その他
project(':sub1').hello << { println "one sub project" }
Ant
// 依存関係をつける
task taskX(dependsOn: 'taskY') << { println 'taskX' } 階層
task hello1 << {
// 置き換え ルートプロジェクトの parent/settings.gradle ant.echo(message: 'hello1')
task taskZ(overwrite: true) << { println 'taskZ' } }
include "sub1", "sub2", "sub3"

アーティファクト サブプロジェクト間の依存関係 task hello2 << {


ant {
dependencies { echo(message: 'hello1')
artifacts { compile project(':sub1') }
archives jar compile project(path: ':sub2', configuration: 'abc') }
} }

サブプロジェクトのタスクを実行 Wrapper
install
repositories { :sub1:build
mavenInstaller { task wrapper(type: Wrapper) {
gradleVersion = '1.6'
pom.groupId = 'com.github.tq-jappy' フラット
pom.version = '1.0.0-SNAPSHOT' }
pom.artifactId = 'example' ルートプロジェクトの parent/settings.gradle gradlew で実行。
}
} includeFlat 'sub1', 'sub2', 'sub3' 1.7以降はタスクを作る必要がなくなる(予定らしい)
}
静的解析(Checkstyle/Findbugs/Cobertura) gradle.properties
実行可能Jar
build.gradle # proxy settings
systemProp.http.proxyHost=http://proxy:8080/
jar { apply plugin: "checkstyle"
systemProp.http.proxyPort=http://proxy:8080/
copy { apply plugin: "findbugs"
systemProp.https.proxyHost=http://proxy:8080/
from configurations.compile buildscript {
systemProp.https.proxyPort=http://proxy:8080/
into "build/distribution/lib" apply from: 'https://github.com/valkolovos/gradle_cobertura/raw/master/repo/'
} + 'gradle_cobertura/gradle_cobertura/1.2.1/coberturainit.gradle'
def manifestClasspath = configurations.compile.collect{ 'lib/' + it.getName() }.join(' ') }
manifest {

Vous aimerez peut-être aussi