Skip to content

Commit

Permalink
feat: configure Maven wrapper usage if available
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Nov 14, 2023
1 parent f501ffa commit cde460a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -63,7 +67,6 @@ public List<VirtualFile>[] getDeploymentFiles(Module module, ProgressIndicator p
return result;
}


@Override
public String getDisplay() {
return "Maven";
Expand All @@ -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);
}
}
}
}

Expand Down Expand Up @@ -285,4 +297,5 @@ private void ensureRunnerSettings(MavenRunConfiguration mavenConfiguration) {
mavenConfiguration.setRunnerSettings(new MavenRunnerSettings());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 <a href="https://github.com/JetBrains/intellij-community/blob/fa32fd18b2ea30ef9994f9737304df918ba12055/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenWrapperSupport.kt#L177-L187">MavenWrapperSupport.getWrapperDistributionUrl</a> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public Module createModule(@NotNull ModifiableModuleModel moduleModel) throws In
try {
processDownload();
Module module = super.createModule(moduleModel);

wizardContext.getUserData(QuarkusConstants.WIZARD_TOOL_KEY).processImport(module);
telemetry.send();
return module;
Expand Down

0 comments on commit cde460a

Please sign in to comment.