使用 tomcat7-maven-plugin,可以將tomcat內嵌到web項目中,直接運行webapp項目。
第一步、pom.xml的配置:
<build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <!-- 項目訪問路徑 本例:localhost:9090, 如果配置的aa,則訪問路徑為localhost:9090/aa --> <port>9090</port> <uriEncoding>UTF-8</uriEncoding><!-- 非必需項 --> </configuration> </plugin> </plugins> </build>
第二步、配置maven的運行參數:
添加Maven,並在Command line中輸入:
clean tomcat7:run
點擊保存,然后run。
訪問http://localhost:9090
tomcat7-maven-plugin 常用命令:
tomcat7:run 啟動嵌入式tomcat ,並運行當前項目
tomcat7:deploy --部署一個web war包
tomcat7:reload --重新加載web war包
tomcat7:start --啟動tomcat
tomcat7:stop --停止tomcat
tomcat7:undeploy--停止一個war包
mvn tomcat7:deploy //第一次
mvn tomcat7:redeploy//之后
//這里我要求先重新打包,並跳過測試,再部署
//第一次
mvn package -Pdevelop -Dmaven.skip.test=true tomcat7:deploy
//之后
mvn package -Pdevelop -Dmaven.skip.test=true tomcat7:redeploy
拓展:maven的 java 編譯插件
<build> <plugins> <!--java 編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <!--輸入對應的編碼版本號 --> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
這是我們常用的編譯方式,配合tomcat使用。