无法从Gradle应用程序创建可执行的fat jar。

huangapple 未分类评论43阅读模式
英文:

Unable to create a fat jar from Gradle application

问题

我在从Gradle应用程序创建一个fat jar时遇到了困难。

我运行了./gradlew jar./gradlew clean build命令。这两个命令似乎在应用程序的各个子模块中创建了有效的.jar文件,但是在项目根目录中的.jar文件只包含了MANIFEST.MF文件,没有其他内容。

应用程序的结构如下所示:

  • nova-app:这是包含main方法和Dropwizard的config.yml文件的项目
  • nova-dal
  • nova-core
  • build.gradle
  • Procfile
  • settings.gradle
  • ... 其他不相关的文件/文件夹

build.gradle(在根项目中)

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

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

    maven-publish

    // 应用程序插件以添加构建CLI应用程序的支持。
    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(在主类所在的nova-app项目中)

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'

我希望能够使用类似于java -jar build/libs/nova-1.0.0.jar的命令运行应用程序,但目前我得到了Caused by: java.lang.ClassNotFoundException: com.nova.app.Nova的错误。

有人能指出这个设置中出了什么问题吗?

英文:

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(&quot;jvm&quot;) version &quot;1.3.72&quot;

    `maven-publish`

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

application {
    mainClassName = &quot;com.nova.app.Nova&quot;
}

tasks.withType&lt;Jar&gt; {
    enabled = true

    manifest {
        attributes[&quot;Main-Class&quot;] = application.mainClassName
    }

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

allprojects {
    group = &quot;com.nova&quot;
    version = &quot;1.0.0&quot;

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

    tasks.withType&lt;KotlinCompile&gt;().configureEach {
        kotlinOptions.jvmTarget = &quot;1.8&quot;
        kotlinOptions.javaParameters = true
    }
}

tasks.register(&quot;stage&quot;) {
    dependsOn(&quot;:clean&quot;, &quot;:build&quot;)
}

subprojects {
    apply(plugin = &quot;kotlin&quot;)
    apply(plugin = &quot;jacoco&quot;)
    apply(plugin = &quot;maven-publish&quot;)

    tasks.withType&lt;Test&gt;().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(&quot;org.springframework.boot&quot;) version &quot;2.1.3.RELEASE&quot;
}

tasks.withType&lt;Jar&gt; {
    enabled = true
}

tasks.withType&lt;BootJar&gt; {
    archiveFileName.set(&quot;${this.archiveBaseName.get()}.${this.archiveExtension.get()}&quot;)
}

dependencies {
    &quot;implementation&quot;(project(&quot;:nova-core&quot;))
    &quot;implementation&quot;(project(&quot;:nova-dal&quot;))
}

settings.gradle

rootProject.name = &#39;nova&#39;

include &#39;:nova-app&#39;
include &#39;:nova-core&#39;
include &#39;:nova-dal&#39;

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?

huangapple
  • 本文由 发表于 2020年7月25日 23:37:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/63090232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定