. | . |
---|---|
Travis | |
Coveralls | |
Codecov |
Note that both a Gradle (build.gradle) and a Gradle Kotlin DSL (build.gradle.kts) build file are present.
This project aims to provide an example of the use of the Gradle Kotlin DSL with some testing frameworks and libraries, since the official documentation is at the moment of writing very minimal.
There are both a Groovy Gradle (build.gradle
) and a Kotlin Script Gradle (build.gradle.kts
) build file present which do roughly the same, which may make translation a bit easier.
For other translation examples, see below.
This is the Kotlin equivalent of this (older) java template project.
It makes use of
- Kotlin
- Kotlin Script Gradle or Gradle Kotlin DSL
- A normal, Groovy Gradle build file is also present
- JaCoCo for generating test reports
- Codecov.io which takes the JaCoCo report and shows some statistics
There are tests using
- kotlintest (will be run by Gradle since kotlintest 3)
- JUnit 5
- TestFX (for using JavaFX elements)
- Spek 2
Note you have to install JavaFX properly, making sure you install one for the Java version you are using (e.g. on Arch Linux, install jre8-openjdk jdk8-openjdk java8-openjfx
).
Decide whether you want to use Groovy Gradle (older and widely used) or the Gradle Kotlin DSL (newer but no documentation and nobody uses it, yet).
- Copy the
build.gradle.kts
andsettings.gradle.kts
(Gradle Kotlin DSL, as you can see from the.kts
extension) or thebuild.gradle
(Groovy Gradle, it this repo it may be namedbuild.gradle.inactive
but just rename it tobuild.gradle
) - Read through the file you just copied and change the relevant names, remove things of which you are sure you don't need them
- Copy the
gradlew
,gradlew.bat
andgradle/
files and folder. - Go to File | New | Module from Existing Sources and select your
build.gradle
file. Make sure 'Use gradle wrapper' is selected. Select a different Gradle JVM if prompted. - You should commit all the files you added, but not the generated files in
.gradle/
. - You can run Gradle tasks from the Gradle window on the right, for example
build
to build your project,run
to run the main class if you have one orcheck
to run tests. - It is recommended that you use a continuous integration service like Travis, which will build and run tests on their server when you push a commit. Install the Travis GitHub App by going to the GitHub marketplace, and after that copy the
.travis.yml
, possibly remove parts you don't need.
This was only tested with IntelliJ.
-
You can quickly import the project into IntelliJ by importing the
build.gradle.kts
file. Select the Gradle wrapper if needed. -
To run Gradle tasks, e.g. to run all tests (the verification/check task) or to start the main class (application/run) open the Gradle toolbar on the right and double-click (or right-click and debug) the task.
-
Alternatively, without IntelliJ, you can run
gradlew test
on the terminal to run the tests via gradle with the gradle wrapper (so you don't have to first install gradle locally by hand). -
If you see all gradle keywords greyed out you may want to link the Gradle project in IntelliJ, if you do not see a popup asking you to do so then go to modules - import module and select your
build.gradle.kts
.
Feel free to use GitHub emojis as for example by François Parmentier.
Groovy Gradle:
// This is a configuration for the shadowJar task.
shadowJar{
classifier = 'deploy'
}
Kotlin Script Gradle:
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
tasks {
// This is a configuration for the shadowJar task.
"shadowJar"(ShadowJar::class) {
classifier = "deploy"
}
}
Run kotlintest and JUnit 5 tests with Gradle
Upload test reports of Kotlin sources to Coveralls
The DefaultSourceDirectorySet constructor has been deprecated. How to use the ObjectFactory service?