You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Gradle 6.5.1搭配Gretty 3.0.3启动Jetty失败,请求技术支持

解决Gretty无法找到Jetty 9.4运行依赖的问题

看起来你在搭建Jersey REST API时遇到了Gretty插件的依赖解析问题,我来帮你梳理下解决方案:

问题根源

你遇到的Could not find org.gretty:gretty-runner-jetty94:3.0.3错误,主要有两个核心原因:

  1. Gretty 3.x版本的部分运行器依赖并没有同步到Maven Central仓库,导致Gradle无法正常拉取
  2. 你的build.gradle同时混用了Spring Boot插件和Gretty插件,这两个插件都涉及嵌入式容器管理,很容易引发依赖冲突

具体解决方案

方案1:降级Gretty到兼容版本并清理冗余配置

Gretty 2.3.1是一个稳定且兼容性好的版本,它的所有依赖都能在Maven Central正常获取,同时和Gradle 6.5.1适配性更佳。修改你的build.gradle如下:

plugins {
    id 'eclipse-wtp'
    id 'war'
    id 'org.gretty' version '2.3.1' // 切换到稳定兼容的Gretty版本
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    // Jersey核心依赖保留
    implementation 'org.glassfish.jersey.containers:jersey-container-servlet:2.25.1'
    implementation 'org.glassfish.jersey.inject:jersey-hk2:2.26'
    
    // 移除不必要的Spring Boot依赖(如果不需要Spring Boot的话)
    // implementation 'org.springframework.boot:spring-boot-starter'
    
    // 调整测试依赖为标准JUnit 5
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

// 显式指定Gretty使用Jetty 9.4
gretty {
    servletContainer = 'jetty9.4'
}

修改后执行gradle jettyRun就能正常启动服务了。

方案2:改用Spring Boot自带的嵌入式容器(推荐)

既然你原本引入了Spring Boot,完全可以利用它自带的容器能力,不需要额外的Gretty插件,配置会更简洁且冲突更少:

plugins {
    id 'eclipse-wtp'
    id 'war'
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    // 使用Spring Boot整合的Jersey starter,自动处理依赖兼容性
    implementation 'org.springframework.boot:spring-boot-starter-jersey'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

之后直接运行gradle bootRun就能启动应用,Spring Boot会自动配置嵌入式容器(默认Tomcat,如需Jetty可以排除Tomcat依赖并引入spring-boot-starter-jetty)。

方案3:添加Gretty专用仓库(不推荐)

如果一定要坚持使用Gretty 3.x,需要在仓库配置中添加Gradle插件专用仓库:

repositories {
    mavenCentral()
    maven { url 'https://plugins.gradle.org/m2/' }
}

不过这个方案可能还是会有版本兼容问题,因为Gradle 6.5.1和Gretty 3.x的适配性不如2.x版本。

关键提醒

  • 不要同时使用Spring Boot插件和Gretty插件,二者的容器管理逻辑会互相干扰,引发不可预期的问题
  • 确保Jersey版本和容器版本的兼容性,避免出现类加载冲突或功能异常

内容的提问来源于stack exchange,提问作者Prvt_Yadav

火山引擎 最新活动