1 package org.apache.maven.shared.test.plugin;
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.util.List;
26 import java.util.Properties;
27
28 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
29 import org.apache.maven.shared.invoker.DefaultInvoker;
30 import org.apache.maven.shared.invoker.InvocationOutputHandler;
31 import org.apache.maven.shared.invoker.InvocationRequest;
32 import org.apache.maven.shared.invoker.InvocationResult;
33 import org.apache.maven.shared.invoker.Invoker;
34 import org.apache.maven.shared.invoker.MavenInvocationException;
35 import org.codehaus.plexus.component.annotations.Component;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
38 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
39 import org.codehaus.plexus.util.IOUtil;
40 import org.codehaus.plexus.util.cli.CommandLineUtils;
41
42
43
44
45
46
47
48 @Deprecated
49 @Component( role = BuildTool.class )
50 public class BuildTool
51 implements Initializable, Disposable
52 {
53
54 public static final String ROLE = BuildTool.class.getName();
55
56 private Invoker mavenInvoker;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public InvocationResult executeMaven( File pom, Properties properties, List<String> goals, File buildLogFile )
73 throws TestToolsException
74 {
75 InvocationRequest request = createBasicInvocationRequest( pom, properties, goals, buildLogFile );
76
77 return executeMaven( request );
78 }
79
80
81
82
83
84
85
86
87
88
89
90 public InvocationResult executeMaven( InvocationRequest request )
91 throws TestToolsException
92 {
93 try
94 {
95 return mavenInvoker.execute( request );
96 }
97 catch ( MavenInvocationException e )
98 {
99 throw new TestToolsException( "Error executing maven.", e );
100 }
101 finally
102 {
103 closeHandlers( request );
104 }
105 }
106
107
108
109
110
111
112
113
114 private void startInvoker()
115 throws IOException
116 {
117 if ( mavenInvoker == null )
118 {
119 mavenInvoker = new DefaultInvoker();
120
121 if ( System.getProperty( "maven.home" ) == null )
122 {
123 Properties envars = CommandLineUtils.getSystemEnvVars();
124
125 String mavenHome = envars.getProperty( "M2_HOME" );
126
127 if ( mavenHome != null )
128 {
129 mavenInvoker.setMavenHome( new File( mavenHome ) );
130 }
131 }
132 }
133 }
134
135
136
137
138
139
140
141 private void closeHandlers( InvocationRequest request )
142 {
143 InvocationOutputHandler outHandler = request.getOutputHandler( null );
144
145 if ( outHandler != null && ( outHandler instanceof LoggerHandler ) )
146 {
147 ( (LoggerHandler) outHandler ).close();
148 }
149
150 InvocationOutputHandler errHandler = request.getErrorHandler( null );
151
152 if ( errHandler != null && ( outHandler == null || errHandler != outHandler )
153 && ( errHandler instanceof LoggerHandler ) )
154 {
155 ( (LoggerHandler) errHandler ).close();
156 }
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 public InvocationRequest createBasicInvocationRequest( File pom, Properties properties, List<String> goals,
174 File buildLogFile )
175 {
176 InvocationRequest request = new DefaultInvocationRequest();
177
178 request.setPomFile( pom );
179
180 request.setGoals( goals );
181
182 request.setProperties( properties );
183
184 LoggerHandler handler = new LoggerHandler( buildLogFile );
185
186 request.setOutputHandler( handler );
187 request.setErrorHandler( handler );
188
189 return request;
190 }
191
192 private static final class LoggerHandler
193 implements InvocationOutputHandler
194 {
195 private static final String LS = System.getProperty( "line.separator" );
196
197 private final File output;
198
199 private FileWriter writer;
200
201 LoggerHandler( File logFile )
202 {
203 output = logFile;
204 }
205
206
207 public void consumeLine( String line )
208 {
209 if ( writer == null )
210 {
211 try
212 {
213 output.getParentFile().mkdirs();
214 writer = new FileWriter( output );
215 }
216 catch ( IOException e )
217 {
218 throw new IllegalStateException( "Failed to open build log: " + output + "\n\nError: "
219 + e.getMessage() );
220 }
221 }
222
223 try
224 {
225 writer.write( line + LS );
226 writer.flush();
227 }
228 catch ( IOException e )
229 {
230 throw new IllegalStateException( "Failed to write to build log: " + output + " output:\n\n\'" + line
231 + "\'\n\nError: " + e.getMessage() );
232 }
233 }
234
235 void close()
236 {
237 IOUtil.close( writer );
238 }
239 }
240
241
242
243
244
245
246
247 public void initialize()
248 throws InitializationException
249 {
250 try
251 {
252 startInvoker();
253 }
254 catch ( IOException e )
255 {
256 throw new InitializationException( "Error detecting maven home.", e );
257 }
258 }
259
260
261
262
263
264 public void dispose()
265 {
266
267
268 }
269 }