Maven已經是Java的項目管理標配,如何在JavaEE開發使用Maven調用Web應用,是很多同學關心的問題。本文將介紹,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。
tomcat7-maven-plugin 使用(常用)
配置
在pom.xm 加入以下xml。
<build> <!-- 配置了很多插件 --> <plugins> <!-- 編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>80</port> <path>/SSM</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> </configuration> </plugin> </plugins> </build>
注意上面有底色部分,簡要說明一下:
path 是訪問應用的路徑,例如我上面寫的是/SSM,則訪問路徑是 http://localhost/SSM
port 是tomcat 的端口號
uriEncoding URL按UTF-8進行編碼,這樣就解決了中文參數亂碼。
Server 指定tomcat名稱。
IDEA插件運行(重點)
(1)如果在pom.xml中配置了Tomcat插件,在右邊的Maven Project中會出現對應的插件,例如:
pom.xml:
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>80</port> <path>/SSM</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> </configuration> </plugin>
(2)此時運行的時候只需要右擊右邊的命令,然后可以以run模式或者debug模式啟動項目,debug模式可以打斷點進行調試
或者:
點擊Run-》Edit Configurations后搜索maven
啟動之后訪問項目即可
IDEA使用小結:
1.運行之后可以點擊上面的箭頭然后點擊Save.... 將運行方式保存起來,下次選擇相應的命令然后點擊右邊的運行按鈕(三角)或者調試按鈕(蟲子)即可以對應的模式運行命令。
(1)save之前:
(2)save之后選擇對應運行的命令然后以對應模式啟動即可:
(3)通過三角或蟲子上的小綠點可以判斷是以哪種模式啟動的:
2.如果項目中加上tomcat-maven-plugin插件發現點擊右邊的maven projects也會多出此插件:
例如下面配置集成了兩個tomcat插件:
pom.xml
<build> <!-- 配置了很多插件 --> <plugins> <!-- 編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- tomcat6插件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <path>/SSM</path> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <server>tomcat6</server> </configuration> </plugin> <!-- tomcat7插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>80</port> <path>/SSM</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> </configuration> </plugin> </plugins> </build>
發現右邊會兩個插件同時出現,也就是右邊的插件隨pom.xml變動,此時選擇對應的插件運行即可
下面介紹幾個常用的Goal
命令 描述
tomcat7:deploy 部署一個web war包
tomcat7:reload 重新加載web war包
tomcat7:start
啟動tomcat
tomcat7:stop
停止tomcat
tomcat7:undeploy
停止一個war包
tomcat7:run 啟動嵌入式tomcat ,並運行當前項目
tomcat-maven-plugin 插件使用
配置
在pom.xm 加入以下xml。
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <path>/wp</path> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <server>tomcat6</server> </configuration> </plugin>
具體配置一樣。
插件使用
在這里要注意一下,該插件命名方式有些不同,比如啟動tomcat ,對應的目標命令是: tomcat:run ,同樣,其它命令也是這樣,需要更改為:tomcat:<插件執行點>
關於部署項目到獨立的Tomcat下面參考:http://www.cnblogs.com/qlqwjy/p/8232429.html