在部署應用程序時,簡單性是最大的優勢。 您將了解到,尤其是在項目發展且需要在環境中進行某些更改時。 將整個應用程序打包到一個獨立且自足的JAR中似乎是個好主意,尤其是與在目標環境中安裝和升級Tomcat相比。 過去,我通常將Tomcat JARs
包含在我的Web應用程序中,并使用Tomcat API編寫瘦命令行運行程序。 幸運的是,有一個tomcat7:exec-war
maven目標可以做到這一點。 它需要您的WAR工件并將其與所有Tomcat依賴項打包在一起。 最后,它還包含要顯示的Tomcat7RunnerCli
Main-class
。
想嘗試一下嗎? 接受您現有的WAR項目并將以下內容添加到pom.xml
:
<plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.0</version><executions><execution><id>tomcat-run</id><goals><goal>exec-war-only</goal></goals><phase>package</phase><configuration><path>/standalone</path><enableNaming>false</enableNaming><finalName>standalone.jar</finalName><charset>utf-8</charset></configuration></execution></executions>
</plugin>
運行mvn
package,幾秒鐘后,您將在目標目錄中找到閃亮的standalone.jar
。 運行您的Web應用程序從未如此簡單:
$ java -jar target/standalone.jar
…您可以瀏覽localhost:8080/standalone
。 盡管path
參數的文檔說(重點是我):
用于正在運行的Web應用程序的webapp上下文路徑。 將Webapp存儲在exec jar中的名稱。 不要在我們兩個人之間使用 /,畢竟<path>/</path>
似乎可以工作。 事實證明,內置main
類實際上要靈活一些。 例如,您可以說(我希望這是不言而喻的):
$ java -jar standalone.jar -httpPort=7070
這有什么runnable
JAR
所做的就是第一個解壓縮它的WAR文件中的一些目錄( extract
通過default1
),并將其部署到Tomcat -所有必需的Tomcat JARs
也包括在內。 空的standalone.jar(內部幾乎沒有KiB WAR)的權重約為8.5 MiB
–如果您聲稱將每個版本的整個Tomcat與應用程序一起推送是浪費的,那么就不算多。
在談論Tomcat JARs
,您應該想知道如何選擇此runnable
包含的Tomcat版本? 不幸的是,我找不到任何簡單的選項,因此我們必須回到顯式重新定義插件依賴項(2.0版已將7.0.30 Tomcat硬編碼)。 這很無聊,但沒有那么復雜,可能對將來的參考很有用:
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><tomcat7Version>7.0.33</tomcat7Version>
</properties><build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.0</version><executions><execution><id>tomcat-run</id><goals><goal>exec-war-only</goal></goals><phase>package</phase><configuration><path>/standalone</path><enableNaming>false</enableNaming><finalName>standalone.jar</finalName><charset>utf-8</charset></configuration></execution></executions><dependencies><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-util</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-coyote</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-api</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jdbc</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-dbcp</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-servlet-api</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jsp-api</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jasper</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jasper-el</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-el-api</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-catalina</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-tribes</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-catalina-ha</artifactId><version>${tomcat7Version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-annotations-api</artifactId><version>${tomcat7Version}</version></dependency></dependencies></plugin></plugins>
</build>
在下一篇文章中,我們將學習如何處理出現在終端java.util.logging
這些討厭的Tomcat內部日志( java.util.logging
…)。與此同時,我發現并報告了MTOMCAT-186。關閉可執行文件JAR不會調用ServletContextListener.contextDestroyed()
–看看這是否適合您。
1 -這可能是一個好主意,用指定不同的目錄- extractDirectory
與每次重新啟動前清理- resetExtract
。
參考: Java和社區博客中來自JCG合作伙伴 Tomasz Nurkiewicz的帶有可運行Tomcat的獨立Web應用程序 。
翻譯自: https://www.javacodegeeks.com/2012/11/standalone-web-application-with-executable-tomcat.html