-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert project to Gradle+Kotlin #2
Conversation
Pro-Tip: add a `Gradle scope` with the pattern `file:*.gradle||file:*.kts||file[buildSrc]:*/||file:.gitlab-ci.yml||file:*.properties`, it's immensly useful when you are messing with your gradle builds. https://youtrack.jetbrains.com/issue/IDEA-200335 Action: `Replace in path` Scope: `Gradle` Replace all single quote ['] by a double quote [""]
Use the plugins {} block plugins { application kotlin("jvm") } Groovy has no "=" but Kotlin needs it for things like group = "me.rozkmin.testing" version = "1.0-SNAPSHOT" Groovy needs less parenthesis that Kotlin so add them as well testCompile Libs.junit => testCompile(Libs.junit) Do kotlinOptions.jvmTarget = "1.8" for all projects in the root build file
That was non-trivial: The Kotlin equivalent of test { useJUnitPlatform() } is tasks.withType<Test>().configureEach { this.useJUnitPlatform() }
Just more of the same plugins parenthesis =
Groovy: test { useJUnitPlatform() testLogging { events "passed", "skipped", "failed" } } Kotlin: tasks.withType<Test>().configureEach { useJUnitPlatform() testLogging { // GROOVY: // events "passed", "skipped", "failed" events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED) } }
Thats nice! |
One module can have either have one. |
Yeah, that's a good solution. Project built fully in Kotlin and Groovy files with .Groovy sufix excluded from build. It also requires proper explanation in readme. |
# Conflicts: # settings.gradle
@rozkminiacz Done[1] I even added a script Please check if you can merge [1] Github has some problems today so it may not appear so |
Looks good! |
jcenter() | ||
} | ||
|
||
tasks.withType<KotlinCompile>().configureEach { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need configureEach
in this case
} | ||
|
||
repositories { | ||
maven { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can replace it with maven(("https://dl.bintray.com/spekframework/spek-dev")
|
||
const val junit: String = "4.12" // up-to-date | ||
|
||
const val junit_jupiter_api: String = "5.3.1" // up-to-date |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you decide to violate Kotlin style guide and do not use camelCase for constants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, I forgot to reply. I follow the Kotlin style guide by default but in this case, I decided to that it was more important to be closer to what the maven coordinate is.
/** | ||
* [kotlin-stdlib-jdk8 website](https://kotlinlang.org/) */ | ||
const val kotlin_stdlib_jdk8: String = | ||
"org.jetbrains.kotlin:kotlin-stdlib-jdk8:" + Versions.kotlin_stdlib_jdk8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use string templates?
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
mavenCentral() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need both mavenCentral and jcenter? jcenter is superset of maven central
} | ||
|
||
apply(plugin = "java") | ||
java { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather to use plugins.withType
, so you could apply java plugin specific settings only if some of modules applied java plugin, and do not force to apply it
} | ||
|
||
dependencies { | ||
implementation(project(":application")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also use approach when all our modules are items of enum, so access to modules became type safe, I probably share sample project that uses this approach
} | ||
|
||
// GROOVY: test { ... } | ||
tasks.withType<Test>().configureEach { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, you don't need configureEach
If you want to configure only task "test", not all tasks with this type (same is in groovy), instead you can use tasks.named("test") {}
(not sure that available before Gradle 5, than you can use some other DSL to get task, such as getByName or delegates)
While I was doing this small pull request earlier, I realized that your project was exactly the playground I needed (neither trivial not too large, with tests) for something I had in mind :
Showing by example how to convert a Gradle project to the Kotlin DSL.
It will be a follow-up to this article I wrote and I plan to show it at a Kotlin Meetup in Berlin.
https://blog.kotlin-academy.com/gradle-kotlin-the-missing-piece-of-the-puzzle-7528a85f0d2c
If you prefer to stay with Gradle/Groovy, no hard feeling :)
It should work fine with
Intellij IDEA 2018.+
andAndroid Studio 3.2+
, tell me if you encounter problems.