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