Why does gradle initializing sometimes take very long?
Asked Answered
C

2

6

When I execute gradle sometimes it takes very long to "Initzialize" (up to 40 seconds).

PS C:\Users\Username\project> gradle build
<------------> 0% INITIALIZING [35s]
> IDLE

It doesn't seem to matter what goal I try to execute (compileJava, test and build all take this long)

If I re-run Gradle shortly after it took so long it's very quick and executes in a matter of milliseconds or seconds but if I wait longer to re-run, it again takes very long.

This happens to me in different projects and it happens with gradlew and also with gradle.

I'm on Windows 10 and I use Gradle 7.4.

Chine answered 21/3, 2022 at 14:30 Comment(2)
compileJava is part of build, and test depends on build, so if e.g., compileJava is slow, the others are slow, too, as they need to run compileJava. Have you tried running with --profile to get more information? Or --parallel to speed things up?Mccue
@Mccue --parallel did not result in a significant performance improvement. See this scan for performance details: scans.gradle.com/s/vpdk72ozzv73m/performance/build For comparison see this performance scan when I re-run gradle right after it taking a long time (as described in the question): scans.gradle.com/s/3rts7cpwy2o4w/performance/build As you can see there is a dramatic performance improvement.Chine
K
17

This is the following issue https://github.com/gradle/gradle/issues/17955

During initialization Gradle tries to decide if file system watching is safe to enable. On a Windows machine with network drives connected and depending on your network setup it can take a long while.

The good workaround is not to disable FSW but to enable it explicitly. This will disable the discovery process that takes a long time while keeping FSW enabled and making your builds faster.

This can be done on the command line:

gradlew.bat --watch-fs yourTaskName

Or persistently in your gradle.properties

org.gradle.vfs.watch=true
Kotz answered 30/11, 2022 at 10:39 Comment(0)
I
2

EDIT: Use @eskatos answer.

Explicitly enabling filesystem watch is naturally better than disabling it.

A little bit counter-intuitive and there's no hint in the documentation, but it works.

---- old answer begins here

TLDR: disable file system watching (if you can afford it)

gradlew.bat --no-watch-fs yourTaskName

Details:

Same problem troubled me for a month. Finally I got bored enough and after examining gradle debug output (-d key) I found that just before going into IDLE the gradle reports that:

[INFO] [org.gradle.tooling.internal.provider.FileSystemWatchingBuildActionRunner] Watching the file system is configured to be enabled if available
[DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Build operation 'Build started for file system watching' started
[DEBUG] [org.gradle.internal.watch.registry.impl.DefaultFileWatcherRegistry] Started listening to file system change events

So I disabled FS watching and whoosh!

https://docs.gradle.org/current/userguide/gradle_daemon.html#enabling_and_disabling_file_system_watching

Imaimage answered 14/6, 2022 at 17:46 Comment(3)
Hi. That helped me with the issue. Did you find out the issue why fs is causing this? Previously everythiong worked fineLissome
@Lissome I think it's another Windows update, I don't know which one. Seems like it doesn't get along well with JDK WatchService API. I have issues in my app which I had not before, and the Gradle issue is probably another manifestation of the issue.Imaimage
@Imaimage note that Gradle doesn't use the JDK WatchService API, but its own implementation in github.com/gradle/native-platformFunerary

© 2022 - 2024 — McMap. All rights reserved.