我了解,为了访问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.
如果将资源文件夹
static
和templates
移到src/main/webapp
,则应该在War归档文件中获得所需的结构。
关于spring - 在spring boot中gradle build(war)时,可以将静态文件放在根目录中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53772815/