gradle中使用嵌入式(embedded) tomcat, debug 启动

本文介绍了如何在Gradle项目中使用嵌入式Tomcat,包括创建Gradle Web项目,配置运行任务,修改默认的端口和contextPath,以及设置调试。通过添加Tomcat插件并配置相关属性,可以实现通过gradlew tomcatRun启动项目。此外,还讲解了如何解决编译错误,并提供了Debug启动的设置方法。
摘要由CSDN通过智能技术生成
在gradle项目中使用embedded tomcat。

最开始部署项目需要手动将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
n: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dbHealthContributor' defined in class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfiguration.class]: Unsatisfied dependency expressed through method 'dbHealthContributor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class 2023-07-13 16:19:27.253 INFO 14712 --- [ main] o.a.c.c.StandardService : Stopping service [Tomcat] 2023-07-13 16:19:27.291 INFO 14712 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2023-07-13 16:19:27.310 ERROR 14712 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值