war與war exploded區別

weixin_34087301發表於2018-01-15

在使用IDEA開發專案的時候,部署Tomcat的時候會出現這樣的情況:
是選擇war模式,還是war exploded模式。
這兩個模式有什麼區別呢?

war模式:將WEB工程以包的形式上傳到指定的伺服器(tomcat)
war exploded模式:將WEB工程以當前資料夾的位置關係上傳到伺服器(tomcat)。

  1. war模式:稱之為釋出模式,先打成war包,再發布;
  2. war exploded模式:是直接把資料夾、jsp頁面 、classes等等移到Tomcat 部署資料夾裡面,進行載入部署。因此這種方式支援熱部署,一般在開發的時候也是用這種方式。


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
@RestController
@RequestMapping("/")
public class HelloController {
    @PostConstruct
    private void init() {
        System.out.println("初始化:HelloController");
    }

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome() {
        // 獲取web專案的根路徑
        String path = this.getClass().getResource("/").getPath();
        return path;
    }
} 

通過上面的程式碼測試兩個部署方式,web專案的實際根目錄。
原生程式碼的測試結果是:

  1. war模式輸出:/Users/jessie/local/apache-tomcat-7.0.68/webapps/demod/WEB-INF/classes/
  2. war exploded模式輸出:/Users/jessie/project/demo/target/demod/WEB-INF/classes/

由輸出結果可以看出:

  1. war模式部署在指定的tomcat裡面的webapps下。
  2. war exploded模式在當前的專案的target裡面。

相關文章