Here you can find the source of runCommandAndWait(String command, String workingDir, String extraPath)
Parameter | Description |
---|---|
command | - Command to be run in another process, e.g. "mvn clean install" |
workingDir | - Working directory in which to run the command. |
extraPath | a parameter |
Parameter | Description |
---|---|
Exception | an exception |
private static Integer runCommandAndWait(String command, String workingDir, String extraPath) throws Exception
//package com.java2s; /*/*from w w w. j a v a 2 s. co m*/ * Copyright (C) 2012 Red Hat, Inc. (jcasey@redhat.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { private static final String MVN_LOCATION = System.getProperty("maven.home"); /** * Run command in another process and wait for it to finish. * * @param command - Command to be run in another process, e.g. "mvn clean install" * @param workingDir - Working directory in which to run the command. * @param extraPath * @return exit value. * @throws Exception */ private static Integer runCommandAndWait(String command, String workingDir, String extraPath) throws Exception { String path = System.getenv("PATH"); if (extraPath != null) { path = extraPath + System.getProperty("path.separator") + path; } Process proc = Runtime.getRuntime().exec(command, new String[] { "M2_HOME=" + MVN_LOCATION, "PATH=" + path }, new File(workingDir)); File buildlog = new File(workingDir + "/build.log"); BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream())); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(buildlog, true))); String line = null; String errline = null; while ((line = stdout.readLine()) != null || (errline = stderr.readLine()) != null) { if (line != null) { out.println(line); } if (errline != null) { out.println(errline); } } stdout.close(); stderr.close(); out.close(); return proc.waitFor(); } }