1 | |
package org.apache.maven.it; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileWriter; |
24 | |
import java.io.IOException; |
25 | |
import java.io.Writer; |
26 | |
import java.util.Collections; |
27 | |
import java.util.Map; |
28 | |
import org.apache.maven.shared.utils.cli.CommandLineException; |
29 | |
import org.apache.maven.shared.utils.cli.CommandLineUtils; |
30 | |
import org.apache.maven.shared.utils.cli.Commandline; |
31 | |
import org.apache.maven.shared.utils.cli.StreamConsumer; |
32 | |
import org.apache.maven.shared.utils.cli.WriterStreamConsumer; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
class ForkedLauncher |
38 | |
implements MavenLauncher |
39 | |
{ |
40 | |
|
41 | |
private final String mavenHome; |
42 | |
|
43 | |
private final String executable; |
44 | |
|
45 | |
public ForkedLauncher( String mavenHome ) |
46 | |
{ |
47 | 0 | this( mavenHome, false ); |
48 | 0 | } |
49 | |
|
50 | |
public ForkedLauncher( String mavenHome, boolean debugJvm ) |
51 | 0 | { |
52 | 0 | this.mavenHome = mavenHome; |
53 | |
|
54 | 0 | String script = debugJvm ? "mvnDebug" : "mvn"; |
55 | |
|
56 | 0 | if ( mavenHome != null ) |
57 | |
{ |
58 | 0 | executable = new File( mavenHome, "bin/" + script ).getPath(); |
59 | |
} |
60 | |
else |
61 | |
{ |
62 | 0 | executable = script; |
63 | |
} |
64 | 0 | } |
65 | |
|
66 | |
public int run( String[] cliArgs, Map envVars, String workingDirectory, File logFile ) |
67 | |
throws IOException, LauncherException |
68 | |
{ |
69 | 0 | Commandline cmd = new Commandline(); |
70 | |
|
71 | 0 | cmd.setExecutable( executable ); |
72 | |
|
73 | 0 | if ( mavenHome != null ) |
74 | |
{ |
75 | 0 | cmd.addEnvironment( "M2_HOME", mavenHome ); |
76 | |
} |
77 | |
|
78 | 0 | if ( envVars != null ) |
79 | |
{ |
80 | 0 | for ( Object o : envVars.keySet() ) |
81 | |
{ |
82 | 0 | String key = (String) o; |
83 | |
|
84 | 0 | cmd.addEnvironment( key, (String) envVars.get( key ) ); |
85 | 0 | } |
86 | |
} |
87 | |
|
88 | 0 | if ( envVars == null || envVars.get( "JAVA_HOME" ) == null ) |
89 | |
{ |
90 | 0 | cmd.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) ); |
91 | |
} |
92 | |
|
93 | 0 | cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" ); |
94 | |
|
95 | 0 | cmd.setWorkingDirectory( workingDirectory ); |
96 | |
|
97 | 0 | for ( String cliArg : cliArgs ) |
98 | |
{ |
99 | 0 | cmd.createArg().setValue( cliArg ); |
100 | |
} |
101 | |
|
102 | 0 | Writer logWriter = new FileWriter( logFile ); |
103 | |
|
104 | 0 | StreamConsumer out = new WriterStreamConsumer( logWriter ); |
105 | |
|
106 | 0 | StreamConsumer err = new WriterStreamConsumer( logWriter ); |
107 | |
|
108 | |
try |
109 | |
{ |
110 | 0 | return CommandLineUtils.executeCommandLine( cmd, out, err ); |
111 | |
} |
112 | 0 | catch ( CommandLineException e ) |
113 | |
{ |
114 | 0 | throw new LauncherException( "Failed to run Maven: " + e.getMessage() + "\n" + cmd, e ); |
115 | |
} |
116 | |
finally |
117 | |
{ |
118 | 0 | logWriter.close(); |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
public int run( String[] cliArgs, String workingDirectory, File logFile ) |
123 | |
throws IOException, LauncherException |
124 | |
{ |
125 | 0 | return run( cliArgs, Collections.EMPTY_MAP, workingDirectory, logFile ); |
126 | |
} |
127 | |
|
128 | |
} |