错误记录Groovy 报错 ( Execution failed for task ‘:compileGroovy‘. > org/apache/tools/ant/taskdefs/Java )

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误记录Groovy 报错 ( Execution failed for task ‘:compileGroovy‘. > org/apache/tools/ant/taskdefs/Java )相关的知识,希望对你有一定的参考价值。





一、报错信息



编译 Groovy 代码时 , 报如下错误 , 该错误不是代码错误 , 而是 Groovy 工程的环境配置错误 ;

相同的环境 , 昨天还能编译 , 今天出现问题了 ;

10:37:17: Executing task 'Test.main()'...

> Task :compileJava NO-SOURCE
> Task :compileGroovy FAILED
1 actionable task: 1 executed

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileGroovy'.
> org/apache/tools/ant/taskdefs/Java

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
10:37:18: Task execution finished 'Test.main()'.

完整报错信息 : 使用 --stacktrace 参数 , 即可打印出完整的报错信息 ;

11:00:32: Executing task 'Test.main() --stacktrace'...

> Task :compileJava NO-SOURCE
> Task :compileGroovy FAILED
1 actionable task: 1 executed

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileGroovy'.
> org/apache/tools/ant/taskdefs/Java

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileGroovy'.
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$2.accept(ExecuteActionsTaskExecuter.java:121)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$2.accept(ExecuteActionsTaskExecuter.java:117)
	at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:184)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:110)
	at org.gradle.api.internal.tasks.execution.ResolveIncrementalChangesTaskExecuter.execute(ResolveIncrementalChangesTaskExecuter.java:84)
	at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:91)
	at org.gradle.api.internal.tasks.execution.ResolveBeforeExecutionStateTaskExecuter.execute(ResolveBeforeExecutionStateTaskExecuter.java:74)
	at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
	at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:109)
	at org.gradle.api.internal.tasks.execution.ResolveBeforeExecutionOutputsTaskExecuter.execute(ResolveBeforeExecutionOutputsTaskExecuter.java:67)
	at org.gradle.api.internal.tasks.execution.ResolveAfterPreviousExecutionStateTaskExecuter.execute(ResolveAfterPreviousExecutionStateTaskExecuter.java:46)
	at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:93)
	at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:45)
	at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:94)
	at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
	at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:56)
	at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:63)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:49)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:46)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:416)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:406)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:102)
	at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:46)
	at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:43)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:355)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:343)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:336)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:322)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker$1.execute(DefaultPlanExecutor.java:134)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker$1.execute(DefaultPlanExecutor.java:129)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:202)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.executeNextNode(DefaultPlanExecutor.java:193)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:129)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
	at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
	at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: java.lang.NoClassDefFoundError: org/apache/tools/ant/taskdefs/Java
	at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:179)
	at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:57)
	at org.gradle.api.internal.tasks.compile.GroovyCompilerFactory$DaemonSideCompiler.execute(GroovyCompilerFactory.java:80)
	at org.gradle.api.internal.tasks.compile.GroovyCompilerFactory$DaemonSideCompiler.execute(GroovyCompilerFactory.java:68)
	at org.gradle.api.internal.tasks.compile.daemon.AbstractDaemonCompiler$CompilerCallable.call(AbstractDaemonCompiler.java:86)
	at org.gradle.api.internal.tasks.compile.daemon.AbstractDaemonCompiler$CompilerCallable.call(AbstractDaemonCompiler.java:74)
	at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:42)
	at org.gradle.workers.internal.WorkerDaemonServer.execute(WorkerDaemonServer.java:38)
	at org.gradle.process.internal.worker.request.WorkerAction.run(WorkerAction.java:111)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:175)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:157)
	at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
	... 3 more
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.taskdefs.Java
	... 17 more


* Get more help at https://help.gradle.org

BUILD FAILED in 1s
11:00:33: Task execution finished 'Test.main() --stacktrace'.





二、问题分析



错误是由于没有找到 org.apache.tools.ant.taskdefs.Java 类导致的 ; 查看 工程的 外部依赖库 , 没有看到 apache 相关的库 ;

尝试删除 .gradle , build , .idea 三个目录 , 编译也会报错 ;

即使屏蔽了所有代码 , 也是报错 , 说明这是 Groovy 构建环境导致的错误 ;


当前出现错误的 Groovy 工程构建配置 :
groovy_demo\\gradle\\wrapper\\gradle-wrapper.properties 中配置的 Gradle 工具版本号是 5.2.1 5.2.1 5.2.1 ;

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

在 Groovy 工程根目录 配置的 build.gradle 构建脚本 内容如下 :

plugins 
    id 'groovy'
    id 'java'


group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories 
    mavenCentral()


dependencies 
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'


找了半天没找出原因 , 直接创建一个新的工程 , 查看其配置 :

新工程使用 6.8 6.8 6.8 版本的 Gradle 工具 ;

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-6.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

配置的 build.gradle 构建脚本内容如下 :

plugins 
    id 'groovy'
    id 'java'


group 'org.example'
version '1.0-SNAPSHOT'

repositories 
    mavenCentral()


dependencies 
    implementation 'org.codehaus.groovy:groovy-all:3.0.5'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'


test 
    useJUnitPlatform()

外部依赖库 :





三、解决方案



使用新的 build.gradle 的相关配置 , 只需要修改

org.codehaus.groovy:groovy-all

依赖的版本为 3.0.5 3.0.5 3.0.5 即可 , 其它的配置不动 ;


最终的 build.gradle 构建脚本如下 :

plugins 
    id 'groovy'
    id 'java'


group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories 
    mavenCentral()


dependencies 
    compile 'org.codehaus.groovy:groovy-all:3.0.5'
    testCompile group: 'junit', name: 'junit', version: '4.12'


修改完毕后 , Groovy 代码执行成功 ;

以上是关于错误记录Groovy 报错 ( Execution failed for task ‘:compileGroovy‘. > org/apache/tools/ant/taskdefs/Java )的主要内容,如果未能解决你的问题,请参考以下文章

错误记录Groovy 运行报错 ( Exception in thread “main“ groovy.lang.MissingMethodException: No signature of )

错误记录Android Studio 编译报错 ( Execution failed for task ‘:APP_MIDI:lintVitalRelease‘. )

错误记录Android Studio 编译报错 ( Execution failed for task ‘:APP_MIDI:lintVitalRelease‘. )

错误记录IntelliJ IDEA 编译 Groovy 报错 ( GroovyRuntimeException: This script or class could not be run. )

错误记录Groovy 工程编译报错 ( java.lang.NoClassDefFoundError: org/apache/tools/ant/util/ReaderInputStream )

错误记录Groovy 闭包使用报错 ( 闭包中不能直接使用外部对象的方法 | 需要先设置 delegate 代理 )