1 | |
package org.apache.maven.shared.release.exec; |
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.InputStream; |
26 | |
import java.io.OutputStream; |
27 | |
import java.util.List; |
28 | |
|
29 | |
import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer; |
30 | |
import org.apache.maven.shared.release.ReleaseResult; |
31 | |
import org.apache.maven.shared.release.env.ReleaseEnvironment; |
32 | |
import org.codehaus.plexus.util.IOUtil; |
33 | |
import org.codehaus.plexus.util.StringUtils; |
34 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
35 | |
import org.codehaus.plexus.util.cli.Commandline; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 92 | public class ForkedMavenExecutor |
44 | |
extends AbstractMavenExecutor |
45 | |
{ |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private CommandLineFactory commandLineFactory; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
@Override |
57 | |
public void executeGoals( File workingDirectory, List<String> goals, ReleaseEnvironment releaseEnvironment, |
58 | |
boolean interactive, String additionalArguments, String pomFileName, |
59 | |
ReleaseResult relResult ) |
60 | |
throws MavenExecutorException |
61 | |
{ |
62 | 10 | String mavenPath = null; |
63 | |
|
64 | 10 | if ( releaseEnvironment.getMavenHome() != null ) |
65 | |
{ |
66 | 0 | mavenPath = releaseEnvironment.getMavenHome().getAbsolutePath(); |
67 | |
} |
68 | |
else |
69 | |
{ |
70 | 10 | mavenPath = System.getProperty( "maven.home" ); |
71 | |
} |
72 | |
|
73 | 10 | File settingsFile = null; |
74 | 10 | if ( releaseEnvironment.getSettings() != null ) |
75 | |
{ |
76 | |
|
77 | |
try |
78 | |
{ |
79 | 0 | settingsFile = File.createTempFile( "release-settings", ".xml" ); |
80 | 0 | SettingsXpp3Writer writer = new SettingsXpp3Writer(); |
81 | 0 | FileWriter fileWriter = null; |
82 | |
try |
83 | |
{ |
84 | 0 | fileWriter = new FileWriter( settingsFile ); |
85 | 0 | writer.write( fileWriter, releaseEnvironment.getSettings() ); |
86 | |
} |
87 | |
finally |
88 | |
{ |
89 | 0 | IOUtil.close( fileWriter ); |
90 | 0 | } |
91 | |
} |
92 | 0 | catch ( IOException e ) |
93 | |
{ |
94 | 0 | throw new MavenExecutorException( "Could not create temporary file for release settings.xml", e ); |
95 | 0 | } |
96 | |
} |
97 | |
try |
98 | |
{ |
99 | |
|
100 | 10 | Commandline cl = |
101 | |
commandLineFactory.createCommandLine( mavenPath + File.separator + "bin" + File.separator + "mvn" ); |
102 | |
|
103 | 10 | cl.setWorkingDirectory( workingDirectory.getAbsolutePath() ); |
104 | |
|
105 | 10 | cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" ); |
106 | |
|
107 | 10 | cl.addEnvironment( "M2_HOME", mavenPath ); |
108 | |
|
109 | 10 | if ( settingsFile != null ) |
110 | |
{ |
111 | 0 | cl.createArg().setValue( "-s" ); |
112 | 0 | cl.createArg().setFile( settingsFile ); |
113 | |
} |
114 | |
|
115 | 10 | if ( pomFileName != null ) |
116 | |
{ |
117 | 2 | cl.createArg().setValue( "-f" ); |
118 | 2 | cl.createArg().setValue( pomFileName ); |
119 | |
} |
120 | |
|
121 | 10 | for ( String goal : goals ) |
122 | |
{ |
123 | 20 | cl.createArg().setValue( goal ); |
124 | |
} |
125 | |
|
126 | 10 | cl.createArg().setValue( "--no-plugin-updates" ); |
127 | |
|
128 | 10 | if ( !interactive ) |
129 | |
{ |
130 | 10 | cl.createArg().setValue( "--batch-mode" ); |
131 | |
} |
132 | |
|
133 | 10 | if ( !StringUtils.isEmpty( additionalArguments ) ) |
134 | |
{ |
135 | 2 | cl.createArg().setLine( additionalArguments ); |
136 | |
} |
137 | |
|
138 | 10 | TeeOutputStream stdOut = new TeeOutputStream( System.out ); |
139 | |
|
140 | 10 | TeeOutputStream stdErr = new TeeOutputStream( System.err ); |
141 | |
|
142 | |
try |
143 | |
{ |
144 | 10 | relResult.appendInfo( "Executing: " + cl.toString() ); |
145 | 10 | getLogger().info( "Executing: " + cl.toString() ); |
146 | |
|
147 | 10 | int result = executeCommandLine( cl, System.in, stdOut, stdErr ); |
148 | |
|
149 | 8 | if ( result != 0 ) |
150 | |
{ |
151 | 2 | throw new MavenExecutorException( "Maven execution failed, exit code: \'" + result + "\'", result, |
152 | |
stdOut.toString(), stdErr.toString() ); |
153 | |
} |
154 | |
} |
155 | 2 | catch ( CommandLineException e ) |
156 | |
{ |
157 | 2 | throw new MavenExecutorException( "Can't run goal " + goals, stdOut.toString(), stdErr.toString(), e ); |
158 | |
} |
159 | |
finally |
160 | |
{ |
161 | 10 | relResult.appendOutput( stdOut.toString() ); |
162 | 6 | } |
163 | |
} |
164 | |
finally |
165 | |
{ |
166 | 10 | if ( settingsFile != null && settingsFile.exists() && !settingsFile.delete() ) |
167 | |
{ |
168 | 0 | settingsFile.deleteOnExit(); |
169 | |
} |
170 | |
} |
171 | 6 | } |
172 | |
|
173 | |
public void setCommandLineFactory( CommandLineFactory commandLineFactory ) |
174 | |
{ |
175 | 10 | this.commandLineFactory = commandLineFactory; |
176 | 10 | } |
177 | |
|
178 | |
|
179 | |
public static int executeCommandLine( Commandline cl, InputStream systemIn, OutputStream systemOut, |
180 | |
OutputStream systemErr ) |
181 | |
throws CommandLineException |
182 | |
{ |
183 | 10 | if ( cl == null ) |
184 | |
{ |
185 | 0 | throw new IllegalArgumentException( "cl cannot be null." ); |
186 | |
} |
187 | |
|
188 | 10 | Process p = cl.execute(); |
189 | |
|
190 | |
|
191 | |
|
192 | 8 | RawStreamPumper inputFeeder = null; |
193 | |
|
194 | 8 | if ( systemIn != null ) |
195 | |
{ |
196 | 8 | inputFeeder = new RawStreamPumper( systemIn, p.getOutputStream(), true ); |
197 | |
} |
198 | |
|
199 | 8 | RawStreamPumper outputPumper = new RawStreamPumper( p.getInputStream(), systemOut ); |
200 | 8 | RawStreamPumper errorPumper = new RawStreamPumper( p.getErrorStream(), systemErr ); |
201 | |
|
202 | 8 | if ( inputFeeder != null ) |
203 | |
{ |
204 | 8 | inputFeeder.start(); |
205 | |
} |
206 | |
|
207 | 8 | outputPumper.start(); |
208 | |
|
209 | 8 | errorPumper.start(); |
210 | |
|
211 | |
try |
212 | |
{ |
213 | 8 | int returnValue = p.waitFor(); |
214 | |
|
215 | 8 | if ( inputFeeder != null ) |
216 | |
{ |
217 | 8 | inputFeeder.setDone(); |
218 | |
} |
219 | 8 | outputPumper.setDone(); |
220 | 8 | errorPumper.setDone(); |
221 | |
|
222 | |
|
223 | |
|
224 | 8 | return returnValue; |
225 | |
} |
226 | 0 | catch ( InterruptedException ex ) |
227 | |
{ |
228 | |
|
229 | 0 | throw new CommandLineException( "Error while executing external command, process killed.", ex ); |
230 | |
} |
231 | |
finally |
232 | |
{ |
233 | 0 | try |
234 | |
{ |
235 | 8 | errorPumper.closeInput(); |
236 | |
} |
237 | 0 | catch ( IOException e ) |
238 | |
{ |
239 | |
|
240 | 8 | } |
241 | |
try |
242 | |
{ |
243 | 8 | outputPumper.closeInput(); |
244 | |
} |
245 | 0 | catch ( IOException e ) |
246 | |
{ |
247 | |
|
248 | 8 | } |
249 | 8 | if ( inputFeeder != null ) |
250 | |
{ |
251 | |
try |
252 | |
{ |
253 | 8 | inputFeeder.closeOutput(); |
254 | |
} |
255 | 0 | catch ( IOException e ) |
256 | |
{ |
257 | |
|
258 | 16 | } |
259 | |
} |
260 | |
} |
261 | |
} |
262 | |
|
263 | |
|
264 | |
} |