gradle–groovy-dsl和kotlin-dsl对比
常用对比
- groovy可以适用单引号和双引号,而kotlin只能使用双引号
- groovy运行在函数调用时省略括号,而kotlin必须加上括号
- groovy在赋值时可以省略等于号,而kotlin必须加上等号
- 为了减少迁移成本,在groovy时就应该约定使用双引号,调用加上括号,使用等号赋值
插件引用对比
- Groovy DSL有两种方式去引用插件:
- 1 plugins{} //强烈推荐
- 2 apply plugin
- 注意,核心插件可以用短名称,非核心插件必须声明id和version(Gradle - Plugins)
plugins {
id 'java'
id 'jacoco'
id 'maven-publish'
id 'org.springframework.boot' version '2.4.1'
}
plugins {
java
jacoco
`maven-publish`
id("org.springframework.boot") version "2.4.1"
}
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'org.springframework.boot'
apply(plugin = "java")
apply(plugin = "jacoco")
apply(plugin = "org.springframework.boot")
apply<ExamplePlugin>()
gradle脚本引用对比
apply from: 'other.gradle'
apply(from = "other.gradle.kts")
任务task对比
配置任务
tasks.jar {
archiveFileName = 'foo.jar'
}
tasks.named('jar') {
archiveFileName = 'foo.jar'
}
tasks.getByName('jar') {
archiveFileName = 'foo.jar'
}
tasks.jar {
archiveFileName.set("foo.jar")
}
tasks.named<Jar>("jar") {
archiveFileName.set("foo.jar")
}
tasks.getByName<Jar>("jar") {
archiveFileName.set("foo.jar")
}
创建任务
task greeting {
doLast { println 'Hello, World!' }
}
tasks.create('greeting') {
doLast { println('Hello, World!') }
}
tasks.register('greeting') {
doLast { println('Hello, World!') }
}
tasks.register('docZip', Zip) {
archiveFileName = 'doc.zip'
from 'doc'
}
tasks.create(name: 'docZip', type: Zip) {
archiveFileName = 'doc.zip'
from 'doc'
}
task("greeting") {
doLast { println("Hello, World!") }
}
tasks.create("greeting") {
doLast { println("Hello, World!") }
}
tasks.register("greeting") {
doLast { println("Hello, World!") }
}
tasks.register<Zip>("docZip") {
archiveFileName.set("doc.zip")
from("doc")
}
tasks.create<Zip>("docZip") {
archiveFileName.set("doc.zip")
from("doc")
}
依赖对比
plugins {
id 'java-library'
}
dependencies {
implementation 'com.example:lib:1.1'
runtimeOnly 'com.example:runtime:1.0'
testImplementation('com.example:test-support:1.3') {
exclude(module: 'junit')
}
testRuntimeOnly 'com.example:test-junit-jupiter-runtime:1.3'
}
plugins {
`java-library`
}
dependencies {
implementation("com.example:lib:1.1")
runtimeOnly("com.example:runtime:1.0")
testImplementation("com.example:test-support:1.3") {
exclude(module = "junit")
}
testRuntimeOnly("com.example:test-junit-jupiter-runtime:1.3")
}
参考
|