I start to use gradle and replace maven, and want to share my opinion and learnings
Reason
- FI saw many of the excellent colleagues use gradle and not maven. I feel like using maven is outdated
2. maven is verbose comparing to kotlin DSL with gradle. As I write kotlin, using kotlin to write the dependency and the build cycle is very concise
3. gradle works with Android development and is the official tool. I have a plan to learn how to develop Android application
Learning
- There is an auto convert tool provided by gradle. Step-by-step guide: https://docs.gradle.org/current/userguide/migrating_from_maven.html#migmvn:automatic_conversion
You still need to setup the version number that is commonly used manually, and also replace the maven plugin with gradle plugin. In my case I replace avro plugin with the gradle one - Create a file called `gradle.properties` in ~/.gradle just like ~/.mvn, and put the nexus access information
- Modify your Dockerfile, because the jar is in /build, and not in /target (If you use jib, I think it’s more transparent )
- When you google for some answers, note that buildScript is the old syntax
- Sample project https://github.com/gradle/kotlin-dsl-samples
- To use Kotlin DSL, remember to name the file as `build.gradle.kts`
My gradle config
The gradle config structure is different than in mvn. There are gradle files for root and for src. The good thing is you can use Kotlin DSL to generate some documents that is not related to src in the documentation folder
gradle-example - build.gradle.kts
- gradle-example - build.gradle.kts
- src - ...
- documentation
Have a look at : https://github.com/HungUnicorn/gradle-example
The build.gradle.kts file that puts along side with the src folder I’m using looks as the following:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val jacksonVersion: String by project
val kotlinVersion: String by project
val testContainerVersion: String by project
plugins {
application
id("org.springframework.boot") version "2.4.0"
kotlin("jvm") version "1.4.20"
kotlin("plugin.spring") version "1.4.20"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
id("maven-publish")
jacoco
id("com.gorylenko.gradle-git-properties") version "2.2.3"
id("com.github.davidmc24.gradle.plugin.avro") version "1.0.0"
}
application {
mainClassName = "org.MyApplicationKt"
}
group = "org.my"
version = "DEV-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
gradlePluginPortal()
maven { url = uri("https://repo.spring.io/milestone") }
jcenter()
maven { url = uri("https://packages.confluent.io/maven/") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.security:spring-security-config")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.security:spring-security-oauth2-client")
implementation("org.springframework.security:spring-security-oauth2-jose")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.kafka:spring-kafka")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")
implementation("javax.servlet:javax.servlet-api:4.0.1")
implementation("io.springfox:springfox-boot-starter:3.0.0")
implementation("org.apache.avro:avro:1.10.1")
implementation("io.confluent:kafka-avro-serializer:5.3.0")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
implementation("io.github.microutils:kotlin-logging:1.8.3")
runtimeOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")
testImplementation("com.github.tomakehurst:wiremock-jre8:2.27.2")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("io.mockk:mockk:1.10.0")
testImplementation("com.ninja-squad:springmockk:2.0.2")
testImplementation("org.testcontainers:junit-jupiter:$testContainerVersion")
testImplementation("org.testcontainers:mongodb:$testContainerVersion")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")
testImplementation ("io.rest-assured:rest-assured:3.3.0") {
exclude(group = "com.sun.xml.bind", module = "jaxb-osgi")
}
}
jacoco {
toolVersion = "0.8.6"
}
tasks.withType<Test> {
useJUnitPlatform()
finalizedBy(tasks.jacocoTestReport)
}
tasks.jacocoTestReport {
reports {
csv.isEnabled = true
xml.isEnabled = true
}
}
springBoot {
buildInfo()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
}
}
avro {
stringType.set("String")
}