1. 程式人生 > >springboot專案在eclipse下打包成war包

springboot專案在eclipse下打包成war包

1 在pom.xml檔案的配置

    a 去除tomcat依賴  

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

   b 去除servlet,jstl,jsp依賴

        <!--servlet依賴 -->
		<dependency>
    		<groupId>javax.servlet</groupId>
   			<artifactId>javax.servlet-api</artifactId>
    		<version>3.1.0</version>
    		<scope>provided</scope>
		</dependency>

		<!--jstl依賴 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<scope>provided</scope>
		</dependency>

		<!--使jsp頁面生效 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>

2 提供外部tomcat執行的入口方法

public class StarterForTomcat extends SpringBootServletInitializer{
		//覆蓋父類方法,將入口指向springboot的main
	@Override
	protected SpringApplicationBuilder 
	configure(SpringApplicationBuilder builder) {
		//引數Builder就是容器建立整個spring容器的起始物件,也就是main方法的類
		return builder.sources(StartApplicationTestJsp.class);
	}
}

3 main方法啟動類

@SpringBootApplication
public class StartApplicationTestJsp {
	public static void main(String[] args) {
		SpringApplication.run(StartApplicationTestJsp.class, args);

	}	

}

4 eclipse打包

   a 右擊專案——》Run As——》Maven build

   

b 填寫goals和去掉skin tests,最後選擇run

e 在專案target就可以看到打好的war包了