Spring Boot (25) - playing war package

Fight the war package

Usually play when using Spring Boot is a jar package, if the development of Web applications is also commonly used built-in Web container, such as Tomcat. If you want to make war package that how should I do it?

1. First need to Maven's pom.xml in the package changed to war.

<packaging>war</packaging>

2. Remove the spring-boot-maven-pluginplug, because we do not need a repackage.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

3. When playing war built-in Web-related container jar is no longer needed, it has an impact when deployed to the outside of the container to avoid. So when we use the built-in Tomcat, we need to adjust the scope of dependent Tomcat is provided.

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

4. Adjust the Java Application startup class so that it inherits SpringBootServletInitializer, configure and override its method, the call reference sources SpringApplicationBuilder method of rewriting, configuration class and pass as a parameter, such as follows. This will continue to run the Java Application during development, deployment time hit war package deployment, the deployment package to fight war will eventually start to relevant content Spring Boot Application of the following classes start on Servlet3 of ServletContainerInitializer specification.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(Application.class);
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}

Spring Boot deployed by more than a few applications can be labeled as war package to external Web container, run out of the jar packaging operation with effect effect is the same.

(Note: This article is written based on Spring Boot 2.0.3)

Guess you like

Origin elim.iteye.com/blog/2441922