跳转到内容

修复initializeIntelliJPlugin超时问题

当开发插件时,常见的任务是 Run / Debug / Build gradle 任务,但它们总是长时间卡在 initializeIntelliJPlugin 任务上。

这是 InitializeIntelliJPluginTask.kt 文件的源代码。

InitializeIntelliJPluginTask.kt
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.intellij.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.UntrackedTask
import org.jetbrains.intellij.*
import org.jetbrains.intellij.IntelliJPluginConstants.PLUGIN_GROUP_NAME
import org.jetbrains.intellij.utils.LatestVersionResolver
@UntrackedTask(because = "Should always be run to initialize the plugin")
abstract class InitializeIntelliJPluginTask : DefaultTask() {
12 collapsed lines
@get:Internal
abstract val offline: Property<Boolean>
@get:Internal
abstract val selfUpdateCheck: Property<Boolean>
init {
group = PLUGIN_GROUP_NAME
description = "Initializes the Gradle IntelliJ Plugin"
}
private val context = logCategory()
@TaskAction
fun initialize() {
checkPluginVersion()
}
17 collapsed lines
private fun checkPluginVersion() {
if (!selfUpdateCheck.get() || offline.get()) {
return
}
try {
val version = getCurrentPluginVersion()
?.let(Version::parse)
.or { Version() }
val latestVersion = LatestVersionResolver.fromGitHub(IntelliJPluginConstants.PLUGIN_NAME, IntelliJPluginConstants.GITHUB_REPOSITORY)
if (version < Version.parse(latestVersion)) {
warn(context, "${IntelliJPluginConstants.PLUGIN_NAME} is outdated: $version. Update `${IntelliJPluginConstants.PLUGIN_ID}` to: $latestVersion")
}
} catch (e: Exception) {
error(context, e.message.orEmpty(), e)
}
}
}

It mainly does one thing: checkPluginVersion(), so it is likely because of the network connection. In other words, if the connection to the server is slow, this problem may occur.

checkPluginVersion() 方法中,有一个判断条件。只要 selfUpdateCheck 设置为 falseoffline 设置为 true,就可以避免检查。现在问题很容易解决,只要满足上述任一条件即可。

build.gradle.kts 文件中,添加以下代码解决问题。

tasks {
...
initializeIntelliJPlugin {
selfUpdateCheck.set(false)
}
...
}