From:http://my.oschina.net/angel243/blog/178554apache
http://yuandingjiema.iteye.com/blog/1752544api
之前在項目中不多使用Maven,最近本身學習了一下maven,真的是很是強大的項目構建工具,對於依賴包的定義及版本,以及依賴包的集中管理(中央倉庫)都讓人驚喜(原諒個人大驚小怪,雖然Maven出來好久了,但小弟剛接觸),但發現開發Web項目時,須要手動部署到Web服務器(Tomcat7),若是能自動部署到Web服務器,而不用每次手動把target下編譯好的war包拷貝到Tomcat下就更好了。tomcat
下面是具體的使用方法:服務器
新建系統變量CATALINA_HOME,值爲:C:\opensource\tomcat-7.0.34(Tomcat的安裝目錄),而後Path的最後面添加%CATALINA_HOME%\lib; %CATALINA_HOME%\lib\servlet-api.jar;%CATALINA_HOME%\lib\jsp-api.jareclipse
Tomcat7的用戶及權限配置:在conf目錄下,找到tomcat-users.xml,添加manager權限的用戶。這裏我把所用的權限都添加給admin用戶了,具體代碼以下: jsp
<role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="admin" roles="manager-gui,manager-script"/>
在Maven的安裝路徑找到conf目錄下的setting.xml文件,在<servers>節點中添加tomcat7下配置的用戶信息(id能夠任意填寫,但username和password必須和步驟1一致): maven
<server>
<id>tomcat7</id>
<username>admin</username>
<password>admin</password>
</server>
在pom.xml的<plugins>節點中,添加tomcat7的maven插件ide
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>tomcat7</server> <username>admin</username> <password>admin</password> </configuration> </plugin>
在project節點下,在插件倉庫(plugin repositories)和普通倉庫(repositories)中添加如下倉庫到pom.xml,保證maven能夠從倉庫中下載到tomcat-maven-plugin插件,少添加了這段信息,會出現以下報錯信息:工具
No plugin found for prefix 'tomcat' in the current project and in the plug……學習
<repositories> <repository> <id>people.apache.snapshots</id> <url> http://repository.apache.org/content/groups/snapshots-group/ </url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>apache.snapshots</id> <name>Apache Snapshots</name> <url> http://repository.apache.org/content/groups/snapshots-group/ </url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>
在maven build的goals中輸入命令tomcat7:deploy便可發佈,run as --- run configuration下
eclipse中使用maven插件的時候,運行run as maven build的時候報錯及解決方案:
1. -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
能夠設一個環境變量M2_HOME指向你的maven安裝目錄
M2_HOME=D:\Apps\apache-maven-3.3.1
而後在Window->Preference->Java->Installed JREs->Edit,在Default VM arguments中設置
-Dmaven.multiModuleProjectDirectory=$M2_HOME
2. No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
解決方案:參考http://blog.csdn.net/fox_lht/article/details/16369147
jdk的設置請指定jdk目錄,而非jre目錄,不然會出現如上錯誤
3. tomcat7-maven-plugin:2.0-SNAPSHOT:deploy (default-cli) on project : Cannot find war file
解決方案:對於生成的war包文件命名爲{project.build.finalName}
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warName>${project.build.finalName}</warName> </configuration> </plugin>
Maven內置變量說明: