spring boot 打包
阿新 • • 發佈:2017-11-30
ssi fas 必須 cat artifact 5.1 databind bind oid
打成jar直接運行比較簡單,這裏特別說明的是打成war包,部署在外部的tomcat方式:
1、修改啟動代碼
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication @EnableAutoConfiguration @ComponentScan("com.xx.xx") @Controller // mapper 接口類掃描包配置 public class Application extends SpringBootServletInitializer { @RequestMapping("/") String home() { return "index"; } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
2、修改pom.xml
<packaging>war</packaging>
去除一些可能沖突的配置項:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> <version>1.5.1.RELEASE</version> </dependency>
增加servlet(可能不是必須的)
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> </dependency>
tomcat外部依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>1.4.7.RELEASE</version> <scope>provided</scope> </dependency>
war包名稱:
<finalName>ROOT</finalName>
防止沒有web.xml報錯:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>
spring boot 打包