提问者:小点点

当 gradle build(war) 在 Spring 启动时,可以将静态文件放在根目录中吗?


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

以下是为测试而创建的基本spring-boot启动程序项目。

环境

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战争结构

└META-INF
└org
└WEB-INF
    └classes
        └com
        └static
        └templates
        └application.properties
    └lib
    └lib-provided

未来战争结构

└META-INF
└org
└resources
    └static
    └templates
└WEB-INF
    └classes
        └com
        └application.properties
    └lib
    └lib-provided

谢谢。


共1个答案

匿名用户

请在此处查看< code>war Gradle插件文档war插件-默认设置:

War 任务的默认行为是将 src/main/webapp 的内容复制到存档的根目录。您的 Web 应用程序目录当然可能包含一个 WEB-INF 子目录,该子目录可能包含一个 web.xml 文件。编译的类将编译为 WEB-INF/类。运行时 1 配置的所有依赖项都复制到 WEB-INF/lib。

如果将资源文件夹静态模板移动到 src/main/webapp 中,您应该会在 War 存档中获得预期的结构。