@Test(expected = IOException.class) public void testMavenClasspathTimeout() throws Exception { Path workingDirectory = Paths.get(System.getProperty("user.dir") + File.separator + "src/test/resources"); List<String> cmd = new LinkedList<String>(); cmd.add(workingDirectory.toString() + File.separator + "test-timeout.sh"); mavenClasspathHelper.executeMavenProcess(workingDirectory.toString(), cmd); }
protected String executeMavenProcess(String workingDirectory, List<String> cmd) throws IOException { String mavenOutputFilePath = workingDirectory + File.separator + "maven.output.txt"; if (LOG.isDebugEnabled()) { LOG.debug("Starting to execute process for command: " + cmd + "\n\tworkingDirectory: " + workingDirectory + "\n\tmvn output log: " + mavenOutputFilePath); } File cwd = null; long startTime = System.currentTimeMillis(); long totalTime = 0; Process process = null; StringBuilder outputMessage = new StringBuilder(); if (workingDirectory == null || workingDirectory.isEmpty() || cmd == null || cmd.isEmpty() || cmd.get(0).isEmpty()) { LOG.error("Invalid workingDirectory: " + workingDirectory + " or command: " + cmd + " specified"); throw new IllegalArgumentException("Working directory and command must be specified"); } if (workingDirectory != null) { cwd = new File(workingDirectory); if (!cwd.exists()) { throw new IOException("Working Directory does not exist: " + workingDirectory); } else if (!cwd.isDirectory()) { throw new IOException("Working Directory is not a directory: " + workingDirectory); } } InputStream stdout = null; BufferedReader reader = null; long lastTimeDebugPrinted = 0; try { ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.redirectErrorStream(true); processBuilder.directory(new File(workingDirectory)); process = processBuilder.start(); boolean running = true; int r = -1; if (process != null) { try { stdout = process.getInputStream(); reader = new BufferedReader(new InputStreamReader(stdout)); } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } Path mavenOutputFile = Paths.get(mavenOutputFilePath); while (running) { Thread.sleep(getProcessCheckInterval()); totalTime = totalTime + getProcessCheckInterval(); if (process != null) { try { StringBuilder currentUpdate = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { if (outputMessage.length() > 0) { outputMessage.append(line).append("\n"); currentUpdate.append(line).append("\n"); } else { outputMessage.append(line); } } if (currentUpdate.length() > 0) { try { Files.write(mavenOutputFile, currentUpdate.toString().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (Exception e) { LOG.error( "Error writing output for command: " + cmd + " to file: " + mavenOutputFilePath, e); } } } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } if (totalTime >= getProcessMaxExecutionTime()) { process.destroy(); running = false; throw new IOException("Command " + cmd + " killed due to exceeding max process time"); } try { r = process.exitValue(); running = false; if (r != 0) { outputMessage.append("Command returned non-zero exit code: " + r); } } catch (IllegalThreadStateException itse) { if (LOG.isDebugEnabled() && (System.currentTimeMillis() - lastTimeDebugPrinted > 10000)) { String timeHMS = StringUtil.formatTimeHMS(System.currentTimeMillis() - startTime); LOG.debug("Command still running: " + cmd + ", msg: " + itse.getMessage() + ", elapsed: " + timeHMS); lastTimeDebugPrinted = System.currentTimeMillis(); } } } } catch (IllegalArgumentException iae) { String errMsg = "Unable to execute command: " + cmd + " error: " + iae.getMessage(); LOG.error(errMsg, iae); throw new IOException(errMsg, iae); } catch (IOException ioe) { String errMsg = "Unable to execute command: " + cmd + " error: " + ioe.getMessage(); LOG.error(errMsg, ioe); throw new IOException(errMsg, ioe); } catch (InterruptedException intre) { String errMsg = "Command interrupted cmd: " + cmd + " error: " + intre.getMessage(); LOG.error(errMsg, intre); throw new IOException(errMsg, intre); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { LOG.warn("Error closing BufferedReader for cmd: " + cmd, e); } finally { reader = null; } } if (stdout != null) { try { stdout.close(); } catch (Exception e) { LOG.warn("Error closing InputStream for cmd: " + cmd, e); } finally { stdout = null; } } if (process != null) { try { process.destroy(); } catch (Throwable t) { LOG.error( "Error while attempting to destroy process for command: " + cmd + " msg: " + t.getMessage(), t); } } if (LOG.isDebugEnabled()) { LOG.debug("Exiting process for command: " + cmd + " exec time(ms): " + (System.currentTimeMillis() - startTime) + " workingDirectory: " + workingDirectory); } } if (LOG.isDebugEnabled()) { LOG.debug("Output for command: " + cmd + "\n" + outputMessage.toString()); } return outputMessage.toString(); }
MavenClasspathHelper { protected String executeMavenProcess(String workingDirectory, List<String> cmd) throws IOException { String mavenOutputFilePath = workingDirectory + File.separator + "maven.output.txt"; if (LOG.isDebugEnabled()) { LOG.debug("Starting to execute process for command: " + cmd + "\n\tworkingDirectory: " + workingDirectory + "\n\tmvn output log: " + mavenOutputFilePath); } File cwd = null; long startTime = System.currentTimeMillis(); long totalTime = 0; Process process = null; StringBuilder outputMessage = new StringBuilder(); if (workingDirectory == null || workingDirectory.isEmpty() || cmd == null || cmd.isEmpty() || cmd.get(0).isEmpty()) { LOG.error("Invalid workingDirectory: " + workingDirectory + " or command: " + cmd + " specified"); throw new IllegalArgumentException("Working directory and command must be specified"); } if (workingDirectory != null) { cwd = new File(workingDirectory); if (!cwd.exists()) { throw new IOException("Working Directory does not exist: " + workingDirectory); } else if (!cwd.isDirectory()) { throw new IOException("Working Directory is not a directory: " + workingDirectory); } } InputStream stdout = null; BufferedReader reader = null; long lastTimeDebugPrinted = 0; try { ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.redirectErrorStream(true); processBuilder.directory(new File(workingDirectory)); process = processBuilder.start(); boolean running = true; int r = -1; if (process != null) { try { stdout = process.getInputStream(); reader = new BufferedReader(new InputStreamReader(stdout)); } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } Path mavenOutputFile = Paths.get(mavenOutputFilePath); while (running) { Thread.sleep(getProcessCheckInterval()); totalTime = totalTime + getProcessCheckInterval(); if (process != null) { try { StringBuilder currentUpdate = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { if (outputMessage.length() > 0) { outputMessage.append(line).append("\n"); currentUpdate.append(line).append("\n"); } else { outputMessage.append(line); } } if (currentUpdate.length() > 0) { try { Files.write(mavenOutputFile, currentUpdate.toString().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (Exception e) { LOG.error( "Error writing output for command: " + cmd + " to file: " + mavenOutputFilePath, e); } } } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } if (totalTime >= getProcessMaxExecutionTime()) { process.destroy(); running = false; throw new IOException("Command " + cmd + " killed due to exceeding max process time"); } try { r = process.exitValue(); running = false; if (r != 0) { outputMessage.append("Command returned non-zero exit code: " + r); } } catch (IllegalThreadStateException itse) { if (LOG.isDebugEnabled() && (System.currentTimeMillis() - lastTimeDebugPrinted > 10000)) { String timeHMS = StringUtil.formatTimeHMS(System.currentTimeMillis() - startTime); LOG.debug("Command still running: " + cmd + ", msg: " + itse.getMessage() + ", elapsed: " + timeHMS); lastTimeDebugPrinted = System.currentTimeMillis(); } } } } catch (IllegalArgumentException iae) { String errMsg = "Unable to execute command: " + cmd + " error: " + iae.getMessage(); LOG.error(errMsg, iae); throw new IOException(errMsg, iae); } catch (IOException ioe) { String errMsg = "Unable to execute command: " + cmd + " error: " + ioe.getMessage(); LOG.error(errMsg, ioe); throw new IOException(errMsg, ioe); } catch (InterruptedException intre) { String errMsg = "Command interrupted cmd: " + cmd + " error: " + intre.getMessage(); LOG.error(errMsg, intre); throw new IOException(errMsg, intre); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { LOG.warn("Error closing BufferedReader for cmd: " + cmd, e); } finally { reader = null; } } if (stdout != null) { try { stdout.close(); } catch (Exception e) { LOG.warn("Error closing InputStream for cmd: " + cmd, e); } finally { stdout = null; } } if (process != null) { try { process.destroy(); } catch (Throwable t) { LOG.error( "Error while attempting to destroy process for command: " + cmd + " msg: " + t.getMessage(), t); } } if (LOG.isDebugEnabled()) { LOG.debug("Exiting process for command: " + cmd + " exec time(ms): " + (System.currentTimeMillis() - startTime) + " workingDirectory: " + workingDirectory); } } if (LOG.isDebugEnabled()) { LOG.debug("Output for command: " + cmd + "\n" + outputMessage.toString()); } return outputMessage.toString(); } }
MavenClasspathHelper { protected String executeMavenProcess(String workingDirectory, List<String> cmd) throws IOException { String mavenOutputFilePath = workingDirectory + File.separator + "maven.output.txt"; if (LOG.isDebugEnabled()) { LOG.debug("Starting to execute process for command: " + cmd + "\n\tworkingDirectory: " + workingDirectory + "\n\tmvn output log: " + mavenOutputFilePath); } File cwd = null; long startTime = System.currentTimeMillis(); long totalTime = 0; Process process = null; StringBuilder outputMessage = new StringBuilder(); if (workingDirectory == null || workingDirectory.isEmpty() || cmd == null || cmd.isEmpty() || cmd.get(0).isEmpty()) { LOG.error("Invalid workingDirectory: " + workingDirectory + " or command: " + cmd + " specified"); throw new IllegalArgumentException("Working directory and command must be specified"); } if (workingDirectory != null) { cwd = new File(workingDirectory); if (!cwd.exists()) { throw new IOException("Working Directory does not exist: " + workingDirectory); } else if (!cwd.isDirectory()) { throw new IOException("Working Directory is not a directory: " + workingDirectory); } } InputStream stdout = null; BufferedReader reader = null; long lastTimeDebugPrinted = 0; try { ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.redirectErrorStream(true); processBuilder.directory(new File(workingDirectory)); process = processBuilder.start(); boolean running = true; int r = -1; if (process != null) { try { stdout = process.getInputStream(); reader = new BufferedReader(new InputStreamReader(stdout)); } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } Path mavenOutputFile = Paths.get(mavenOutputFilePath); while (running) { Thread.sleep(getProcessCheckInterval()); totalTime = totalTime + getProcessCheckInterval(); if (process != null) { try { StringBuilder currentUpdate = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { if (outputMessage.length() > 0) { outputMessage.append(line).append("\n"); currentUpdate.append(line).append("\n"); } else { outputMessage.append(line); } } if (currentUpdate.length() > 0) { try { Files.write(mavenOutputFile, currentUpdate.toString().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (Exception e) { LOG.error( "Error writing output for command: " + cmd + " to file: " + mavenOutputFilePath, e); } } } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } if (totalTime >= getProcessMaxExecutionTime()) { process.destroy(); running = false; throw new IOException("Command " + cmd + " killed due to exceeding max process time"); } try { r = process.exitValue(); running = false; if (r != 0) { outputMessage.append("Command returned non-zero exit code: " + r); } } catch (IllegalThreadStateException itse) { if (LOG.isDebugEnabled() && (System.currentTimeMillis() - lastTimeDebugPrinted > 10000)) { String timeHMS = StringUtil.formatTimeHMS(System.currentTimeMillis() - startTime); LOG.debug("Command still running: " + cmd + ", msg: " + itse.getMessage() + ", elapsed: " + timeHMS); lastTimeDebugPrinted = System.currentTimeMillis(); } } } } catch (IllegalArgumentException iae) { String errMsg = "Unable to execute command: " + cmd + " error: " + iae.getMessage(); LOG.error(errMsg, iae); throw new IOException(errMsg, iae); } catch (IOException ioe) { String errMsg = "Unable to execute command: " + cmd + " error: " + ioe.getMessage(); LOG.error(errMsg, ioe); throw new IOException(errMsg, ioe); } catch (InterruptedException intre) { String errMsg = "Command interrupted cmd: " + cmd + " error: " + intre.getMessage(); LOG.error(errMsg, intre); throw new IOException(errMsg, intre); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { LOG.warn("Error closing BufferedReader for cmd: " + cmd, e); } finally { reader = null; } } if (stdout != null) { try { stdout.close(); } catch (Exception e) { LOG.warn("Error closing InputStream for cmd: " + cmd, e); } finally { stdout = null; } } if (process != null) { try { process.destroy(); } catch (Throwable t) { LOG.error( "Error while attempting to destroy process for command: " + cmd + " msg: " + t.getMessage(), t); } } if (LOG.isDebugEnabled()) { LOG.debug("Exiting process for command: " + cmd + " exec time(ms): " + (System.currentTimeMillis() - startTime) + " workingDirectory: " + workingDirectory); } } if (LOG.isDebugEnabled()) { LOG.debug("Output for command: " + cmd + "\n" + outputMessage.toString()); } return outputMessage.toString(); }  }
MavenClasspathHelper { protected String executeMavenProcess(String workingDirectory, List<String> cmd) throws IOException { String mavenOutputFilePath = workingDirectory + File.separator + "maven.output.txt"; if (LOG.isDebugEnabled()) { LOG.debug("Starting to execute process for command: " + cmd + "\n\tworkingDirectory: " + workingDirectory + "\n\tmvn output log: " + mavenOutputFilePath); } File cwd = null; long startTime = System.currentTimeMillis(); long totalTime = 0; Process process = null; StringBuilder outputMessage = new StringBuilder(); if (workingDirectory == null || workingDirectory.isEmpty() || cmd == null || cmd.isEmpty() || cmd.get(0).isEmpty()) { LOG.error("Invalid workingDirectory: " + workingDirectory + " or command: " + cmd + " specified"); throw new IllegalArgumentException("Working directory and command must be specified"); } if (workingDirectory != null) { cwd = new File(workingDirectory); if (!cwd.exists()) { throw new IOException("Working Directory does not exist: " + workingDirectory); } else if (!cwd.isDirectory()) { throw new IOException("Working Directory is not a directory: " + workingDirectory); } } InputStream stdout = null; BufferedReader reader = null; long lastTimeDebugPrinted = 0; try { ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.redirectErrorStream(true); processBuilder.directory(new File(workingDirectory)); process = processBuilder.start(); boolean running = true; int r = -1; if (process != null) { try { stdout = process.getInputStream(); reader = new BufferedReader(new InputStreamReader(stdout)); } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } Path mavenOutputFile = Paths.get(mavenOutputFilePath); while (running) { Thread.sleep(getProcessCheckInterval()); totalTime = totalTime + getProcessCheckInterval(); if (process != null) { try { StringBuilder currentUpdate = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { if (outputMessage.length() > 0) { outputMessage.append(line).append("\n"); currentUpdate.append(line).append("\n"); } else { outputMessage.append(line); } } if (currentUpdate.length() > 0) { try { Files.write(mavenOutputFile, currentUpdate.toString().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (Exception e) { LOG.error( "Error writing output for command: " + cmd + " to file: " + mavenOutputFilePath, e); } } } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } if (totalTime >= getProcessMaxExecutionTime()) { process.destroy(); running = false; throw new IOException("Command " + cmd + " killed due to exceeding max process time"); } try { r = process.exitValue(); running = false; if (r != 0) { outputMessage.append("Command returned non-zero exit code: " + r); } } catch (IllegalThreadStateException itse) { if (LOG.isDebugEnabled() && (System.currentTimeMillis() - lastTimeDebugPrinted > 10000)) { String timeHMS = StringUtil.formatTimeHMS(System.currentTimeMillis() - startTime); LOG.debug("Command still running: " + cmd + ", msg: " + itse.getMessage() + ", elapsed: " + timeHMS); lastTimeDebugPrinted = System.currentTimeMillis(); } } } } catch (IllegalArgumentException iae) { String errMsg = "Unable to execute command: " + cmd + " error: " + iae.getMessage(); LOG.error(errMsg, iae); throw new IOException(errMsg, iae); } catch (IOException ioe) { String errMsg = "Unable to execute command: " + cmd + " error: " + ioe.getMessage(); LOG.error(errMsg, ioe); throw new IOException(errMsg, ioe); } catch (InterruptedException intre) { String errMsg = "Command interrupted cmd: " + cmd + " error: " + intre.getMessage(); LOG.error(errMsg, intre); throw new IOException(errMsg, intre); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { LOG.warn("Error closing BufferedReader for cmd: " + cmd, e); } finally { reader = null; } } if (stdout != null) { try { stdout.close(); } catch (Exception e) { LOG.warn("Error closing InputStream for cmd: " + cmd, e); } finally { stdout = null; } } if (process != null) { try { process.destroy(); } catch (Throwable t) { LOG.error( "Error while attempting to destroy process for command: " + cmd + " msg: " + t.getMessage(), t); } } if (LOG.isDebugEnabled()) { LOG.debug("Exiting process for command: " + cmd + " exec time(ms): " + (System.currentTimeMillis() - startTime) + " workingDirectory: " + workingDirectory); } } if (LOG.isDebugEnabled()) { LOG.debug("Output for command: " + cmd + "\n" + outputMessage.toString()); } return outputMessage.toString(); }  String generateClasspathFromPom(String pom); Integer cleanupTempFolders(); long getProcessCheckInterval(); void setProcessCheckInterval(long processCheckInterval); long getProcessMaxExecutionTime(); void setProcessMaxExecutionTime(long processMaxExecutionTime); String getBaseFolder(); void setBaseFolder(String baseFolder); }
MavenClasspathHelper { protected String executeMavenProcess(String workingDirectory, List<String> cmd) throws IOException { String mavenOutputFilePath = workingDirectory + File.separator + "maven.output.txt"; if (LOG.isDebugEnabled()) { LOG.debug("Starting to execute process for command: " + cmd + "\n\tworkingDirectory: " + workingDirectory + "\n\tmvn output log: " + mavenOutputFilePath); } File cwd = null; long startTime = System.currentTimeMillis(); long totalTime = 0; Process process = null; StringBuilder outputMessage = new StringBuilder(); if (workingDirectory == null || workingDirectory.isEmpty() || cmd == null || cmd.isEmpty() || cmd.get(0).isEmpty()) { LOG.error("Invalid workingDirectory: " + workingDirectory + " or command: " + cmd + " specified"); throw new IllegalArgumentException("Working directory and command must be specified"); } if (workingDirectory != null) { cwd = new File(workingDirectory); if (!cwd.exists()) { throw new IOException("Working Directory does not exist: " + workingDirectory); } else if (!cwd.isDirectory()) { throw new IOException("Working Directory is not a directory: " + workingDirectory); } } InputStream stdout = null; BufferedReader reader = null; long lastTimeDebugPrinted = 0; try { ProcessBuilder processBuilder = new ProcessBuilder(cmd); processBuilder.redirectErrorStream(true); processBuilder.directory(new File(workingDirectory)); process = processBuilder.start(); boolean running = true; int r = -1; if (process != null) { try { stdout = process.getInputStream(); reader = new BufferedReader(new InputStreamReader(stdout)); } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } Path mavenOutputFile = Paths.get(mavenOutputFilePath); while (running) { Thread.sleep(getProcessCheckInterval()); totalTime = totalTime + getProcessCheckInterval(); if (process != null) { try { StringBuilder currentUpdate = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { if (outputMessage.length() > 0) { outputMessage.append(line).append("\n"); currentUpdate.append(line).append("\n"); } else { outputMessage.append(line); } } if (currentUpdate.length() > 0) { try { Files.write(mavenOutputFile, currentUpdate.toString().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (Exception e) { LOG.error( "Error writing output for command: " + cmd + " to file: " + mavenOutputFilePath, e); } } } catch (Exception e) { LOG.error("Error reading output for command: " + cmd + " msg: " + e.getMessage(), e); } } if (totalTime >= getProcessMaxExecutionTime()) { process.destroy(); running = false; throw new IOException("Command " + cmd + " killed due to exceeding max process time"); } try { r = process.exitValue(); running = false; if (r != 0) { outputMessage.append("Command returned non-zero exit code: " + r); } } catch (IllegalThreadStateException itse) { if (LOG.isDebugEnabled() && (System.currentTimeMillis() - lastTimeDebugPrinted > 10000)) { String timeHMS = StringUtil.formatTimeHMS(System.currentTimeMillis() - startTime); LOG.debug("Command still running: " + cmd + ", msg: " + itse.getMessage() + ", elapsed: " + timeHMS); lastTimeDebugPrinted = System.currentTimeMillis(); } } } } catch (IllegalArgumentException iae) { String errMsg = "Unable to execute command: " + cmd + " error: " + iae.getMessage(); LOG.error(errMsg, iae); throw new IOException(errMsg, iae); } catch (IOException ioe) { String errMsg = "Unable to execute command: " + cmd + " error: " + ioe.getMessage(); LOG.error(errMsg, ioe); throw new IOException(errMsg, ioe); } catch (InterruptedException intre) { String errMsg = "Command interrupted cmd: " + cmd + " error: " + intre.getMessage(); LOG.error(errMsg, intre); throw new IOException(errMsg, intre); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { LOG.warn("Error closing BufferedReader for cmd: " + cmd, e); } finally { reader = null; } } if (stdout != null) { try { stdout.close(); } catch (Exception e) { LOG.warn("Error closing InputStream for cmd: " + cmd, e); } finally { stdout = null; } } if (process != null) { try { process.destroy(); } catch (Throwable t) { LOG.error( "Error while attempting to destroy process for command: " + cmd + " msg: " + t.getMessage(), t); } } if (LOG.isDebugEnabled()) { LOG.debug("Exiting process for command: " + cmd + " exec time(ms): " + (System.currentTimeMillis() - startTime) + " workingDirectory: " + workingDirectory); } } if (LOG.isDebugEnabled()) { LOG.debug("Output for command: " + cmd + "\n" + outputMessage.toString()); } return outputMessage.toString(); }  String generateClasspathFromPom(String pom); Integer cleanupTempFolders(); long getProcessCheckInterval(); void setProcessCheckInterval(long processCheckInterval); long getProcessMaxExecutionTime(); void setProcessMaxExecutionTime(long processMaxExecutionTime); String getBaseFolder(); void setBaseFolder(String baseFolder); static final String WORKING_FOLDER_PREFIX; }