Reading Bundled Maven version

Answered

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?

0
5 comments

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 use 

val mavenSettings = MavenProjectsManager.getInstance(project).getGeneralSettings()
val mavenHomeType = mavenSettings.getMavenHomeType()
val mavenVersion: String? = MavenUtil.getMavenVersion(mavenHomeType)

In the older versions, please use

val mavenSettings = MavenProjectsManager.getInstance(project).getGeneralSettings()
val mavenHome = mavenSettings.getMavenHome()  // getMavenHomeType() is not available
val mavenVersion: String? = MavenUtil.getMavenVersion(MavenUtil.getMavenHomeFile(mavenHome))
0

Hi Dmitry Kichinsky

Thanks for taking a look. From your second suggestions, it appears that mavenSettings.getMavenHome() returns a String but MavenUtil.getMavenHomeFile(mavenHome) expects a StaticResolvedMavenHomeType.

From the first suggestion, it appears that mavenSettings.getMavenHomeType() returns a MavenHomeType but MavenUtil.getMavenVersion() expects StaticResolvedMavenHomeType as well

Wondering how I can resolve this StaticResolvedMavenHomeType?

Tried things like staticOrBundled but also got “Unresolved reference: staticOrBundled”

 

0

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:
 

public static String getMavenVersion(Project project, String workingDir) {
  MavenHomeType homeType = MavenWorkspaceSettingsComponent.getInstance(project).getSettings().getGeneralSettings().getMavenHomeType();
  if (homeType instanceof StaticResolvedMavenHomeType srmt) {
    return getMavenVersion(srmt);
  }
  MavenDistribution distribution = MavenDistributionsCache.getInstance(project).getWrapper(workingDir);
  if (distribution != null) return distribution.getVersion();
  return null;
}


In IntelliJ, Maven version can be configured in different ways: 

  • Bundled (the one that comes together with IntelliJ)
  • Path (a specific path on your machine)
  • Wrapper (IntelliJ will look for Maven Wrapper in the project)
     

Bundled and Path are covered in the first branch (StaticResolvedMavenHomeType). To get the version from a Maven Wrapper, we need a workingDir – this is where your pom file is.

0

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>")

None of the following functions can be called with the arguments supplied: 
public open fun getMavenVersion(p0: File?): String? defined in org.jetbrains.idea.maven.utils.MavenUtil
public open fun getMavenVersion(p0: String!): String? defined in org.jetbrains.idea.maven.utils.MavenUtil

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?

0

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:

Path mavenHome = MavenDistributionsCache.getInstance(project).getSettingsDistribution().getMavenHome();
String version = MavenUtil.getMavenVersion(mavenHome.toFile());

If I'm not mistaken, signatures of getSettingsDistribution(), getMavenHome(), getMavenVersion(File) haven't changed since 2023.1

0

Please sign in to leave a comment.