- Kotlin dsl with gradle 4.8
- Basic set up
- Code Coverage
- JUnit
- Jacoco
- Create code coverage task
- Ignore a class from coverage
- Execute the project
- With the Application gradle plugin
- Make the fat Jar
- Make the wrapper
- Saved searches
- Use saved searches to filter your results more quickly
- wadejensen/kotlin-fat-jar-example
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
Kotlin dsl with gradle 4.8
Kotlin dsl with gradle means to use the kotlin build.gradle.kts instead of the groovy build.gradle . Kotlin is so general that it reaches specific programming niche.
Basic set up
Basic set up for your kotlin project with Gradle > 4.8 (2018), may not apply to latest gradle releases:
allprojects group = "hello" version = "1.0" repositories jcenter() > >
You want to specify the kotlin version and plugin you wish to use:
plugins kotlin("jvm") version "1.3.21" // instead of id("org.jetbrains.kotlin.jvm") version "1.3.21" >
You don’t need to apply the plugin just define it, and you’re good to go. Add the basic repositories for your dependencies:
repositories mavenCentral() maven(url = "https://plugins.gradle.org/m2/") >
Add dependencies like kotlin:
dependencies compile(kotlin("stdlib-jdk8")) // Or compile("org.jetbrains.kotlin:kotlin-stdlib:1.3.21") >
The kotlin key word replaces org.jetbrains.kotlin you can also use it for plugins this way.
For higher version of gradle, compile has been deprecated to implementation
You can also add this for source compatibility:
tasks.withTypeKotlinCompile> kotlinOptions.jvmTarget = "1.8" >
Code Coverage
JUnit
To look at coverage that means you have unit test. Don’t forget to add something like JUnit into your dependencies:
dependencies testCompile("junit:junit:4.12") >
Also you want to use the junit platform to run your tests with google using:
tasks.test useJUnitPlatform() >
Which will use the junit-vintage-engine incompatible with the newer Junit5 version (you can recognize them by the jupiter in the package name), you may encounter the No tests found issue if you mix them up.
If you want to use org.junit.jupiter:junit-jupiter:5.7.2 with its junit-jupiter-api and junit-jupiter-engine, you might need to exclude junit4 modules like in springboot:
testImplementation("org.springframework.boot:spring-boot-starter-test") exclude(module = "junit") exclude(module = "junit-vintage-engine") >
With that the useJunitPlatform() will use the junit5 one.
Jacoco
Create code coverage task
Code coverage with jacoco plugin
tasks.withTypeJacocoReport> reports xml.isEnabled = true csv.isEnabled = false html.destination = file("$/reports/jacoco") > >
You can now roll the test code coverage with jacoco using gradle test jacocoTestReport .
Ignore a class from coverage
If you have a main class which can’t be tested and you’d rather remove it from coverage, you would do it like:
tasks.withTypeJacocoReport> doFirst classDirectories = fileTree("build/classes/kotlin/main").apply exclude("**/MainKt.class") > > // . your other stuff >
And MainKt is not considered for the coverage anymore!
Execute the project
With the Application gradle plugin
It is a plugin available with gradle, add it to your gradle script like:
Then set your application main file:
- It has to be outside a class
- The Kt at the end is normal, Kotlin automatically generates it for backward compatibility with Java classes
application mainClassName = "hello.MainKt" > // Or you can use application.mainClassName = "hello.MainKt"
Now you can run your program with:
Make the fat Jar
So the basic Jar file generated doesnt include all you need to run. You need to include them manually by adding this (gradle 5+):
tasks.withTypeJar> // Otherwise you'll get a "No main manifest attribute" error manifest attributes["Main-Class"] = "com.example.MainKt" > // To add all of the dependencies otherwise a "NoClassDefFoundError" error from(sourceSets.main.get().output) dependsOn(configurations.runtimeClasspath) from( configurations.runtimeClasspath.get().filter it.name.endsWith("jar") >.map zipTree(it) > >) >
The jar will be created as -.jar like hello-kotlin-1.0.jar . You can then run it using:
java -jar hello-kotlin-1.0.jar
You can also create another task fatJar that would create the jar with all of your dependencies. Follow the documentation on gradle.
Make the wrapper
For your project to work almost anywhere, you can use the wrapper:
# To use the installed gradle version as wrapper gradle wrapper # To specify the gradle version gradle wrapper --gradle-version 4.8 --distribution-type all
Then you’ll be able to use ./gradlew instead of gradle, and your project should run fine 👍
Now if you have the opportunity to use a newer version of gradle, do it.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Building an executable fat jar in Kotlin Gradle DSL with a default main class and arbitrary alternate main classes
wadejensen/kotlin-fat-jar-example
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Kotlin build fat jar in Gradle DSL
To build a fat jar which includes all Kotlin project dependencies, we need to add a «fatJar» task as a dependency to the gradle build step.
Add the following to your build.gradle.kts file:
import org.gradle.jvm.tasks.Jar val fatJar = task("fatJar", type = Jar::class) < baseName = "$ -fat" // manifest Main-Class attribute is optional. // (Used only to provide default main class for executable jar) manifest < attributes["Main-Class"] = "example.HelloWorldKt" // fully qualified class name of default main class > from(configurations.runtime.map(< if (it.isDirectory) it else zipTree(it) >)) with(tasks["jar"] as CopySpec) > tasks < "build" < dependsOn(fatJar) >>
baseName is the name of the resulting fat jar. If a project is called hello-kotlin , the default skinny jar is hello-kotlin.jar . The fat jar will in turn be named hello-kotlin-fat.jar in this example.
To change the $ variable you can override it in settings.gradle.kts
rootProject.name = "hello-kotlin"
Then run gradle build from the project root to compile the project and create the jar artifact. By default it is created under: «build/libs/project-name-fat.jar»
To run the default main class in the fat jar file:
> java -jar build/libs/hello-kotlin-fat.jar Hello, world!
To run another main class:
> java -cp build/libs/hello-kotlin-fat.jar example.GoodbyeWorldKt Goodbye, world!
Note that because Kotlin does not require a main function to be declared within a class, the class name for the function in GoodbyeWorld.kt is just the filename with ‘Kt’ appended.
How does the dependency packaging work?
Black magic. Sorry, I was just starting with Kotlin and Gradle when this was written. It seems to recursively copy directories of runtime dependencies into the output jar artifact.
About
Building an executable fat jar in Kotlin Gradle DSL with a default main class and arbitrary alternate main classes