In addition to executable jar packages, Spring Boot also supports traditional war packages. This article describes how to build traditional war packages using Spring Boot.
The steps for Spring Boot to use war package are as follows:
1. Define the packaging type in pom.xml
<packaging>war</packaging>
2. Add Spring Boot Launcher (can also be through parent)
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependency> </dependency> </dependencyManagement>
3. Add spring-boot-starter-web dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
4. Add packaged plug-ins
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
5. Main class inherits SpringBootServletInitializer
/** * WAR application */@SpringBootApplicationpublic class WarApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(WarApplication.class, args); }}6. Execute mvn clean package
$mvn clean package
7. Copy the finished war package to a container (such as tomcat) and run.
Here we need a brief explanation:
The main application can override the SpringBootServletInitializer with the configure method and customize the configuration of Spring Boot.
/** * Configure the application. Normally all you would need to do is to add sources * (eg config classes) because other settings have sensible defaults. You might * choose (for instance) to add default command line arguments, or set an active * Spring profile. * @param builder a builder for the application context * @return the application builder * @see SpringApplicationBuilder */ protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder; } Download the instance source code
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.