Spring Boot war package instance tutorial, springwar
In addition to executable jar packages, Spring Boot also supports traditional war packages. This article describes how to use Spring Boot to build a traditional war package.
Follow these steps to create a war package for Spring Boot:
1. Define the packaging type in pom. xml
<packaging>war</packaging>
2. Add the Spring Boot starter (you can also use 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> </dependencies> </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 packaging plug-ins
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
5. The main class inherits SpringBootServletInitializer
/** * WAR application */@SpringBootApplicationpublic class WarApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(WarApplication.class, args); }}
6. Run mvn clean package to package
$mvn clean package
7. Copy the prepared war package to a container (such as tomcat) and run it.
Here we need to briefly describe:
The main application can rewrite SpringBootServletInitializer with the configure method to customize Spring Boot configuration.
/** * Configure the application. Normally all you would need to do is to add sources * (e.g. 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 instance source code
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.