Reading Bundled Maven version
Hi all, I'm working on a plugin that need to access the current Maven version being used by IntelliJ. I have tried to access the information through MavenProjectsManager and MavenGeneralSettings, which works if the maven home path is specifically defined. However, when set to Bundled Maven, I was unable to retrieve the version.
val mavenSettings = MavenProjectsManager.getInstance(project).getGeneralSettings()
val mavenHome = mavenSettings.getMavenHome() // p.s. getMavenHomeType() was not available
val mavenVersion: String? = MavenUtil.getMavenVersion(mavenHome)
Using the above, when I set Maven home directories in setting to a local version (3.9.5), I successfully got Maven version: 3.9.5
But when I set Maven home directories to Bundled (Maven 3), which happened to be 3.9.2, I got Maven version: null
Since I have seem different versions of IntelliJ IDE come with different bundled versions, how can I correctly fetch the version for the bundled Maven?
Please sign in to leave a comment.
Hi Sophia73583 ,
The code will depend on the IntelliJ version that you create a plugin for. The API has changed over time.
In the latest versions (where
getMavenHomeType()
is available), please useIn the older versions, please use
Hi Dmitry Kichinsky,
Thanks for taking a look. From your second suggestions, it appears that
mavenSettings.getMavenHome()
returns a String butMavenUtil.getMavenHomeFile(mavenHome)
expects a StaticResolvedMavenHomeType.From the first suggestion, it appears that
mavenSettings.getMavenHomeType()
returns a MavenHomeType butMavenUtil.getMavenVersion()
expects StaticResolvedMavenHomeType as wellWondering how I can resolve this StaticResolvedMavenHomeType?
Tried things like staticOrBundled but also got “Unresolved reference: staticOrBundled”
Sophia73583 , would this method work for you?
org.jetbrains.idea.maven.utils.MavenUtil#getMavenVersion(com.intellij.openapi.project.Project, java.lang.String)
Here is its implementation:
In IntelliJ, Maven version can be configured in different ways:
Bundled and Path are covered in the first branch (
StaticResolvedMavenHomeType
). To get the version from a Maven Wrapper, we need aworkingDir
– this is where your pom file is.Hi Dmitry Kichinsky, thank you for taking a look again.
I did find that function with the project and workingDir as input, but when I tried using it, it wasn't able to build for the sandbox version I was using (Core 2023.2 Sandbox):
val version: String? = MavenUtil.getMavenVersion(project, "<redacted root path of project>")
Update:
It appears that MavenUtil.getMavenVersion(project, workingDir) works for Core 2023.3 for fetching bundled Maven version. However, our plugin need to support older versions from 2023.1, so I likely cannot use it as it cannot compile for earlier versions of plugins.
I wonder if there is a way to successfully fetch bundled version for <2023.2 IDEA versions? Or if there are documentation on which MavenUtil functions are available/deprecated in which versions?
MavenUtil
code changed over time, here are the versions for 2023.1, 2023.2, 2023.3 (231, 232, 233 branches respectively)https://github.com/JetBrains/intellij-community/blob/231/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java
https://github.com/JetBrains/intellij-community/blob/232/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java
https://github.com/JetBrains/intellij-community/blob/233/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java
--
And here's another option to try:
If I'm not mistaken, signatures of
getSettingsDistribution()
,getMavenHome()
,getMavenVersion(File)
haven't changed since 2023.1