【发布时间】:2019-02-27 21:55:59
【问题描述】:
我尝试在独立的 Tomcat 7.0.91 上部署 Gradle + Spring Boot 2 + Angular2 应用 WAR 文件。 如果我使用提供的 jar 和嵌入式 tomcat 启动应用程序,一切正常,但是当我进入“/springboot”上下文时在独立容器上部署时,它给了我 404:“源服务器没有找到目标资源的当前表示或者是不愿意透露它的存在。”来自 tomcat 的日志没有说明任何有用的信息。 通过在 server.xml 上下文路径和文档库中设置,我尝试了 2 种部署方法:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context path="/springboot" docBase="/home/user/SideProjects/springboot-angular2-gradle-war/build/libs"/>
</Host>
第二个是在 webapps 目录中“手动”部署 war 文件。
我不确定,但 tomcat 中的文件 context.xml 设置为监视 WEB-INF/web.xml 中的资源,但据我所知,spring boot 不再使用 web.xml,所以可能是这种情况?不幸的是,我发现的每个教程都没有说明任何有关更改 context.xml 的内容,而且我现在找不到 spring 中的哪些资源被视为部署描述符。
这是我的 build.gradle 文件:
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
apply plugin: 'application'
mainClassName = "com.demo.SpringbootAngular2GradleWarApplication"
group = 'com.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
war {
baseName = "demo-spring"
}
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
def frontendDir = "$projectDir/src/main/frontend"
def frontDistDir = "$frontendDir/dist"
sourceSets {
main {
resources {
srcDirs = ["$frontDistDir", "$projectDir/src/main/resources"]
}
}
}
clean.doFirst {
delete "$frontDistDir"
println "$frontDistDir - cleaned"
}
task buildAngular(type: Exec) {
workingDir "$frontendDir"
inputs.dir "$frontendDir"
commandLine "npm", "run-script", "build"
}
processResources.dependsOn "buildAngular"
主类扩展了 servlet 初始化器:
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringbootAngular2GradleWarApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootAngular2GradleWarApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootAngular2GradleWarApplication.class, args);
}
}
【问题讨论】:
-
你的应用程序被命名为
demo-spring,所以在部署战争时,url 将类似于http://localhost:8080/demo-spring-0.0.1-SNAPSHOT。 -
不幸的是,事实并非如此。我不知道为什么,但是这种更改名称的方式不起作用,并且 gradle 仍然会生成默认名称: springboot-angular2-gradle-war-0.0.1-SNAPSHOT.war 无论如何,上下文设置中的“路径”属性设置名称 i想要
-
如果它指向某个有效的位置。将它指向包含战争的目录不会做任何事情。此外,在放弃战争时,上下文路径将被忽略,它将
springboot-angular2-gradle-war-0.0.1-SNAPSHOT作为路径。但是我建议在 META-INF 文件夹 (AFAIK) 的战争中包含一个 context.xml 来设置路径。 -
我到底应该在 context.xml 中设置什么?我认为它应该在 XmlConfiguration 类中完成?在 webapps 中删除文件而不重命名它,所以我使用全名作为上下文路径,仍然导致 404
标签: java spring-boot tomcat gradle