最开始部署项目需要手动将web项目打成war包,然后手动上传到tomcat的webapp下,然后启动tomcat来部署项目。这种手动工作通常还要指定端口,指定项目位置等,这些操作是重复的操作。
开发的时候,ide自然想到集成这些功能,于是都是server模块,设置好参数就可以run server,测试了。个人操作的时候确实挺方便的,然而当团队协作的时候,每个人都要手动去设置这些参数,而且大家或许还在使用着各种各样的idea。eclipse和idea的配置方式截然不同。这好在都是java程序员,但还有前端呢,前端的同事一起开发的时候,他们会感觉这些配置繁琐。
后来发现,框架发展到现在反而更倾向于命令行的方式了。embedded 就是一种流行的方式。
当项目集成了embedded tomcat之后,只要type gradlew tomcatRun就可以运行项目。这样即使是前端程序员也可以本地调试项目了。
1.创建一个gradle web项目
在idea中,new -> project -> gradle -> web 就可以创建一个空的gradle web项目。
在build.gradle 中添加tomcat的依赖:
buildscript { repositories { jcenter() } dependencies { classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.5' } } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' def tomcatVersion = '7.0.59' tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}", "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}" }
添加tomcat 插件:
apply plugin: 'com.bmuschko.tomcat'
2. 运行
这时候就可以运行了。在命令行中输入:
gradlew tomcatRun
第一次运行会下载对应的jar包,然后显示:
Started Tomcat Server The Server is running at http://localhost:8080/embTomcat
端口默认8080,contextPath默认为项目名。
2.1 Task
tomcatRun 为 运行 exploded web application;
tomcatRunWar 则是运行war包。
Task Name | Depends On | Type | Description |
---|---|---|---|
tomcatRun | - | TomcatRun | Starts a Tomcat instance and deploys the exploded web application to it. |
tomcatRunWar | - | TomcatRunWar | Starts a Tomcat instance and deploys the WAR to it. |
tomcatStop | - | TomcatStop | Stops the Tomcat instance. |
tomcatJasper | - | TomcatJasper | Runs the JSP compiler and turns JSP pages into Java source usingJasp |