You need to enable JavaScript to run this app. 运行gradle clean报错 应用org.grails.grails-web插件失败求帮助
优惠活动
大模型
产品
解决方案
定价
更多

运行gradle clean报错 应用org.grails.grails-web插件失败求帮助

执行gradle clean命令报错求助

我运行gradle clean命令时遇到报错,请求协助排查build.gradle文件的相关问题。

报错信息

  • 问题位置:构建文件~/application/api/build.gradle第27行
  • 出错原因:评估根项目api时出现问题

应用插件org.grails.grails-web失败。无法创建任务:findMainClass。当前不支持替换可能已被其他插件使用过的现有任务,请为该任务(findMainClass)使用其他名称。

我确认错误发生在以下代码行:
apply plugin: "org.grails.grails-web"

请问有人能帮忙定位问题吗?我无法弄清出错原因。

当前build.gradle完整配置

buildscript {
        repositories {
                mavenLocal()
                maven { url "https://repo.grails.org/grails/core" }
                maven { url "https://dl.bintray.com/cscpublicgrails/plugins/" }
        }
        dependencies {
                classpath "org.grails:grails-gradle-plugin:$grailsVersion"
                classpath "org.grails.plugins:hibernate5:${gormVersion - ".RELEASE"}"
                classpath "org.grails.plugins:views-gradle:1.1.5"
                classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.1"
                classpath "org.grails.plugins:database-migration:3.0.3"
        }
}

def infoFile = file("../info.json")
def parsedInfo = new JsonSlurper().parseText(infoFile.text as String)

version parsedInfo.version
group "coconut"

apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "war"
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"
apply plugin: "org.grails.plugins.views-json"
apply plugin: "asset-pipeline"

repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://repo.grails.org/grails/core" }
}

dependencyManagement {
        imports {
                mavenBom "org.grails:grails-bom:$grailsVersion"
        }
        applyMavenExclusions false
}

dependencies {

        compile "org.springframework.boot:spring-boot-starter-logging"
        compile "org.springframework.boot:spring-boot-autoconfigure"
        compile "org.springframework.boot:spring-boot-starter-actuator"
        compile "org.springframework.boot:spring-boot-starter-tomcat"
        compile "org.hibernate:hibernate-core:5.1.3.Final"
        compile "org.hibernate:hibernate-ehcache:5.1.3.Final"
        compile "org.grails.plugins:hibernate5:${gormVersion - ".RELEASE"}"
        compile "org.grails:grails-core:$grailsVersion"
        compile "org.grails:grails-dependencies:$grailsVersion"
        compile "org.grails:grails-web-boot:$grailsVersion"
        compile "org.grails:grails-logging:$grailsVersion"
        compile "org.grails.plugins:cache:4.0.0"
        compile "org.grails.plugins:scaffolding:3.3.3"
        compile "org.grails.plugins:views-json:1.2.3"
        compile "org.grails.plugins:views-json-templates:1.2.3"

        compile "org.mindrot:jbcrypt:0.3m"
        compile "org.grails.plugins:mail:2.0.0"
        compile "mysql:mysql-connector-java:8.0.13"
        compile "org.grails.plugins:cookie:2.0.4"
        compile "org.grails.plugins:gsp:3.3.0.RC1"
        compile "com.google.guava:guava:20.0"
        compile "org.grails:grails-datastore-rest-client:6.1.5.RELEASE"
        compile "org.jetbrains:annotations-java5:15.0"
        compile "org.apache.httpcomponents:httpclient:4.5.3"
        compile "org.apache.commons:commons-io:1.3.2"
        compile "org.grails.plugins:cache-headers:2.0.2"
        compile "com.stripe:stripe-java:9.6.0"
        compile files("libs/basecommerce-sdk-5.1.93.jar")
        compile "com.atlassian.commonmark:commonmark:0.10.0"
        compile "net.htmlparser.jericho:jericho-html:3.3"
        compile "org.grails.plugins:database-migration:3.0.3"
        compile "org.liquibase:liquibase-core:3.5.3"
        compile "org.grails.plugins:quartz:2.0.13"
        compile "redis.clients:jedis:2.9.0"
        compile "org.grails.plugins:external-config:1.1.2"
        compile "org.grails.plugins:cascade-validation:3.0.1"
        compile "com.beanstream.api:beanstream:1.0.0"
        compile "org.jsoup:jsoup:1.13.1"

        implementation "io.nats:jnats:2.8.0"

        console "org.grails:grails-console"

        profile "org.grails.profiles:web-plugin"

        runtime 'org.grails.plugins:grails-console:2.1.1'
        runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.14.1"
        runtime "com.h2database:h2"

        testCompile "org.grails.plugins:geb:1.0.2"
        testCompile "org.grails:grails-datastore-gorm-test:$gormVersion"
        testCompile "org.grails:grails-gorm-testing-support:$testingVersion"
        testCompile "org.grails:grails-web-testing-support:$testingVersion"

        testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.52.0"
        testRuntime "net.sourceforge.htmlunit:htmlunit:2.9"
}

sourceSets {
        main {
                resources {
                        srcDir 'grails-app/migrations'
                }
        }
}

boolean debug = true
bootRun {
        List<String> args = ['-Dspring.output.ansi.enabled=always']
        if (debug) {
                args << '-Xdebug'
                args << '-Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n'
        }
        jvmArgs(args)
}

问题解决方案

根因定位

该报错属于Gradle 7.0+ 版本的插件任务兼容问题:

  • Gradle 7.0及以上版本默认禁止不同插件覆盖已经被注册的同名任务
  • org.grails.grails-web插件初始化时会自动创建findMainClass任务
  • 当前构建脚本中,在应用Grails Web插件之前,已经有eclipse/idea/war等插件提前注册了同名的findMainClass任务,触发了Gradle的兼容校验规则,导致插件应用失败。

修复步骤

  1. 调整插件应用顺序
    将Grails核心插件放在所有辅助插件之前应用,避免其他插件提前占用同名任务的注册名,调整后的插件应用顺序参考:
    // 优先应用Grails核心插件
    apply plugin: "org.grails.grails-web"
    apply plugin: "org.grails.grails-gsp"
    // 再应用其他辅助插件
    apply plugin: "eclipse"
    apply plugin: "idea"
    apply plugin: "war"
    apply plugin: "org.grails.plugins.views-json"
    apply plugin: "asset-pipeline"
    
  2. 兼容旧插件行为(若调整顺序后仍报错)
    在项目根目录的gradle.properties文件中添加配置,临时放开Gradle对旧版插件任务覆盖的限制:
    org.gradle.unsafe.task_overrides=true
    
  3. 版本匹配校验
    确认当前使用的grails-gradle-plugin版本与Gradle版本兼容:使用Gradle 7+版本时,需要搭配Grails 4.0.13及以上、Grails 5.x系列的官方兼容插件版本,避免旧版Grails插件适配问题。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.28 16:45:03