Adding a windows based 'where' checker for mvn executable.
*/
package com.blackducksoftware.integration.hub.packman.parser.maven;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.util.List;
+import java.util.Map;
+import org.assertj.core.internal.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -25,6 +30,8 @@ import com.blackducksoftware.integration.hub.bdio.simple.model.DependencyNode;
import com.blackducksoftware.integration.hub.packman.parser.Packager;
import com.blackducksoftware.integration.hub.packman.parser.maven.parsers.MavenOutputParser;
+import okhttp3.internal.platform.Platform;
+
public class MavenPackager extends Packager {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -45,7 +52,17 @@ public class MavenPackager extends Packager {
logger.info("writing maven outputsteram to " + mavenOutputFile.getAbsolutePath());
final File sourceDirectoryFile = new File(sourceDirectory);
- final ProcessBuilder processBuilder = new ProcessBuilder(System.getenv("M2"), "dependency:tree");
+ String mavenPath = null;
+ try {
+ mavenPath = findExecutable("mvn"); // TODO: Create enum
+ } catch (Exception ignore) {
+ }
+
+ if (mavenPath == null)
+ mavenPath = System.getenv("M2");
+
+ final ProcessBuilder processBuilder = new ProcessBuilder(mavenPath, "dependency:tree");
+
processBuilder.directory(sourceDirectoryFile);
processBuilder.redirectOutput(Redirect.to(mavenOutputFile));
@@ -89,4 +106,32 @@ public class MavenPackager extends Packager {
}
}
+ private String findExecutable(String executable) throws Exception {
+ String path = null;
+ String command = "which";
+ if (System.getProperty("os.name").toLowerCase().contains("windows")) {
+ command = "where";
+ }
+ final ProcessBuilder processBuilder = new ProcessBuilder(command, executable);
+
+ final Process process = processBuilder.start();
+ try {
+
+ int errCode = process.waitFor();
+ if (errCode == 0) {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+
+ String line = null;
+ while ((line = reader.readLine()) != null) {
+ path = (line); // the last line will be the execution line
+ }
+ }
+ } catch (final InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ if (path == null)
+ throw new Exception("Unable to determine path for: " + executable);
+ return path;
+ }
+
}
