Skip to content

Commit fa0f4ff

Browse files
committed
U command execution test
1 parent c1fa561 commit fa0f4ff

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/main/java/fvarrui/maven/plugin/javapackager/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fvarrui.maven.plugin.javapackager;
22

3+
import java.io.File;
4+
35
import org.apache.maven.plugin.MojoExecutionException;
46

57
import fvarrui.maven.plugin.javapackager.utils.CommandUtils;
@@ -8,7 +10,8 @@ public class Main {
810

911
public static void main(String[] args) throws MojoExecutionException {
1012
String result =
11-
CommandUtils.execute(
13+
CommandUtils.execute2(
14+
new File("."),
1215
"/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/bin/jdeps",
1316
"-q",
1417
"--ignore-missing-deps",
@@ -17,6 +20,7 @@ public static void main(String[] args) throws MojoExecutionException {
1720
"/Users/fran/teuton-panel/target/app/teuton-panel.app/Contents/Resources/Java/libs/*.jar",
1821
"/Users/fran/teuton-panel/target/teuton-panel-0.1.1-runnable.jar"
1922
);
23+
2024
System.out.println(result);
2125
}
2226

src/main/java/fvarrui/maven/plugin/javapackager/utils/CommandUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ public static String execute(File workingDirectory, String executable, Object ..
7272
return outputBuffer.toString();
7373
}
7474

75+
public static String execute2(File workingDirectory, String executable, Object ... arguments) throws MojoExecutionException {
76+
StringBuffer outputBuffer = new StringBuffer();
77+
try {
78+
79+
arguments = expandArray(arguments);
80+
81+
Commandline command = new Commandline();
82+
command.setWorkingDirectory(workingDirectory);
83+
command.setExecutable(executable);
84+
createArguments(command, arguments);
85+
86+
String line = StringUtils.join(command.getCommandline(), " ");
87+
88+
Process process = Runtime.getRuntime().exec(line);
89+
90+
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
91+
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
92+
93+
while (process.isAlive()) {
94+
if (output.ready()) outputBuffer.append(Logger.info(output.readLine()) + "\n");
95+
if (error.ready()) Logger.error(error.readLine());
96+
}
97+
98+
output.close();
99+
error.close();
100+
101+
} catch (IOException e) {
102+
throw new MojoExecutionException(e.getMessage(), e);
103+
}
104+
105+
return outputBuffer.toString();
106+
}
107+
75108
public static String execute(String executable, Object... arguments) throws MojoExecutionException {
76109
return execute(new File("."), executable, arguments);
77110
}

0 commit comments

Comments
 (0)