Skip to content

Commit

Permalink
fix: lookup absolute path to maven executable (#4298)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Wittlinger <wittlinger.martin@gmail.com>
  • Loading branch information
algomaster99 and MartinWitt authored Nov 25, 2021
1 parent 454dada commit 5842bfd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
34 changes: 20 additions & 14 deletions src/main/java/spoon/support/compiler/SpoonPom.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,12 @@ private static String[] readClassPath(File... classPathFiles) throws IOException
/**
* Try to guess Maven home when none is provided.
* @return the path toward maven install on the local machine.
* @throws SpoonException if path to maven executable is wrong, process is interrupted, or maven home could not be
* found.
*/
public static String guessMavenHome() {
String mvnHome = null;
try {
String[] cmd;
if (System.getProperty("os.name").contains("Windows")) {
cmd = new String[]{"mvn.cmd", "-version"};
} else if (System.getProperty("os.name").contains("Mac")) {
cmd = new String[]{"sh", "-c", "mvn -version"};
} else {
cmd = new String[]{"mvn", "-version"};
}
String[] cmd = new String[]{getPathToMavenExecutable(), "-version"};
Process p = Runtime.getRuntime().exec(cmd);
try (BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
Expand All @@ -541,15 +535,30 @@ public static String guessMavenHome() {
}
}
}

p.waitFor();
} catch (IOException e) {
throw new SpoonException("Maven home detection has failed.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SpoonException("Maven home detection was interrupted.");
}
return mvnHome;
throw new SpoonException("Couldn't find path to maven home.");
}

private static String getPathToMavenExecutable() {
String executableName;
if (System.getProperty("os.name").contains("Windows")) {
executableName = "mvn.cmd";
} else {
executableName = "mvn";
}
for (String dirname : System.getenv("PATH").split(File.pathSeparator)) {
File file = new File(dirname, executableName);
if (file.isFile() && file.canExecute()) {
return file.getAbsolutePath();
}
}
throw new SpoonException("Maven executable does not exist on PATH.");
}

/**
Expand All @@ -564,9 +573,6 @@ public static String guessMavenHome() {
public String[] buildClassPath(String mvnHome, MavenLauncher.SOURCE_TYPE sourceType, Logger LOGGER, boolean forceRefresh) {
if (mvnHome == null) {
mvnHome = guessMavenHome();
if (mvnHome == null) {
throw new SpoonException("M2_HOME must be initialized to use this MavenLauncher constructor.");
}
}
generateClassPathFile(new File(mvnHome), sourceType, LOGGER, forceRefresh);

Expand Down
13 changes: 6 additions & 7 deletions src/test/java/spoon/MavenLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,11 @@ public void mavenLauncherTestMultiModulesAndVariables() {
}

@Test
public void testGuessMavenHome() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = SpoonPom.class.getDeclaredMethod("guessMavenHome", new Class[]{});
method.setAccessible(true);
String mvnHome = (String) method.invoke(null, new Object[]{});
File mvnDir = new File(mvnHome);
assertTrue(mvnDir.exists());
assertTrue(mvnDir.isDirectory());
public void testGuessMavenHome() {
// contract: it should correctly fetch path to maven home
String pathToMavenHome = SpoonPom.guessMavenHome();
File mavenHome = new File(pathToMavenHome);
assertTrue(mavenHome.exists());
assertTrue(mavenHome.isDirectory());
}
}

0 comments on commit 5842bfd

Please # to comment.