修复initializeIntelliJPlugin超时问题
当开发插件时,常见的任务是 Run
/ Debug
/ Build
gradle 任务,但它们总是长时间卡在 initializeIntelliJPlugin
任务上。
这是 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.DefaultTaskimport org.gradle.api.provider.Propertyimport org.gradle.api.tasks.Internalimport org.gradle.api.tasks.TaskActionimport org.gradle.api.tasks.UntrackedTaskimport org.jetbrains.intellij.*import org.jetbrains.intellij.IntelliJPluginConstants.PLUGIN_GROUP_NAMEimport 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
设置为 false
或 offline
设置为 true
,就可以避免检查。现在问题很容易解决,只要满足上述任一条件即可。
在 build.gradle.kts
文件中,添加以下代码解决问题。
tasks { ... initializeIntelliJPlugin { selfUpdateCheck.set(false) } ...}