1. 程式人生 > >利用tomcat7-maven-plugin外掛,do what you want to do!

利用tomcat7-maven-plugin外掛,do what you want to do!

一、pom.xml

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <path>/</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/text</url>
                    <server>tomcat7</server>
                </configuration>
                <executions>
                    <execution>
                        <id>executable</id>
                        <goals>
                            <goal>exec-war</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>

注意事項:
tomcat7-maven-plugin的2.2版本有嚴重bug,因此,想要正常執行,請使用2.1版本。另,單純的tomcat7:exec-war命令是沒有任何卵用的,單獨執行會報錯,只有綁定了package這個phase,才能正常打包!
就像maven-war-plugin外掛的war:war一樣,直接執行war:war命令,打出來的包裡面的classe是空的,只有繫結package這個phase, phase中的maven-compile-plugin外掛才會將原始碼編譯並放入classes目錄。


二、在開發過程,擺脫外接tomcat伺服器

在專案原始碼中,通過一條maven命令,啟動springmvc專案

mvn tomcat7:run

三、在部署過程,擺脫外接tomcat伺服器和maven配置

通過命令

mvn package

得到一個可在外接tomcat伺服器部署的war包,和一個可以直接通過java命令執行的jar包,該包命名規則為${artifactId-version-packaging-exec}.jar,對該包執行

java -jar ${artifactId-version-packaging-exec}.jar

即可執行web系統。完全脫離外接伺服器和maven搭建。

參考:
https://ufasoli.blogspot.com/2013/11/create-runnable-war-with-embedded.html?q=runnable+war