Maven集成Tomcat插件


類似插件及版本區別:

Maven Tomcat插件現在主要有兩個版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同。

tomcat-maven-plugin 插件官網:http://mojo.codehaus.org/tomcat-maven-plugin/plugin-info.html

tomcat7-maven-plugin 插件官網:http://tomcat.apache.org/maven-plugin.html

tomcat-maven-plugin這個插件是老版本,不知道是被apache收購還是怎么的,現在已經停用,命令是mvn tomcat:run,而且該插件應該也不支持tomcat7

Apache內部這個插件現在也有兩個版本,分別是tomcat6,tomcat7

tomcat6:

<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat6-maven-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<url>http://127.0.0.1:8080/manager</url>
		<server>tomcat</server>
		<username>admin</username> 
		<password>admin</password>
		<path>/dev_web</path><!--WEB應用上下文路徑-->
		<contextReloadable>true</contextReloadable>
	</configuration>
</plugin>

tomcat7:

<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<url>http://127.0.0.1:8080/manager</url>
		<server>tomcat</server>
		<username>admin</username> 
		<password>admin</password>
		<path>/dev_web</path><!--WEB應用上下文路徑-->
		<contextReloadable>true</contextReloadable>
	</configuration>
</plugin>

下面的篇幅中就只討論tomcat7

本地運行,啟動嵌入式tomcat:

<plugin>   
  <groupId>org.apache.tomcat.maven</groupId>   
  <artifactId>tomcat7-maven-plugin</artifactId>   
  <version>2.2</version>   
  <configuration>      
    <hostName>localhost</hostName>        <!--   Default: localhost -->  
    <port>8080</port>                     <!-- 啟動端口 Default:8080 --> 
    <path>/</path>   <!-- 訪問應用路徑  Default: /${project.artifactId}-->  
    <uriEncoding>UTF-8</uriEncoding>      <!-- uri編碼 Default: ISO-8859-1 -->
  </configuration>
</plugin>

如果在啟動運行過程中報異常:

錯誤一:

Unknown default host [localhost] for connector [Connector[HTTP/1.1-8083]]

那么把hostName改成localhost即可。

錯誤二:

[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on project springdemo-list: Could not start Tomcat: Failed to start component [StandardServer[-1]]: Failed to start component [StandardService[Tomcat]]: Failed to start component [StandardEngine[Tomcat]]: A child container failed during start -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

那么一般就是

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

這個依賴包添加scope為provided就可以

意思是這個servlet-api的依賴包只在編譯和測試時使用而不在運行時使用;因為web容器自身一般都會帶這些依賴包,故配置上scope。假如不配置此項,啟動tomcat時出現上述的異常,個人認為是由於我們自己在pom.xml引入的依賴跟web容器自己的一些依賴包沖突導致。

Idea運行調試:

這種內嵌tomcat方式啟動項目,直接命令操作即可

但是如果想要調試,就必須使用編輯器的maven插件,

比如idea,直接在Run/Debug Configuration->Maven->Commandline中輸入 clean tomcat7:run 即可

上述方式調試,頁面修改可以直接顯示,后台代碼可以使用Jrebel熱部署

vscode運行調試:

launch.json中添加配置:

 {
            "type": "java",
            "name": "Debug (Attach)",
            "request": "attach",
            "hostName": "localhost",
            "port": 8000
        }

命令啟動:

mvnDebug -DskipTests tomcat7:run -Pirm-web -Pdev

再啟動vscode中的啟動按鈕

遠程部署:

項目中的pom.xml配置:

<!-- Tomcat 自動部署 plugin -->
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <!-- 對應的 tomcat manager的接口-->
        <url>http://127.0.0.1:8080/manager/text</url>
        <!-- setting.xml 的server id-->
        <server>tomcat</server>
        <!-- tomcat-user.xml 的 username -->
        <username>admin</username>
        <!-- tomcat-user,xml 的 password -->
        <password>admin</password>
        <!-- web項目的項目名稱-->
        <path>/${project.artifactId}</path>
        <uriEncoding>UTF-8</uriEncoding>
        <!-- 若tomcat項目中已存在,且使"mvn tomcat7:deploy"命令必須要設置下面的代碼 -->
        <!-- 更新項目時,僅需要執行"mvn tomcat7:redeploy"命令即可 -->
        <!-- 上述命令無論服務器是tomcat7、8或9,均是使用"mvn tomcat7:deploy"或"mvn tomcat7:redeploy" -->
        <update>true</update>
    </configuration>
</plugin>

url:打包好的包通過這個url上傳到tomcat處

path:這里如果設置/,默認就是ROOT,最好設置為項目名稱,這樣可以在一個端口下部署多個項目

Tomcat中的tomcat-users.xml配置:

<role rolename="admin"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<user username="admin" password="admin" roles="manager-gui,admin,manager-jmx,manager-script,manager-status,admin-gui" />

注意tomcat一定要重啟才會生效

Maven中的settings.xml配置:

<server>
	<id>tomcat</id>
	<username>admin</username>
	<password>admin</password>
</server>

注意事項:

tomcat一定要啟動才能部署項目

Tomcat插件命令:

tomcat7:deploy --部署web war包
tomcat7:redeploy --重新部署web war包
tomcat7:undeploy --停止該項目運行,並刪除部署的war包
tomcat7:run --啟動嵌入式tomcat ,並運行當前項目
tomcat7:exec-war --創建一個可執行的jar文件,允許使用java -jar mywebapp.jar 運行web項目
tomcat7:help --在tomcat7-maven-plugin顯示幫助信息

參考:

Maven 集成Tomcat插件

tomcat7:run

使用maven的tomcat插件部署項目

maven 插件系列1 :tomcat插件 集成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM