maven 配置tomcat外掛執行web專案
其實這樣的問題或者教程網上一搜都是一大片,但是我還是要重新宣告一下,然後再討論為什麼我要寫這篇部落格!
tomcat官網(點我)中介紹的清晰:Apache Tomcat Maven Plugin 共有兩種方式。
1·在pom.xml檔案中新增關於tomcat的外掛的配置:
Note:You must configure your pom to use this new groupld
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
注意:這種配置只針對某一個專案的,即只對一個專案生效。
2·在maven的setting.xml檔案中設定
add the groupld in your settings.xml
<pluginGroups>
....
<pluginGroup>org.apache.tomcat.maven</pluginGroup>
....
</pluginGroups>
注意:這種maven外掛上的配置會對所有繫結該maven的專案中。
配置好後,就可以啟動看效果。
此時,我有一個疑問?
非maven專案的話,我們會繫結專案和應用伺服器(以tomcat為例),並會設定好context name,(context name 就是你專案部署在tomcat中的應用名稱,如圖:
然後啟動埠會在tomcat的service.xml檔案中配置。這樣在url位址列輸入 http://ip:port/context name 即可訪問。那如果啟動maven專案,埠號和應用名稱應該怎麼配置呢?同樣有兩種配置方式:
1·在pom.xml檔案配置
<build>
<plugins>
<!-- 配置Tomcat外掛 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 這裡配置埠號和訪問路徑 -->
<port>8082</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
2·使用Maven Build 啟動專案,Goals那一欄填寫:
如果是應用在tomcat6外掛上:
tomcat6:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080
如果是應用在tomcat7外掛上:
tomcat7:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080
最後啟動成功!如圖:
整體內容介紹到這裡,那麼接下來就是重頭戲呢?大家好好看,我哪裡出問題了
1·我的pom.xml檔案:
<build>
<plugins>
<!-- 配置Tomcat外掛 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 這裡配置埠號和訪問路徑 -->
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
2·我點選我的專案,右擊Run as --》Maven Build,在Goals中輸入:clean tomcat:run
3·啟動成功,如下圖:
看到了嗎?我在pom檔案中已經配置了path為根目錄/,那為什麼啟動成功後的提示資訊為:
Running war on http://localhost:8080/taotao-portal呢?what's wrong with my tomcat?
起初我不明白,為什麼我的path已經設定為根目錄了,按照正常情況來說,提示 Running war on http://localhost:8080/,這樣才正常呢,然後試圖更改過,但無效,這就忍了,知道今天,我在pom.xml更改port為8082後,正常啟動後,還是這樣的,我就鬱悶了,我的配置檔案為什麼不管作用呢!!!為什麼呢?我也沒看出來我哪裡有問題啊?
最後看了看tomcat官網後,才有所啟示:我的pom.xml檔案中配置的為tomcat7外掛,然後大家看,上圖中的第三個紅框中的內容:(原來沒有注意過這些),它怎麼是tomcat6呢?
原因:我在Maven build 的goals中輸入的為:clean tomcat:run,這樣的命令,會預設載入tomcat6外掛,所以,我pom.xml配置檔案中配置的tomcat7也就無效了,所以,不管更改port還是path,都會失效
so,just it!
---------------------
作者:賈麗敏
來源:CSDN
原文:https://blog.csdn.net/shiyiqijialimin/article/details/60332883
版權宣告:本文為博主原創文章,轉載請附上博文連結!
另外就是maven 執行tomcat8 外掛的問題
1 有位大佬,發現在中央倉庫等等倉庫根本找不到tomcat8 外掛,
2 後來在某些倉庫裡找到了外掛,
3 然後pom.xml 裡面有個標籤,可以設定外掛的獲取地址
4 然後就配置好了tomcat8 外掛,,,,
注意:
1 命令和tomcat7一樣的,
執行:tomcat8:run 。
2 第一次下載比較慢。