我在项目的一个文件夹中的gradle.build中添加了一个小示例任务:
task fail {
println "ready to fail..."
throw(new Exception("This should not be reached!"))
}
当我尝试提交这个非常gradle.build文件时,它失败并显示消息:
9:53 Commit failed with error
0 files committed, 1 file failed to commit: A useful commit transaction abort!
rollback completed
abort: The system cannot find the file specified
当我查看日志时,我看到了:
Caused by: java.lang.Exception: This should not be reached!
...
2017-07-19 10:22:55,233 [ 247646] INFO - .BaseProjectImportErrorHandler - Failed to import Gradle project at 'C:/Users/543829657/workspace/dev.appl.ib.cbl' org.gradle.tooling.BuildException: Could not run build action using Gradledistribution 'https://services.gradle.org/distributions/gradle-3.2.1-bin.zip'.
Caused by: org.gradle.internal.exceptions.LocationAwareException: Build file 'C:\Users\543829657\workspace\dev.appl.ib.cbl\application\build.gradle' line: 276
...
Caused by: java.lang.Exception: This should not be reached!
...
令人惊讶的是,在日志结束时出现了关于丢失文件的奇怪消息:
2017-07-19 10:23:05,307 [ 257720] INFO - ea.execution.HgCommandExecutor - hg.exe commit
--logfile C:\Users\543829657\.IntelliJIdea2017.1\system\.hg4idea-commit.tmp application\build.gradle
2017-07-19 10:23:07,655 [ 260068] INFO - ea.execution.HgCommandExecutor - transaction abort!
rollback completed
abort: The system cannot find the file specified
2017-07-19 10:24:38,784 [ 351197] INFO - ea.execution.HgCommandExecutor - hg.exe incoming
2017-07-19 10:24:49,856 [ 362269] INFO - ea.execution.HgCommandExecutor - hg.exe outgoing
2017-07-19 10:27:32,259 [ 524672] INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from C:/Program Files/Java/jdk1.8.0_72
2017-07-19 10:27:32,299 [ 524712] INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from C:/Program Files/Java/jdk1.8.0_72
2017-07-19 10:27:32,319 [ 524732] INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: -Didea.version=2017.1.5
-Didea.resolveSourceSetDependencies=true
-Pandroid.injected.build.model.only=true
-Pandroid.injected.build.model.only.advanced=true
-Pandroid.injected.invoked.from.ide=true
-Pandroid.injected.studio.version=2017.1.5.0.0 --init-script C:\Users\543829657\AppData\Local\Temp\ijinit.gradle
2017-07-19 10:27:33,554 [ 525967] INFO - ntellij.analysis.SonarLintTask - Running SonarLint Analysis for 'build.gradle'
2017-07-19 10:27:33,751 [ 526164] INFO - ntellij.analysis.SonarLintTask - SonarLint analysis done
2017-07-19 10:27:56,522 [ 548935] INFO - pl.ProjectRootManagerComponent - project roots have changed
2017-07-19 10:27:56,835 [ 549248] INFO - .diagnostic.PerformanceWatcher - Pushing properties took 51ms; general responsiveness: ok; EDT responsiveness: ok
2017-07-19 10:27:56,947 [ 549360] INFO - pl.ProjectRootManagerComponent - project roots have changed
2017-07-19 10:27:56,957 [ 549370] INFO - indexing.UnindexedFilesUpdater - Unindexed files update canceled
2017-07-19 10:27:57,084 [ 549497] INFO - .diagnostic.PerformanceWatcher - Pushing properties took 33ms; general responsiveness: ok; EDT responsiveness: ok
2017-07-19 10:27:57,328 [ 549741] INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 244ms; general responsiveness: ok; EDT responsiveness: ok
2017-07-19 10:29:38,745 [ 651158] INFO - ea.execution.HgCommandExecutor - hg.exe incoming
2017-07-19 10:29:56,117 [ 668530] INFO - ea.execution.HgCommandExecutor - hg.exe outgoing
2017-07-19 10:34:38,752 [ 951165] INFO - ea.execution.HgCommandExecutor - hg.exe incoming
2017-07-19 10:34:54,816 [ 967229] INFO - ea.execution.HgCommandExecutor - hg.exe outgoing
我不希望IntelliJ在每次提交之前运行gradle任务。另外,我不明白为什么“找不到文件”被认为是失败的原因,而不是真实的。
当然,我真的遇到了一个真正的任务问题,但它现在已被注释掉了,因为它是一个相对纯粹的状态。这是一个发布任务,显然,我不需要它在IDE的意愿下运行。更糟糕的是,它不是从正确的目录启动而是因为该问题而创建了无效的文件夹。
P.S。当然,我已尝试重启并缓存失效。
答案 0 :(得分:1)
任务未执行 ,仅已配置。 Gradle区分配置阶段,其中构建脚本被执行(是的,有点混乱)来配置所有任务,以及执行阶段,执行所选任务(来自命令行及其依赖项)。在任务定义之后,闭包中提供的任何代码都用于配置任务,但在执行阶段期间不执行。在执行阶段期间,仅执行任务操作(由任务类型定义),doFirst
和doLast
闭包。包含代码段中任务定义的build.gradle
文件将始终失败,因为您在任何配置阶段中都会抛出异常。
现代IDE集成了Gradle(或Maven)等工具来构建项目,还可以集成其他任务。所以我认为,成功的Gradle配置是稳定的IntelliJ项目的要求。由于IntelliJ使用(辅助)任务来确定为项目提供的任务,因此如果配置阶段失败,则无法检索它们。因此,它将项目解释为腐败。我不知道Git是如何集成到IntelliJ中的,但我可以想象这种集成需要一个稳定的项目。我想你可以将Gradle项目失败的配置阶段与不可解析的Maven pom.xml
文件进行比较。
我不知道你的真实发布任务是如何定义的,但是我可以想象你通过将执行代码放入配置闭包来做同样的错误,导致任务被执行"在每个Gradle调用上,即使是通过IntelliJ提交。
task myTask() {
// Executed when the task is defined, everytime you run gradle
println 'Configuration phase'
doLast {
// Executed on task execution, only if the task is specified (CMD or task dependency)
println 'Execution phase'
}
}
请注意,Gradle有一个已弃用的功能,这可能会导致此阶段混淆。您可以(但不应该)使用<<
运算符定义任务,该运算符会自动创建doLast
闭包:
task myDeprecatedTask << {
println 'Execution phase'
}