0%

maven迁移至gradle kotlin1——Quarkus篇

这里以QuarkusGET STARTED项目为例,主要此项目使用到了quarkus-universe-bom依赖管理及特殊的编译插件。

准备

先下载项目

1
2
3
4
5
6
mvn io.quarkus:quarkus-maven-plugin:1.13.0.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=getting-started \
-DclassName="org.acme.getting.started.GreetingResource" \
-Dpath="/hello"
cd getting-started

迁移

先使用gradle init将maven项目转化为gradle with groovy dsl项目

1
2
3
4
5
6
7
8
9
10
$ gradle init                                                                 
Found a Maven build. Generate a Gradle build from this? (default: yes) [yes, no] yes


> Task :init
Maven to Gradle conversion is an incubating feature.
Get more help with your project: https://docs.gradle.org/6.5.1/userguide/migrating_from_maven.html

BUILD SUCCESSFUL in 7s
2 actionable tasks: 2 executed

手动操作

Step 1: 把.gradle文件中的单引号换为双引号*

1
2
3
sed  's/'"'/"'"/g' settings.gradle > settings.gradle.kts
sed 's/'"'/"'"/g' build.gradle > build.gradle.kts
rm -rf settings.gradle build.gradle

Step 2: 修改dependencies依赖添加括号

1
2
3
4
5
6
dependencies {
implementation("io.quarkus:quarkus-arc:1.13.0.Final")
implementation("io.quarkus:quarkus-resteasy:1.13.0.Final")
testImplementation("io.quarkus:quarkus-junit5:1.13.0.Final")
testImplementation("io.rest-assured:rest-assured:4.3.3")
}

Step 3: 修改plugins

1
2
3
4
plugins {
java
`maven-publish`
}

Step 4: 修改java配置

1
2
3
java {
sourceCompatibility = JavaVersion.VERSION_1_8
}

Step 5: 修改JavaCompile任务配置

1
2
3
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}

Step 6: 修改maven-publish配置

1
2
3
4
5
6
7
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
}
}

Step 7: 修改依赖配置

1
2
3
4
5
6
7
dependencies {
implementation(enforcedPlatform("io.quarkus:quarkus-universe-bom:1.13.0.Final"))
implementation("io.quarkus:quarkus-arc")
implementation("io.quarkus:quarkus-resteasy")
testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.rest-assured:rest-assured")
}

这里使用enforcedPlatform关键字引入bom依赖管理

Step 8: 添加quarkus插件依赖

settings.gradle.kts

1
2
3
4
5
6
7
8
9
10
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
plugins {
id("io.quarkus") version "1.13.0.Final"
}
}

build.gradle.kts

1
2
3
4
5
plugins {
java
`maven-publish`
id("io.quarkus")
}

测试

1
./gradlew quarkusDev
1
2
3
4
5
6
7
8
9
$ http :8080/hello
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 14
Content-Type: text/plain;charset=UTF-8
Keep-Alive: timeout=4
Proxy-Connection: keep-alive

Hello RESTEasy

以上,完成。根据Quarkus官方说法,目前gradle的支持不在预期考虑范围内。不建议在生产型工程上使用。

更多扩展

下列步骤并非必须,而是个人兴趣的扩展——kotlin开发Quarkus支持

Step1: 添加kotlin插件

1
2
3
4
5
plugins {
...
kotlin("jvm") version "1.4.32"
id("org.jetbrains.kotlin.plugin.allopen") version "1.4.32"
}

Step2: 配置allOpen插件

1
2
3
4
5
allOpen {
annotation("javax.ws.rs.Path")
annotation("javax.enterprise.context.ApplicationScoped")
annotation("io.quarkus.test.junit.QuarkusTest")
}

Step2: 添加kotlin依赖

1
2
3
4
5
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("io.quarkus:quarkus-kotlin")
...
}

这里的quarkus-kotlin用于支持quarkus的特殊特性,如liveEdit

参考

Android Gradle从Groovy迁移到Kotlin

如何将gradle中的groovy任务转换为Gradle Kotlin DSL以生成pom.xml?

Migrating build logic from Groovy to Kotlin

使用Gradle生成BOM管理依赖版本

The Java Platform Plugin

BUILDING QUARKUS APPS WITH GRADLE

QUARKUS - USING KOTLIN