【问题标题】:Gradle is not place WebJar contents into the correct place in my Jar fileGradle 没有将 WebJar 内容放入我的 Jar 文件中的正确位置
【发布时间】:2018-04-02 13:54:19
【问题描述】:

我有一个基本的 Java 应用程序,我想在其中使用 WebJars。我使用 Gradle 作为我的构建系统。我想将 WebJars 用于 Bootstrap 和 JQuery,这样我就可以在我的 Spring-Boot/ThymeLeaf 应用程序中轻松引用和更新它们。该应用程序基本上是位于here的表单教程中的应用程序

据我了解,Gradle 应该将 WebJars 中的所有文件放入我的 Jar 文件中的 META-INF 文件夹中。如果我正确理解所有内容,那么当我在我的 html 页面中引用以/webjars/ 开头的内容时,Spring-Boot 资源处理程序将从META-INF/ 加载资源

不幸的是,这不起作用(还)。因为我在 Tomcat 的日志输出中看到资源处理程序已正确安装。我决定检查这些文件是否真的在我的 Jar 文件中。

当我提取我的 Jar 文件时,META-INF 文件夹只有一个名为 MANIFEST.MF 的文件,其中包含有关 Spring Boot 的一些信息。 BOOT-INF/lib 中有一个 BootStrap-3.3.7.Jar 和一个 JQuery-3.2-1.Jar 文件,但我认为这不是它们应该结束的地方。 (或者我错了,资源处理程序中的某个地方有错误吗?)。

当我运行 gradle build 时,如何告诉 gradle 对这些文件做正确的事情?

我的 gradle.build 文件如下所示:

buildscript {
    ext {
        springBootVersion = '1.5.8.RELEASE'
    }
    repositories {
        mavenCentral()      
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")        
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: "jacoco"

jar {
    baseName = 'gs-serving-web-content'
    version =  '0.0.1'
}

bootRun {
    addResources = true
}

repositories {
    mavenCentral()  
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.webjars:jquery:3.2.1")
    compile("org.webjars:bootstrap:3.3.7")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}   

【问题讨论】:

    标签: java spring-boot gradle webjars


    【解决方案1】:

    Gradle 的做法是正确的,因为 jar 应该打包在 BOOT-INF/lib 中。然后将BOOT-INF/lib 中每个 jar 的根添加到类路径中,从中可以找到 META-INF 内容中的每个与 webjar 相关的内容。

    我建议您再问一个问题,重点关注您的应用程序在运行时所做的事情。据我所知,在构建时一切正常。

    【讨论】:

    • 感谢您的回答。你是对的,gradle 正在做正确的事情。似乎其他人对此感到困惑,这误导我相信 gradle 有错。事实证明,它只是 Eclipse 起作用了。从控制台构建可以正确完成所有操作。 :/。可能几次重新启动将使 Eclipse 恢复正常。
    • 是的,重新启动 Eclipse + 从 Eclipse 刷新 gradle 项目,然后搞乱启动配置(不知何故出错)修复了它。 ://
    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2016-07-19
    • 2014-06-17
    相关资源
    最近更新 更多