Gangnus
Gangnus

Reputation: 24464

IntelliJ runs Gradle tasks before commit. How to stop that?

I have added a small example task to the gradle.build in one of the folders of the project:

task fail {
    println "ready to fail..."
    throw(new Exception("This should not be reached!"))
}

When I am trying to commit this very gradle.build file, it fails with the message:

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

When I am looking into the log, I see there:

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!
...

And, surprisingly, at the end of the log appears that strange message about the lost file:

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  

I don't want IntelliJ to run the gradle tasks before the every commit. Also, I do not understand why "file not found" is declared as a reason for fail, instead of the real one.

Of course, really I have problems with a real task, but it is commented out for now, to hava a problem it its relatively pure state. It is a publishing task, and obviously, I don't need it running at IDE's will. Even worse, it is launched not from the correct directory and creates invalid folders because of that problem.

P.S. And of course, I have tried restart and caches invalidation.

Upvotes: 0

Views: 1615

Answers (1)

Lukas Körfer
Lukas Körfer

Reputation: 14503

The tasks do not get executed, only configured. Gradle distinguishes between the configuration phase, where the build script gets executed (yeah, a little bit confusing) to configure all tasks, and the execution phase, where selected tasks (from the command line and their dependencies) are executed. Any code provided in the closure after a task definition is meant to configure the task, but it is not executed during execution phase. Only task actions (defined by the task type), doFirst and doLast closures are executed during execution phase. A build.gradle file containing the task definition from your snippet will always fail, because you throw an exception in any configuration phase.

Modern IDEs integrate tools like Gradle (or Maven) to build the projects, but also for other tasks. So I assume, that a successful Gradle configuration is a requirement for a stable IntelliJ project. Since IntelliJ uses (helper) tasks to determine the tasks provided for a project, it cannot retrieve them, if the configuration phase fails. Therefor it interprets the project as corrupt. I don't know how Git is integrated in IntelliJ, but I could imagine that this integration requires a stable project. I guess you could compare the failing configuration phase of a Gradle project with an unparsable Maven pom.xml file.

I don't know how your real publishing task is defined, but I could imagine that you did the same mistake by putting execution code into the configuration closure, causing the task to be "executed" on each Gradle invocation, even on commit via 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'
    }
}

Please note, that Gradle has a deprecated feature, which may have caused this phase confusion. You can (but you should not) define a task with the << operator, which automatically creates a doLast closure:

task myDeprecatedTask << {
    println 'Execution phase'
}

Upvotes: 2

Related Questions