Java 部署 Spring Boot 应用程序是否需要 web.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36315591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is web.xml required to deploy a spring boot application
提问by Nasreddin
I was trying to package a spring boot application as a war. According to this, I modified my Application class:
我试图将 Spring Boot 应用程序打包为一场战争。根据这个,我修改了应用程序类:
@SpringBootApplication
@EntityScan({"org.mdacc.rists.cghub.model"})
@EnableJpaRepositories(basePackages = {"org.mdacc.rists.cghub.ws.repository"})
public class Application extends SpringBootServletInitializer
{
public static void main( String[] args )
{
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Also added the following in my pom.xml
还在我的 pom.xml 中添加了以下内容
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
When I package the project though, I got the following error:
但是,当我打包项目时,出现以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project cg-web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
As I was reading upon spring boot application, I never saw anything about creating a web.xml. Is web.xml required in deploying spring boot application as war?
当我阅读 Spring Boot 应用程序时,我从未看到任何关于创建 web.xml 的内容。将 Spring Boot 应用程序部署为 war 时是否需要 web.xml?
采纳答案by Aliaxander
According to thisanswer make Mavenignore the web.xmlabsence by adding the following snippet to your pom.xml:
根据这个答案,通过将以下代码段添加到您的pom.xml ,使Maven忽略web.xml 的缺失:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
回答by Aliaxander
You don't actually need web.xml
file to create WAR
artifact ready to be deployed.
Here is how I build Spring Boot
-based artifacts using Gradle.
您实际上不需要web.xml
文件来创建WAR
准备部署的工件。这是我如何Spring Boot
使用Gradle构建基于工件的方法。
build.gradle:
构建.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE"
}
}
apply plugin: 'war'
apply plugin: 'spring-boot'
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web:1.3.3.RELEASE"
//Allows to run spring boot app on standalone Tomcat instance
providedRuntime "org.springframework.boot:spring-boot-starter-tomcat:1.3.3.RELEASE"
}
In order to build WAR
, one should run:
为了构建WAR
,应该运行:
gradle war
gradle war
回答by Koitoer
Do you have the web dependencies.
你有网络依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
You can always have your web.xml if you need some configuration on it, just place the file in the correct folder within WEB-INF so spring can take it an read the configurations. Also change the packaging to
如果你需要一些配置,你总是可以拥有你的 web.xml,只需将文件放在 WEB-INF 中的正确文件夹中,以便 spring 可以读取配置。也将包装更改为
<packaging>war</packaging>
Consider as well use the parent pom for spring-boot as
考虑使用父 pom 作为 spring-boot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
This configuration
这个配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Only tell maven not to include tomcat dependencies in the war file, to avoid interference with the servlet provider.
只告诉maven不要在war文件中包含tomcat依赖,避免干扰servlet provider。
回答by osama yaccoub
In servlet 2.5 specs (java EE 5) , web.xml is mandatory, in servlet specs 3+ (java EE 6) , you can remove web.xml and use annotation configuration instead
在 servlet 2.5 规范 (java EE 5) 中,web.xml 是必需的,在 servlet 规范 3+ (java EE 6) 中,您可以删除 web.xml 并使用注释配置代替