天天看點

gradle中使用嵌入式(embedded) tomcat, debug 啟動1.建立一個gradle web項目2. 運作

在gradle項目中使用embedded tomcat。

最開始部署項目需要手動将web項目打成war包,然後手動上傳到tomcat的webapp下,然後啟動tomcat來部署項目。這種手動工作通常還要指定端口,指定項目位置等,這些操作是重複的操作。

開發的時候,ide自然想到內建這些功能,于是都是server子產品,設定好參數就可以run server,測試了。個人操作的時候确實挺友善的,然而當團隊協作的時候,每個人都要手動去設定這些參數,而且大家或許還在使用着各種各樣的idea。eclipse和idea的配置方式截然不同。這好在都是java程式員,但還有前端呢,前端的同僚一起開發的時候,他們會感覺這些配置繁瑣。

後來發現,架構發展到現在反而更傾向于指令行的方式了。embedded 就是一種流行的方式。

當項目內建了embedded tomcat之後,隻要type gradlew tomcatRun就可以運作項目。這樣即使是前端程式員也可以本地調試項目了。

<a href="#create">建立一個gradle web項目</a>

<a href="#run">運作</a>

<a href="#task">task</a>

<a href="#edit">修改預設配置</a>

<a href="#pro">一些配置屬性</a>

<a href="#faq">FAQ</a>

<a href="#debug">debug</a>

<a href="#gdebug">gradle 提供的 debug</a>

在idea中,new -&gt; project -&gt; gradle -&gt; web 就可以建立一個空的gradle web項目。

在build.gradle 中添加tomcat的依賴:

添加tomcat 插件:

這時候就可以運作了。在指令行中輸入:

第一次運作會下載下傳對應的jar包,然後顯示:

端口預設8080,contextPath預設為項目名。

tomcatRun 為 運作 exploded web application;

tomcatRunWar 則是運作war包。

Task Name

Depends On

Type

Description

tomcatRun

-

<a href="http://bmuschko.github.io/gradle-tomcat-plugin/docs/groovydoc/com/bmuschko/gradle/tomcat/tasks/TomcatRun.html">TomcatRun</a>

Starts a Tomcat instance and deploys the exploded web application to it.

tomcatRunWar

<a href="http://bmuschko.github.io/gradle-tomcat-plugin/docs/groovydoc/com/bmuschko/gradle/tomcat/tasks/TomcatRunWar.html">TomcatRunWar</a>

Starts a Tomcat instance and deploys the WAR to it.

tomcatStop

<a href="http://bmuschko.github.io/gradle-tomcat-plugin/docs/groovydoc/com/bmuschko/gradle/tomcat/tasks/TomcatStop.html">TomcatStop</a>

Stops the Tomcat instance.

tomcatJasper

<a href="http://bmuschko.github.io/gradle-tomcat-plugin/docs/groovydoc/com/bmuschko/gradle/tomcat/tasks/TomcatJasper.html">TomcatJasper</a>

習慣了'/'作為contextPath:

這時候運作結果:

8080端口就挺好的,當然,如果想要自定義也是可以的:

事實上,也提供可更加細膩的自定設定。這時候需要再添加一個依賴:

然後一個自定配置執行個體:

啟動運作:

可以看到,這個開啟了https,頁面可以直接通路https的位址了。端口設定為8091. 通過都設定為8443.

The Tomcat plugin exposes the following properties through the extension named <code>tomcat</code>:

<code>httpPort</code>: The TCP port which Tomcat should listen for HTTP requests on (defaults to <code>8080</code>).

<code>httpsPort</code>: The TCP port which Tomcat should listen for HTTPS requests on (defaults to <code>8443</code>).

<code>ajpPort</code>: The TCP port which Tomcat should listen for AJP requests on (defaults to <code>8009</code>).

<code>stopPort</code>: The TCP port which Tomcat should listen for admin requests on (defaults to <code>8081</code>).

<code>stopKey</code>: The key to pass to Tomcat when requesting it to stop (defaults to <code>null</code>).

<code>contextPath</code>: The URL context path under which the web application will be registered (defaults to WAR name).

<code>enableSSL</code>: Determines whether the HTTPS connector should be created (defaults to <code>false</code>).

<code>daemon</code>: Specifies whether the Tomcat server should run in the background. When true, this task completes as soon as the server has started. When false, this task blocks until the Tomcat server is stopped (defaults to <code>false</code>).

<code>keystoreFile</code>: The keystore file to use for SSL, if enabled (by default, a keystore will be generated).

<code>httpProtocol</code>: The HTTP protocol handler class name to be used (defaults to <code>org.apache.coyote.http11.Http11Protocol</code>).

<code>httpsProtocol</code>: The HTTPS protocol handler class name to be used (defaults to<code>org.apache.coyote.http11.Http11Protocol</code>).

<code>ajpProtocol</code>: The AJP protocol handler class name to be used (defaults to <code>org.apache.coyote.ajp.AjpProtocol</code>).

<code>users</code>: List of users with <code>username</code>, <code>password</code> and <code>roles</code>. Used to configure tomcat with basic authentication with these users.

到這裡基本已經可以使用了。當然,如果想要更多的了解深入的配置:https://github.com/bmuschko/gradle-tomcat-plugin

Furthermore, you can set the following optional task properties: <code>contextPath</code>: The URL context path your web application will be registered under (defaults to WAR name). <code>webDefaultXml</code>: The default web.xml. If it doesn't get defined an instance of<code>org.apache.catalina.servlets.DefaultServlet</code> and <code>org.apache.jasper.servlet.JspServlet</code> will be set up. <code>additionalRuntimeResources</code>: Defines additional runtime JARs or directories that are not provided by the web application. <code>URIEncoding</code>: Specifies the character encoding used to decode the URI bytes by the HTTP Connector (defaults to <code>UTF-8</code>). <code>configFile</code>: The path to the Tomcat context XML file (defaults to <code>src/main/webapp/META-INF/context.xml</code> for <code>tomcatRun</code>, defaults to <code>META-INF/context.xml</code> within the WAR for <code>tomcatRunWar</code>). <code>outputFile</code>: The file to write Tomcat log messages to. If the file already exists new messages will be appended. <code>reloadable</code>: Forces context scanning if you don't use a context file (defaults to <code>true</code>). <code>keystorePass</code>: The keystore password to use for SSL, if enabled. <code>truststoreFile</code>: The truststore file to use for SSL, if enabled. <code>truststorePass</code>: The truststore password to use for SSL, if enabled. <code>clientAuth</code>: The clientAuth setting to use, values may be: <code>true</code>, <code>false</code> or <code>want</code> (defaults to <code>false</code>). Note: <code>keystoreFile</code> and <code>truststoreFile</code> each require an instance of a <code>File</code> object e.g. <code>file("/path/my.file")</code>

編譯錯誤!

tomcat7+需要一個jar包來編譯,如果沒有則添加:

反正我是沒遇到。

Why do I get a <code>java.lang.ClassCastException</code> on <code>javax.servlet.Servlet</code>?

依賴多個版本servlet-api導緻了錯誤:

解決:隻在編譯的時候使用:

debug啟動需要設定gradle的環境變量,即運作gradle指令的時候插入(fork)一些參數指令。

可以設定環境變量GRADLE_OPTS為:

即監聽5005端口。這時候運作則會顯示:

在idea中監聽端口:Run-&gt;edit configurations -&gt;+ -&gt; remote, 預設配置就可以了。

什麼?不知道怎麼設定環境變量?

作為一個java程式員的第一件事就是設定環境變量。當然,我們可以在程式運作的時候添加環境變量,比如在idea中使用自帶的gradle插件的時候指定參數,我感覺相當繁瑣,不過還是記錄下來。

首先,edit configurations  -&gt; + -&gt; gradle -&gt; 

gradle中使用嵌入式(embedded) tomcat, debug 啟動1.建立一個gradle web項目2. 運作

然後,點選運作:

gradle中使用嵌入式(embedded) tomcat, debug 啟動1.建立一個gradle web項目2. 運作

這時候就已經在監聽5005端口了,隻需要添加remote debug就可以了:

edit configurations -&gt; + -&gt; remote:

gradle中使用嵌入式(embedded) tomcat, debug 啟動1.建立一個gradle web項目2. 運作

debug:

gradle中使用嵌入式(embedded) tomcat, debug 啟動1.建立一個gradle web項目2. 運作

這種方式還是相當繁瑣的。不就是設定環境變量嗎,好吧,我确實沒去研究怎麼設定了,應該是在腳本中添加的,以後再研究吧。下面介紹一個簡單的方式:

沒錯,這樣就可以了。剩下的監聽remote同上。指令行結束會等待監聽,debug remote就可以了。

reference:

唯有不斷學習方能改變!

-- <b>Ryan Miao</b>

繼續閱讀