From 7a88b07dc20f594ee6953627db98a26a7994377a Mon Sep 17 00:00:00 2001 From: Paul Gooderham Date: Wed, 8 Nov 2023 14:11:04 -0500 Subject: [PATCH] Create maven wrapper command assuming wrapper is in same directory as build file pom.xml. Signed-off-by: Paul Gooderham --- .../actions/LibertyDevStartAction.java | 6 +++++- .../LibertyDevStartContainerAction.java | 6 +++++- .../tools/intellij/util/LibertyMavenUtil.java | 19 +++++++++++-------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartAction.java b/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartAction.java index d64a9d5e7..4ac1927b0 100644 --- a/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartAction.java +++ b/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartAction.java @@ -43,7 +43,11 @@ protected void executeLibertyAction(LibertyModule libertyModule) { DebugModeHandler debugHandler = new DebugModeHandler(); String buildSettingsCmd; try { - buildSettingsCmd = projectType.equals(Constants.LIBERTY_MAVEN_PROJECT) ? LibertyMavenUtil.getMavenSettingsCmd(project) : LibertyGradleUtil.getGradleSettingsCmd(project); + if(projectType.equals(Constants.LIBERTY_MAVEN_PROJECT)) { + buildSettingsCmd = LibertyMavenUtil.getMavenSettingsCmd(project, buildFile); + } else { + buildSettingsCmd = LibertyGradleUtil.getGradleSettingsCmd(project); + } } catch (LibertyException ex) { // in this case, the settings specified to mvn or gradle are invalid and an error was launched by getMavenSettingsCmd or getGradleSettingsCmd. // Log a warning because a Logger.error creates an entry on "IDE Internal Errors", which we do not want. diff --git a/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartContainerAction.java b/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartContainerAction.java index df4e63138..070cd12b2 100644 --- a/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartContainerAction.java +++ b/src/main/java/io/openliberty/tools/intellij/actions/LibertyDevStartContainerAction.java @@ -38,7 +38,11 @@ protected void executeLibertyAction(LibertyModule libertyModule) { String projectType = libertyModule.getProjectType(); String startCmd; try { - startCmd = projectType.equals(Constants.LIBERTY_MAVEN_PROJECT) ? LibertyMavenUtil.getMavenSettingsCmd(project) + Constants.LIBERTY_MAVEN_START_CONTAINER_CMD : LibertyGradleUtil.getGradleSettingsCmd(project) + Constants.LIBERTY_GRADLE_START_CONTAINER_CMD; + if(projectType.equals(Constants.LIBERTY_MAVEN_PROJECT)) { + startCmd = LibertyMavenUtil.getMavenSettingsCmd(project, buildFile) + Constants.LIBERTY_MAVEN_START_CONTAINER_CMD; + } else { + startCmd = LibertyGradleUtil.getGradleSettingsCmd(project) + Constants.LIBERTY_GRADLE_START_CONTAINER_CMD; + } startCmd += libertyModule.getCustomStartParams(); } catch (LibertyException ex) { // in this case, the settings specified to mvn or gradle are invalid and an error was launched by getMavenSettingsCmd or getGradleSettingsCmd diff --git a/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java b/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java index 6f2fd5d59..f6d9d92c9 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java +++ b/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java @@ -9,6 +9,7 @@ *******************************************************************************/ package io.openliberty.tools.intellij.util; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.vfs.VirtualFile; @@ -35,6 +36,7 @@ import org.apache.maven.artifact.versioning.ComparableVersion; public class LibertyMavenUtil { + private static Logger LOGGER = Logger.getInstance(LibertyMavenUtil.class); /** * Return the project name given a pom.xml build file @@ -199,12 +201,12 @@ private static boolean containerVersion(String version){ * @return String command to execute in the terminal or an exception to display * @throws LibertyException */ - public static String getMavenSettingsCmd(Project project) throws LibertyException { + public static String getMavenSettingsCmd(Project project, VirtualFile buildFile) throws LibertyException { MavenGeneralSettings mavenSettings = MavenWorkspaceSettingsComponent.getInstance(project).getSettings().getGeneralSettings(); String mavenHome = mavenSettings.getMavenHome(); if (MavenServerManager.WRAPPED_MAVEN.equals(mavenHome)) { // it is set to use the wrapper - return getLocalMavenWrapper(project); + return getLocalMavenWrapper(buildFile); } else { // try to use maven home path defined in the settings return getCustomMavenPath(project, mavenHome); @@ -214,19 +216,19 @@ public static String getMavenSettingsCmd(Project project) throws LibertyExceptio /** * Get the local wrapper path for Maven that is in the project level * - * @param project liberty project + * @param buildFile the build file specified in the application project directory * @return the Maven wrapper path to be executed or an exception to display * @throws LibertyException */ - private static String getLocalMavenWrapper(Project project) throws LibertyException { + private static String getLocalMavenWrapper(VirtualFile buildFile) throws LibertyException { String mvnw = SystemInfo.isWindows ? ".\\mvnw.cmd" : "./mvnw"; - File file = new File(project.getBasePath(), mvnw); - if (!file.exists()){ + File wrapper = new File(buildFile.getParent().getPath(), mvnw); + if (!wrapper.exists()){ String translatedMessage = LocalizedResourceUtil.getMessage("maven.wrapper.does.not.exist"); throw new LibertyException("A Maven wrapper for the project could not be found. Make sure to configure a " + "valid Maven wrapper or change the build preferences for Maven inside IntelliJ Maven preferences.", translatedMessage); } - if (!file.canExecute()) { + if (!wrapper.canExecute()) { String translatedMessage = LocalizedResourceUtil.getMessage("maven.wrapper.cannot.execute"); throw new LibertyException("Could not execute Maven wrapper because the process does not have permission to " + "execute it. Consider giving executable permission for the Maven wrapper file or changing the build " + @@ -249,6 +251,7 @@ private static String getCustomMavenPath(Project project, String customMavenHome String translatedMessage = LocalizedResourceUtil.getMessage("maven.invalid.build.preference"); throw new LibertyException("Make sure to configure a valid path for Maven home path inside IntelliJ Maven preferences.", translatedMessage); } + // When a custom maven is specified, IntelliJ settings force it to point to the root folder and consider the subfolders invalid, // and consequently, it will return null. For this reason, we need to use ./bin/mvn in order to execute maven. String maven = SystemInfo.isWindows ? "mvn.cmd" : "mvn"; @@ -261,7 +264,7 @@ private static String getCustomMavenPath(Project project, String customMavenHome String mavenJdk = getMavenJdkPath(project); String mavenPath = mavenHomeFile.getAbsolutePath(); String classworldsPath = LibertyMavenUtil.getMavenClassworldsJarPath(mavenPath); - File java = new File (new File(mavenJdk, "bin"), "java"); + File java = new File (new File(mavenJdk, "bin"), "java"); // mavenJdk could be null, checked later File classworldsConf = MavenUtil.getMavenConfFile(mavenHomeFile); if (java.exists() && classworldsConf.exists() && classworldsPath != null && mavenJdk != null) {