【错误记录】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 executedFAILURE: 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.orgBUILD 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 executedFAILURE: 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/Javaat 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.orgBUILD 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.15.2.15.2.1 ;
在 Groovy 工程根目錄 配置的 build.gradle 構建腳本 內容如下 :
plugins {id 'groovy'id 'java' }group 'org.example' version '1.0-SNAPSHOT'sourceCompatibility = 1.8repositories {mavenCentral() }dependencies {compile 'org.codehaus.groovy:groovy-all:2.3.11'testCompile group: 'junit', name: 'junit', version: '4.12' }找了半天沒找出原因 , 直接創建一個新的工程 , 查看其配置 :
新工程使用 6.86.86.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.53.0.53.0.5 即可 , 其它的配置不動 ;
最終的 build.gradle 構建腳本如下 :
plugins {id 'groovy'id 'java' }group 'org.example' version '1.0-SNAPSHOT'sourceCompatibility = 1.8repositories {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】闭包 Closure (
- 下一篇: 【Groovy】闭包 Closure (