Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Last active November 14, 2017 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/52b367039b734e851308dd4af81815e9 to your computer and use it in GitHub Desktop.
Save mike-neck/52b367039b734e851308dd4af81815e9 to your computer and use it in GitHub Desktop.
gradle kotlin build script の書き方的なあれ
// プラグイン関連
// プラグインの指定(1) - buildscript 使わない
plugins {
java
kotlin("jvm")
}
// プラグインの指定(2) - buildscript 使う
buildscript {
dependencies {
classpath("com.android.tools.build:gradle:2.3.3")
}
repositories {
jcenter()
}
}
apply {
plugin("com.android.application")
}
// task の作り方
// tasks ブロック内で作成する
tasks {
// 特にタスクのタイプを指定しない場合
"foo" {
doLast {
println("this is foo.")
}
}
// 特にタスクのタイプを指定しない場合 & タスクをスクリプトの後ろの方で参照する
val bar by creating {
doLast {
println("this is bar.")
}
}
// タスクのタイプを指定する(1) & タスクをスクリプトの後ろの方で参照する
val createConfig by creating(Copy::class) {
from("src/main/resources")
into("conf")
}
// タスクのタイプを指定する(2)
"copyConf" (Copy::class) {
dependsOn(bar)
from("conf") {
include("**/*.properties")
include("**/*.xml")
include("**/*.xsd")
}
into("$buildDir/conf")
}
createConfig.finalizedBy("copyConf")
}
// tasks ブロック外で作成する
task("baz") {
println("this is baz.")
}
// repository-handler/dependency-handlerなどはgroovyとあまり変わらない
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation("org.slf4j:slf4j-api")
}
// コンパイルの指定
tasks.withType<JavaCompile> {
sourceCompatibility = "9" // build.gradle.kts を Java9 で動かせないっぽい
targetCompatibility = "9" // build.gradle.kts を Java9 で動かせないっぽい
options.encoding = "UTF-8"
}
// プラグインで追加されているタスクの設定(1) 若干まどろっこしい
tasks {
withType<Jar>().matching { it.name == "jar" }.all {
manifest {
attributes(mapOf(
"Implementation-Version" to project.version
))
}
}
}
// プラグインで追加されているタスクの設定(2) もっとまどろっこしい
tasks {
"jar" {
val jar: Task = this
when(jar) {
is Jar -> {
jar.manifest {
attributes(mapOf(
"Implementation-Version" to project.version
))
}
}
}
}
// ↑のはこれでよいらしい
"jar"(Jar::class) {
jar.manifest {
attributes(mapOf("Automatic-Module-Name" to "${project.name}"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment