VS Code Gradle Extension Settings When Gradle Wrapper is Unavailable
To reach a broader audience, this article has been translated from Japanese.
You can find the original version here.
Introduction
#Gradle Wrapper and Maven Wrapper are convenient because they download the necessary version of the binary even if Gradle or Maven is not installed on the system.
Gradle binaries are distributed via URLs like service.gradle.org/distributions/gradle-8.xx.x-bin.zip
. In environments with access restrictions, such as within a company, the download may fail, resulting in a build error.
Building the Project Itself
#If Gradle binaries are distributed on an internal site, you can build the project by setting the distributionUrl
in the project's gradle/gradle-wrapper.properties.
If such a site does not exist, you will need to manually install Gradle. Instead of using the gradlew command within the project, you can build using the gradle command that is in your path.
VS Code Gradle Extension Settings
#In VS Code, the Gradle Extension refers to the project's gradle/gradle-wrapper.properties by default. Therefore, if Gradle Wrapper is distributed on an internal site, it will download and build the binary specified by the distributionUrl
.
If the Gradle binary cannot be obtained, the Gradle build will fail, and the Java extension will fail to construct the project information.
You can edit the code, but code completion, JavaDoc hover display, and refactoring will not work properly.
Therefore, let's change the Gradle extension settings to use the local gradle command instead of the Gradle Wrapper.
Search for gradle
in the settings and uncheck Java > Import > Gradle > Wrapper: Enabled
to prevent it from referring to maven/gradle-wrapper.properties. Next, set the path where Gradle is installed in Java > Import > Gradle: Home
.
In this example, it is set in the workspace, and since the Spring Boot project folder is opened directly in VS Code, .vscode/settings.json is created as follows. You can also edit this file directly.
{
"java.import.gradle.version": "",
"java.import.gradle.wrapper.enabled": false,
"gradle.nestedProjects": false,
"java.import.gradle.home": "/Users/kondoh/lib/gradle-8.10.2"
}
If gradle.nestedProjects
in settings.json is set to true, it will manage not only the root project but also projects placed in subdirectories.
For recommended Java environment setups in VS Code, please refer to the following article.
2024 Edition! Setting Up a Java Development Environment with VS Code
Reopening the project with this setting will result in a successful Gradle build, and the project information will be successfully loaded.
Conclusion
#This was an introduction to how to handle the situation on the VS Code side when the Gradle Wrapper is unavailable.
I haven't tried it with Maven, but there seems to be a configuration value like Maven > Executable: Prefer Maven Wrapper
in the Maven extension, so it seems manageable.