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