1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.codehaus.mojo.cobertura.tasks; |
21 | |
|
22 | |
import org.apache.commons.lang.SystemUtils; |
23 | |
import org.apache.maven.artifact.Artifact; |
24 | |
import org.apache.maven.plugin.MojoExecutionException; |
25 | |
import org.apache.maven.plugin.MojoFailureException; |
26 | |
import org.apache.maven.plugin.logging.Log; |
27 | |
import org.apache.maven.plugin.logging.SystemStreamLog; |
28 | |
import org.codehaus.plexus.util.FileUtils; |
29 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
30 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
31 | |
import org.codehaus.plexus.util.cli.Commandline; |
32 | |
|
33 | |
import java.io.File; |
34 | |
import java.io.IOException; |
35 | |
import java.net.MalformedURLException; |
36 | |
import java.net.URL; |
37 | |
import java.util.Collections; |
38 | |
import java.util.Iterator; |
39 | |
import java.util.List; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public abstract class AbstractTask |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
protected CommandLineArguments cmdLineArgs; |
52 | |
|
53 | |
private Log log; |
54 | |
|
55 | |
private String maxmem; |
56 | |
|
57 | |
private List<Artifact> pluginClasspathList; |
58 | |
|
59 | |
private String taskClass; |
60 | |
|
61 | |
private boolean quiet; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
protected AbstractTask( String taskClassname ) |
69 | 0 | { |
70 | 0 | taskClass = taskClassname; |
71 | 0 | cmdLineArgs = new CommandLineArguments(); |
72 | 0 | maxmem = "64m"; |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public void setQuiet( boolean quiet ) |
81 | |
{ |
82 | 0 | this.quiet = quiet; |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public boolean isQuiet() |
91 | |
{ |
92 | 0 | return quiet; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public String createClasspath() |
103 | |
throws MojoExecutionException |
104 | |
{ |
105 | |
|
106 | 0 | StringBuffer cpBuffer = new StringBuffer(); |
107 | |
|
108 | 0 | for ( Iterator<Artifact> it = pluginClasspathList.iterator(); it.hasNext(); ) |
109 | |
{ |
110 | 0 | Artifact artifact = it.next(); |
111 | |
|
112 | |
try |
113 | |
{ |
114 | 0 | cpBuffer.append( File.pathSeparator ).append( artifact.getFile().getCanonicalPath() ); |
115 | |
} |
116 | 0 | catch ( IOException e ) |
117 | |
{ |
118 | 0 | throw new MojoExecutionException( |
119 | |
"Error while creating the canonical path for '" + artifact.getFile() + "'.", e ); |
120 | 0 | } |
121 | 0 | } |
122 | |
|
123 | 0 | return cpBuffer.toString(); |
124 | |
} |
125 | |
|
126 | |
private String getLog4jConfigFile() |
127 | |
{ |
128 | 0 | String resourceName = "cobertura-plugin/log4j-info.properties"; |
129 | 0 | if ( getLog().isDebugEnabled() ) |
130 | |
{ |
131 | 0 | resourceName = "cobertura-plugin/log4j-debug.properties"; |
132 | |
} |
133 | 0 | if ( quiet ) |
134 | |
{ |
135 | 0 | resourceName = "cobertura-plugin/log4j-error.properties"; |
136 | |
} |
137 | |
|
138 | 0 | String path = null; |
139 | |
try |
140 | |
{ |
141 | 0 | File log4jconfigFile = File.createTempFile( "log4j", "config.properties" ); |
142 | 0 | URL log4jurl = this.getClass().getClassLoader().getResource( resourceName ); |
143 | 0 | FileUtils.copyURLToFile( log4jurl, log4jconfigFile ); |
144 | 0 | log4jconfigFile.deleteOnExit(); |
145 | 0 | path = log4jconfigFile.toURL().toExternalForm(); |
146 | |
} |
147 | 0 | catch ( MalformedURLException e ) |
148 | |
{ |
149 | |
|
150 | |
} |
151 | 0 | catch ( IOException e ) |
152 | |
{ |
153 | |
|
154 | 0 | } |
155 | 0 | return path; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public abstract void execute() |
165 | |
throws MojoExecutionException, MojoFailureException; |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected int executeJava() |
174 | |
throws MojoExecutionException |
175 | |
{ |
176 | 0 | Commandline cl = new Commandline(); |
177 | 0 | File java = new File( SystemUtils.getJavaHome(), "bin/java" ); |
178 | 0 | cl.setExecutable( java.getAbsolutePath() ); |
179 | 0 | cl.addEnvironment( "CLASSPATH", createClasspath() ); |
180 | |
|
181 | 0 | String log4jConfig = getLog4jConfigFile(); |
182 | 0 | if ( log4jConfig != null ) |
183 | |
{ |
184 | 0 | cl.createArg().setValue( "-Dlog4j.configuration=" + log4jConfig ); |
185 | |
} |
186 | |
|
187 | 0 | cl.createArg().setValue( "-Xmx" + maxmem ); |
188 | |
|
189 | 0 | cl.createArg().setValue( taskClass ); |
190 | |
|
191 | 0 | if ( cmdLineArgs.useCommandsFile() ) |
192 | |
{ |
193 | |
String commandsFile; |
194 | |
try |
195 | |
{ |
196 | 0 | commandsFile = cmdLineArgs.getCommandsFile(); |
197 | |
} |
198 | 0 | catch ( IOException e ) |
199 | |
{ |
200 | 0 | throw new MojoExecutionException( "Unable to obtain CommandsFile location.", e ); |
201 | 0 | } |
202 | 0 | if ( FileUtils.fileExists( commandsFile ) ) |
203 | |
{ |
204 | 0 | cl.createArg().setValue( "--commandsfile" ); |
205 | 0 | cl.createArg().setValue( commandsFile ); |
206 | |
} |
207 | |
else |
208 | |
{ |
209 | 0 | throw new MojoExecutionException( "CommandsFile doesn't exist: " + commandsFile ); |
210 | |
} |
211 | 0 | } |
212 | |
else |
213 | |
{ |
214 | 0 | Iterator<String> it = cmdLineArgs.iterator(); |
215 | 0 | while ( it.hasNext() ) |
216 | |
{ |
217 | 0 | cl.createArg().setValue( it.next() ); |
218 | |
} |
219 | |
} |
220 | |
|
221 | 0 | CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); |
222 | |
|
223 | 0 | CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); |
224 | |
|
225 | 0 | if ( quiet ) |
226 | |
{ |
227 | 0 | CommandLineUtils.StringStreamConsumer nullConsumer = new CommandLineUtils.StringStreamConsumer() |
228 | 0 | { |
229 | |
public void consumeLine( String line ) |
230 | |
{ |
231 | |
|
232 | 0 | } |
233 | |
}; |
234 | 0 | stdout = nullConsumer; |
235 | 0 | stderr = nullConsumer; |
236 | |
} |
237 | |
|
238 | 0 | getLog().debug( "Working Directory: " + cl.getWorkingDirectory() ); |
239 | |
try |
240 | |
{ |
241 | 0 | String[] environmentVariables = cl.getEnvironmentVariables(); |
242 | 0 | for ( String environmentVariable : environmentVariables ) |
243 | |
{ |
244 | 0 | getLog().debug( "Environment variable: " + environmentVariable ); |
245 | |
} |
246 | |
} |
247 | 0 | catch( CommandLineException e ) { |
248 | |
|
249 | 0 | } |
250 | 0 | getLog().debug( "Executing command line:" ); |
251 | 0 | getLog().debug( cl.toString() ); |
252 | |
|
253 | |
int exitCode; |
254 | |
try |
255 | |
{ |
256 | 0 | exitCode = CommandLineUtils.executeCommandLine( cl, stdout, stderr ); |
257 | |
} |
258 | 0 | catch ( CommandLineException e ) |
259 | |
{ |
260 | 0 | throw new MojoExecutionException( "Unable to execute Cobertura.", e ); |
261 | 0 | } |
262 | |
|
263 | 0 | getLog().debug( "exit code: " + exitCode ); |
264 | |
|
265 | 0 | String output = stdout.getOutput(); |
266 | |
|
267 | 0 | if ( output.trim().length() > 0 ) |
268 | |
{ |
269 | 0 | getLog().debug( "--------------------" ); |
270 | 0 | getLog().debug( " Standard output from the Cobertura task:" ); |
271 | 0 | getLog().debug( "--------------------" ); |
272 | 0 | getLog().info( output ); |
273 | 0 | getLog().debug( "--------------------" ); |
274 | |
} |
275 | |
|
276 | 0 | String stream = stderr.getOutput(); |
277 | |
|
278 | 0 | if ( stream.trim().length() > 0 ) |
279 | |
{ |
280 | 0 | getLog().debug( "--------------------" ); |
281 | 0 | getLog().debug( " Standard error from the Cobertura task:" ); |
282 | 0 | getLog().debug( "--------------------" ); |
283 | 0 | getLog().error( stderr.getOutput() ); |
284 | 0 | getLog().debug( "--------------------" ); |
285 | |
} |
286 | |
|
287 | 0 | return exitCode; |
288 | |
} |
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public CommandLineArguments getCmdLineArgs() |
296 | |
{ |
297 | 0 | return cmdLineArgs; |
298 | |
} |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public Log getLog() |
304 | |
{ |
305 | 0 | if ( log == null ) |
306 | |
{ |
307 | 0 | log = new SystemStreamLog(); |
308 | |
} |
309 | |
|
310 | 0 | return log; |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public String getMaxmem() |
317 | |
{ |
318 | 0 | return maxmem; |
319 | |
} |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
public List<Artifact> getPluginClasspathList() |
325 | |
{ |
326 | 0 | return pluginClasspathList; |
327 | |
} |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public void setCmdLineArgs( CommandLineArguments cmdLineArgs ) |
335 | |
{ |
336 | 0 | this.cmdLineArgs = cmdLineArgs; |
337 | 0 | } |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public void setLog( Log log ) |
345 | |
{ |
346 | 0 | this.log = log; |
347 | 0 | } |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
public void setMaxmem( String maxmem ) |
355 | |
{ |
356 | 0 | this.maxmem = maxmem; |
357 | 0 | } |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
public void setPluginClasspathList( List<Artifact> pluginClasspathList ) |
363 | |
{ |
364 | 0 | this.pluginClasspathList = Collections.unmodifiableList( pluginClasspathList ); |
365 | 0 | } |
366 | |
} |