eclipse使用maven創建web3.0項目

挺彆扭的,eclipse和maven發展了這麼久,二者都沒有很好的解決這個問題。

默認情況下,使用maven骨架撞見的webapp只支持servlet2.3,eclipse又不允許隨便修改爲3.0。

網上流傳最多的版本可能是修改項目目錄中.settings/org.eclipse.wst.common.project.facet.core.xml文件,然後還要注意一堆東西。

我梳理了一種辦法,感覺處理起來相對合理,如下:

1.創建普通的webapp項目

點擊菜單“File - New - Other - Maven - Maven Project”;

Next;

Next;

Catalog 選擇"Internal",選中最後一個骨架"maven-archetype-webapp","Next";

輸入你自己的groupId,artifactId,"Finished"

2.添加maven插件

右鍵點擊項目名,選擇“Maven - Add Plugin”;

在搜索框輸入"maven-compiler-plugin",選中"org.apache.maven.plugins"開頭的插件,"OK";

在搜索框輸入"maven-eclipse-plugin",選中"org.apache.maven.plugins"開頭的插件,"OK";

此時插件只是插入了基本模板:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.6.0</version>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-eclipse-plugin</artifactId>
  <version>2.10</version>
</plugin>

3.調整依賴

非必須,只是建議。

雙擊打開pom.xml,並切換到"Dependencies"選項卡;

建議:選中junit 3.8.1,點擊"Properties",版本改爲"4.12";

點擊"Add",在搜索框輸入javax.servlet-api,選擇"javax.servlet"開頭的依賴,並調整版本爲"3.1.0",scope改爲"provided","OK"

以上基本上依靠eclipse就可以實現,不需要直接修改pom。這裏只是列出xml片段:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

4.調整第二步中的插件配置

調整compiler插件

`</plugin>`上面插入一個空行;

按"Alt + /",選擇"configuration"插入節點;

在"configuration"節點內重複上一步,插入encoding=utf8,source=1.8,target=1.8;

提示:如果你能記住這幾個單詞,而且又安裝了emmet的話,不放輸入"configuration>encoding{UTF-8}+source{1.8}+target{1.8}"按下Tab看看。

調整eclipse插件

同上,增加"configuration"及下面的節點wtpversion=2.0,jeeversion=6.0;

你還想知道emmet表達式?好吧,"configuration>wtpversion{2.0}+jeeversion{6.0}"

最終插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
      <encoding>UTF-8</encoding>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.10</version>
    <configuration>
      <wtpversion>2.0</wtpversion>
      <jeeversion>6.0</jeeversion>
    </configuration>
  </plugin>

5.重新生成eclipse項目構建

右鍵點擊項目目錄,選擇"Run As - Maven build..."(注意選帶...的);

在Goals中輸入"eclipse:clean eclipse:eclipse",點"Run"

6.重建web.xml

刪除src/main/webapp/WEB-INF/web.xml;

右鍵點擊項目目錄,選擇"Jave EE Tools - Generate Deployment Descriptor Stub"

7.恢復現場

第6步執行完後,你可能會發現,右鍵點擊項目名,”Maven”選項沒有了。別急,恢復一下現場就可以了。

右鍵點擊項目名,依次選擇"Configure - Convert to Maven Project"。

右鍵點擊項目名,選擇"Maven - Update Project..."

8.不太好的消息

maven3將maven-eclipse-plugin標記爲了’Deprecated’,說是交給eclipse來處理,但是eclipse根本沒有處理好這個問題,蛇精病。

好在只是標記爲’Deprecated’,說不準多少年內還能用的。

發佈了150 篇原創文章 · 獲贊 119 · 訪問量 73萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章