Skip to content

Commit

Permalink
Create maven wrapper command assuming wrapper is in same directory as…
Browse files Browse the repository at this point in the history
… build file pom.xml.

Signed-off-by: Paul Gooderham <turkeyonmarblerye@gmail.com>
  • Loading branch information
turkeylurkey committed Nov 9, 2023
1 parent d86af96 commit 7a88b07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 " +
Expand All @@ -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";
Expand All @@ -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) {
Expand Down

0 comments on commit 7a88b07

Please sign in to comment.