Avalon A Programmer Blog

Create executable artifact from Kotlin project

As Kotlin is a much better language to write than Java (it also compatible with Java), I try to write a library in Kotlin and compile as a artifact(.jar). My project is a Gradle project and use Intellij IDEA as IDE. However, it is difficult to find a tutorial that works.

Intellij IDEA

Intellij IDEA has a built-in way to build a artifact. By going File -> Project Structure... -> Artifacts -> + -> JAR -> From modules with dependencies, you add a artifact build to your project and you can build from Build -> Build Artifacts -> Build. However, I do not know why but it does not include resources folder in the artifact. Also, the default location for META-INF/MANIFEST.MF is incorrect.

Application Plugin

Some suggest to use the application plugin in Gradle. After changing you build.gradle, you will have some new task.

apply plugin:'application'
mainClassName = "com.example.MainKt" // It has a "Kt" at the end

After running distZip, it generate a zip file with contain all the dependencies and the starting script. However, the .jar is NOT compile to a single file.


This is the only way that I found which meet my needs. You have to add the following code to build.gradle.

jar {
    manifest {
        attributes "Main-Class": "io.joshuaavalon.dashboard.MainKt"

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

The last line is important without it the jar will mess up.