From 95018ecf14b233c6e13b12a336e5f3fff43e378c Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Tue, 14 Nov 2023 15:21:34 +0100 Subject: [PATCH] feat: configure Maven wrapper usage if available Signed-off-by: Fred Bricon --- .../buildtool/maven/MavenToolDelegate.java | 16 +++++- .../buildtool/maven/MavenWrapperUtils.java | 49 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenWrapperUtils.java diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java index c6cd1c040..c008aef0f 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java @@ -15,6 +15,7 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectUtil; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; @@ -23,7 +24,6 @@ import com.redhat.devtools.intellij.quarkus.buildtool.ProjectImportListener; import com.redhat.devtools.intellij.quarkus.run.QuarkusRunConfiguration; import com.redhat.devtools.intellij.quarkus.buildtool.BuildToolDelegate; -import com.redhat.devtools.intellij.quarkus.settings.UserDefinedQuarkusSettings; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.idea.maven.execution.MavenRunConfiguration; @@ -33,9 +33,11 @@ import org.jetbrains.idea.maven.model.MavenArtifactInfo; import org.jetbrains.idea.maven.model.MavenId; import org.jetbrains.idea.maven.project.MavenImportListener; +import org.jetbrains.idea.maven.project.MavenGeneralSettings; import org.jetbrains.idea.maven.project.MavenProject; import org.jetbrains.idea.maven.project.MavenProjectsManager; import org.jetbrains.idea.maven.server.MavenEmbedderWrapper; +import org.jetbrains.idea.maven.server.MavenServerManager; import org.jetbrains.idea.maven.utils.MavenProcessCanceledException; import org.jetbrains.idea.maven.utils.MavenUtil; import org.slf4j.Logger; @@ -45,6 +47,8 @@ import java.lang.reflect.Method; import java.util.*; +import static com.redhat.devtools.intellij.quarkus.buildtool.maven.MavenWrapperUtils.getWrapperDistributionUrl; + public class MavenToolDelegate implements BuildToolDelegate { private static final Logger LOGGER = LoggerFactory.getLogger(MavenToolDelegate.class); @@ -63,7 +67,6 @@ public List[] getDeploymentFiles(Module module, ProgressIndicator p return result; } - @Override public String getDisplay() { return "Maven"; @@ -85,6 +88,15 @@ public void processImport(Module module) { if (pomFile != null) { MavenProjectsManager mavenProjectsManager = MavenProjectsManager.getInstance(project); mavenProjectsManager.addManagedFiles(Collections.singletonList(pomFile)); + MavenGeneralSettings mavenSettings = mavenProjectsManager.getGeneralSettings(); + //TODO Once 2023-3 is the minimal required version, the following code can be removed + var distributionUrl = getWrapperDistributionUrl(ProjectUtil.guessProjectDir(project)); + if (distributionUrl != null) { + String mavenHome = mavenSettings.getMavenHome(); + if (!MavenServerManager.WRAPPED_MAVEN.equals(mavenHome)){ + mavenSettings.setMavenHome(MavenServerManager.WRAPPED_MAVEN); + } + } } } diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenWrapperUtils.java b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenWrapperUtils.java new file mode 100644 index 000000000..7fce87894 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenWrapperUtils.java @@ -0,0 +1,49 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.redhat.devtools.intellij.quarkus.buildtool.maven; + +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Properties; + +/** + * Port of MavenWrapperSupport.getWrapperDistributionUrl to Java + */ +public class MavenWrapperUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(MavenWrapperUtils.class); + private static final String DISTRIBUTION_URL_PROPERTY = "distributionUrl"; + private MavenWrapperUtils(){} + + public static String getWrapperDistributionUrl(VirtualFile baseDir) { + VirtualFile wrapperPropertiesFile = getWrapperProperties(baseDir); + if (wrapperPropertiesFile == null) { + return null; + } + + try (ByteArrayInputStream stream = new ByteArrayInputStream(wrapperPropertiesFile.contentsToByteArray(true))) { + Properties properties = new Properties(); + properties.load(stream); + return properties.getProperty(DISTRIBUTION_URL_PROPERTY); + } catch (IOException e) { + LOGGER.warn("Failed to read Maven Wrapper from "+baseDir, e); + } + return null; + } + + public static @Nullable VirtualFile getWrapperProperties(VirtualFile baseDir) { + if (baseDir != null) { + VirtualFile mvnDir = baseDir.findChild(".mvn"); + if (mvnDir != null) { + VirtualFile wrapperDir = mvnDir.findChild("wrapper"); + if (wrapperDir != null) { + return wrapperDir.findChild("maven-wrapper.properties"); + } + } + } + return null; + } +} \ No newline at end of file