2016-03-30 15 views
9

Tôi đã cố gắng đóng gói ứng dụng khởi động mùa xuân làm chiến tranh. Theo this, tôi biến đổi lớp ứng dụng của tôi:Yêu cầu web.xml để triển khai ứng dụng khởi động mùa xuân

@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); 
    } 
} 

Cũng được thêm vào sau trong pom.xml của tôi

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope> 
</dependency> 

Khi tôi đóng gói dự án, mặc dù tôi đã nhận lỗi sau:

[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] 

Như tôi đã đọc khi ứng dụng khởi động mùa xuân, tôi chưa bao giờ thấy bất kỳ điều gì về việc tạo một tệp web.xml. Web.xml có cần thiết trong việc triển khai ứng dụng khởi động mùa xuân là chiến tranh không?

+1

Bạn có thêm 'mùa xuân-boot-khởi-web 'là phụ thuộc? Để biết thêm thông tin, hãy xem: https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html#build-tool-plugins-maven-packaging – Aliaxander

+0

@Aliaxander có, nhưng tôi có nên chỉ định phạm vi như được cung cấp cho cái này không? – Nasreddin

+0

Không, bạn không nên. – Aliaxander

Trả lời

12

Theo this câu trả lời làm Maven bỏ qua web.xml vắng mặt bằng cách thêm đoạn mã sau để pom.xml của bạn:

<plugin> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.6</version> 
    <configuration> 
    <failOnMissingWebXml>false</failOnMissingWebXml> 
    </configuration> 
</plugin> 
0

Bạn không thực sự cần web.xml tệp để tạo WAR tạo tác sẵn sàng để được triển khai. Dưới đây là cách tôi tạo các tạo tác dựa trên Spring Boot dựa trên sử dụng Gradle.

build.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" 
} 

Để xây dựng WAR, ta nên chạy:

gradle war

+0

Tôi không quen với gradle. Làm thế nào để gói nó với maven? – Nasreddin

+0

@Nasreddin với maven, sử dụng 'mvn package' –

2

Bạn có phụ thuộc web.

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 

Bạn luôn có thể có web.xml nếu cần cấu hình, chỉ cần đặt tệp vào đúng thư mục trong WEB-INF để mùa xuân có thể đọc cấu hình. Cũng thay đổi bao bì để

<packaging>war</packaging> 

xem xét cũng sử dụng pom mẹ cho mùa xuân-boot như

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.2.RELEASE</version> 
    </parent> 

Cấu hình này

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope> 
</dependency> 

Chỉ nói với Maven không bao gồm phụ thuộc tomcat trong tập tin chiến tranh, để tránh can thiệp với nhà cung cấp servlet.

Các vấn đề liên quan