1

I'm having a hard time creating a fat jar from my Gradle app.

I'm running commands ./gradlew jar and ./gradlew clean build. Both commands seem to create valid .jars in the respective sub-modules of the app, but the .jar in the root of the project only contains the MANIFEST.MF and nothing else.

The structure of the app looks like following

  • nova-app ; this is the project where the main method is and Dropwizard's config.yml file
  • nova-dal
  • nova-core
  • build.gradle
  • Procfile
  • settings.gradle
  • ... other irrelevant files/folders

build.gradle (in the root project)

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.net.URI

plugins {
    base
    kotlin("jvm") version "1.3.72"

    `maven-publish`

    // Apply the application plugin to add support for building a CLI application.
    java
    application
}

application {
    mainClassName = "com.nova.app.Nova"
}

tasks.withType<Jar> {
    enabled = true

    manifest {
        attributes["Main-Class"] = application.mainClassName
    }

    from(configurations.runtimeClasspath.get().map {if (it.isDirectory) it else zipTree(it)})
}

allprojects {
    group = "com.nova"
    version = "1.0.0"

    repositories {
        mavenCentral()
        maven { url = URI("https://plugins.gradle.org/m2/") }
    }

    tasks.withType<KotlinCompile>().configureEach {
        kotlinOptions.jvmTarget = "1.8"
        kotlinOptions.javaParameters = true
    }
}

tasks.register("stage") {
    dependsOn(":clean", ":build")
}

subprojects {
    apply(plugin = "kotlin")
    apply(plugin = "jacoco")
    apply(plugin = "maven-publish")

    tasks.withType<Test>().configureEach {
        useJUnitPlatform()
    }

    dependencies {
       ...
    }

    configurations.all {
        resolutionStrategy.preferProjectModules()
    }

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
    }
}

build.gradle (in the nova-app project where the main class is)

import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
    id("org.springframework.boot") version "2.1.3.RELEASE"
}

tasks.withType<Jar> {
    enabled = true
}

tasks.withType<BootJar> {
    archiveFileName.set("${this.archiveBaseName.get()}.${this.archiveExtension.get()}")
}

dependencies {
    "implementation"(project(":nova-core"))
    "implementation"(project(":nova-dal"))
}

settings.gradle

rootProject.name = 'nova'

include ':nova-app'
include ':nova-core'
include ':nova-dal'

I'd like to be able to run the app with the command like java -jar build/libs/nova-1.0.0.jar, but currently I'm getting Caused by: java.lang.ClassNotFoundException: com.nova.app.Nova

Could someone point me to what is wrong in this setup?

Whizzil
  • 1,264
  • 6
  • 22
  • 39

0 Answers0