天天看点

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>

继续阅读