spring - 在spring boot中gradle build(war)时,可以将静态文件放在根目录中吗?

标签 spring apache spring-boot gradle static

我了解,为了访问Web服务器(Apache)中的静态文件,静态文件必须位于WEB-INF之外。但是,带有gradle的Spring boot starter项目会将静态文件构建到WEB-INF中。没有搜索可以找到将在WEB-INF之外构建静态文件的设置。我不知道我有什么问题。我需要一些建议。

以下是为测试而创建的基本spring boot starter项目。

环境

Spring 启动2.1.1,Java 1.8

布局

src
  └ main
        └ java
            └ ...
        └ resources
            └ static
            └ templates
            └ application.properties
  └ test

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

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.session:spring-session-core')
    compileOnly('org.projectlombok:lombok')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

AS-IS war 结构
└META-INF
└org
└WEB-INF
    └classes
        └com
        └static
        └templates
        └application.properties
    └lib
    └lib-provided

TO-BE war 结构
└META-INF
└org
└resources
    └static
    └templates
└WEB-INF
    └classes
        └com
        └application.properties
    └lib
    └lib-provided

谢谢。

最佳答案

请在War Plugin - default settings上查看war Gradle插件文档:

The default behavior of the War task is to copy the content of src/main/webapp to the root of the archive. Your webapp directory may of course contain a WEB-INF sub-directory, which may contain a web.xml file. Your compiled classes are compiled to WEB-INF/classes. All the dependencies of the runtime 1 configuration are copied to WEB-INF/lib.


如果将资源文件夹statictemplates移到src/main/webapp,则应该在War归档文件中获得所需的结构。

关于spring - 在spring boot中gradle build(war)时,可以将静态文件放在根目录中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53772815/

相关文章:

Spring Boot Jersey 和监控 URL

java - 当配置属性缺少前缀时,使映射为空且不为空

python - 我可以使用 mod_wsgi 使用 apache 部署 python 2 和 3 django 应用程序吗?

java - 部署spring boot应用程序的正确方法是什么

java - Spring Boot 和@ConfigurationProperties

Apache ssl 裸域找不到 VirtualHost

java - “字段需要一个无法找到的 bean。”使用mongodb的错误spring restful API

java - 从 tomcat sprinf 应用程序获取 pdf 文件时不允许加载本地资源

java - 获取Spring boot中当前代理的引用

Apache 不能同时使用 SSL 运行两个虚拟主机