1 | |
package org.apache.maven.plugin.javadoc; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.ByteArrayOutputStream; |
23 | |
import java.io.File; |
24 | |
import java.io.FileInputStream; |
25 | |
import java.io.FileNotFoundException; |
26 | |
import java.io.FileOutputStream; |
27 | |
import java.io.IOException; |
28 | |
import java.io.InputStream; |
29 | |
import java.io.OutputStream; |
30 | |
import java.io.OutputStreamWriter; |
31 | |
import java.io.PrintStream; |
32 | |
import java.io.UnsupportedEncodingException; |
33 | |
import java.lang.reflect.Modifier; |
34 | |
import java.net.SocketTimeoutException; |
35 | |
import java.net.URL; |
36 | |
import java.net.URLClassLoader; |
37 | |
import java.util.ArrayList; |
38 | |
import java.util.Arrays; |
39 | |
import java.util.Collection; |
40 | |
import java.util.Iterator; |
41 | |
import java.util.List; |
42 | |
import java.util.Locale; |
43 | |
import java.util.NoSuchElementException; |
44 | |
import java.util.Properties; |
45 | |
import java.util.Set; |
46 | |
import java.util.StringTokenizer; |
47 | |
import java.util.jar.JarEntry; |
48 | |
import java.util.jar.JarInputStream; |
49 | |
import java.util.regex.Matcher; |
50 | |
import java.util.regex.Pattern; |
51 | |
import java.util.regex.PatternSyntaxException; |
52 | |
|
53 | |
import org.apache.commons.httpclient.Credentials; |
54 | |
import org.apache.commons.httpclient.HttpClient; |
55 | |
import org.apache.commons.httpclient.HttpStatus; |
56 | |
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
57 | |
import org.apache.commons.httpclient.UsernamePasswordCredentials; |
58 | |
import org.apache.commons.httpclient.auth.AuthScope; |
59 | |
import org.apache.commons.httpclient.methods.GetMethod; |
60 | |
import org.apache.commons.httpclient.params.HttpClientParams; |
61 | |
import org.apache.commons.httpclient.params.HttpMethodParams; |
62 | |
import org.apache.commons.lang.SystemUtils; |
63 | |
import org.apache.maven.artifact.Artifact; |
64 | |
import org.apache.maven.plugin.logging.Log; |
65 | |
import org.apache.maven.project.MavenProject; |
66 | |
import org.apache.maven.settings.Proxy; |
67 | |
import org.apache.maven.settings.Settings; |
68 | |
import org.apache.maven.shared.invoker.DefaultInvocationRequest; |
69 | |
import org.apache.maven.shared.invoker.DefaultInvoker; |
70 | |
import org.apache.maven.shared.invoker.InvocationOutputHandler; |
71 | |
import org.apache.maven.shared.invoker.InvocationRequest; |
72 | |
import org.apache.maven.shared.invoker.InvocationResult; |
73 | |
import org.apache.maven.shared.invoker.Invoker; |
74 | |
import org.apache.maven.shared.invoker.MavenInvocationException; |
75 | |
import org.apache.maven.shared.invoker.PrintStreamHandler; |
76 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
77 | |
import org.apache.maven.wagon.proxy.ProxyUtils; |
78 | |
import org.codehaus.plexus.util.FileUtils; |
79 | |
import org.codehaus.plexus.util.IOUtil; |
80 | |
import org.codehaus.plexus.util.Os; |
81 | |
import org.codehaus.plexus.util.StringUtils; |
82 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
83 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
84 | |
import org.codehaus.plexus.util.cli.Commandline; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 0 | public class JavadocUtil |
94 | |
{ |
95 | |
|
96 | |
public static final int DEFAULT_TIMEOUT = 2000; |
97 | |
|
98 | |
|
99 | |
protected static final String ERROR_INIT_VM = |
100 | |
"Error occurred during initialization of VM, try to reduce the Java heap size for the MAVEN_OPTS " + |
101 | |
"environnement variable using -Xms:<size> and -Xmx:<size>."; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public static List<String> pruneDirs( MavenProject project, List<String> dirs ) |
113 | |
{ |
114 | 97 | List<String> pruned = new ArrayList<String>( dirs.size() ); |
115 | 97 | for ( String dir : dirs ) |
116 | |
{ |
117 | 106 | if ( dir == null ) |
118 | |
{ |
119 | 0 | continue; |
120 | |
} |
121 | |
|
122 | 106 | File directory = new File( dir ); |
123 | 106 | if ( !directory.isAbsolute() ) |
124 | |
{ |
125 | 0 | directory = new File( project.getBasedir(), directory.getPath() ); |
126 | |
} |
127 | |
|
128 | 106 | if ( directory.isDirectory() && !pruned.contains( directory.getAbsolutePath() ) ) |
129 | |
{ |
130 | 96 | pruned.add( directory.getAbsolutePath() ); |
131 | |
} |
132 | 106 | } |
133 | |
|
134 | 97 | return pruned; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
protected static List<String> pruneFiles( List<String> files ) |
145 | |
{ |
146 | 40 | List<String> pruned = new ArrayList<String>( files.size() ); |
147 | 40 | for ( String f : files ) |
148 | |
{ |
149 | 2 | if ( !shouldPruneFile( f, pruned ) ) |
150 | |
{ |
151 | 2 | pruned.add( f ); |
152 | |
} |
153 | |
} |
154 | |
|
155 | 40 | return pruned; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public static boolean shouldPruneFile( String f, List<String> pruned ) |
163 | |
{ |
164 | 2 | if ( f != null ) |
165 | |
{ |
166 | 2 | File file = new File( f ); |
167 | 2 | if ( file.isFile() && ( isEmpty( pruned ) || !pruned.contains( f ) ) ) |
168 | |
{ |
169 | 2 | return false; |
170 | |
} |
171 | |
} |
172 | |
|
173 | 0 | return true; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
protected static List getExcludedNames( List sourcePaths, String[] subpackagesList, String[] excludedPackages ) |
186 | |
{ |
187 | 1 | List excludedNames = new ArrayList(); |
188 | 1 | for ( Iterator i = sourcePaths.iterator(); i.hasNext(); ) |
189 | |
{ |
190 | 1 | String path = (String) i.next(); |
191 | 2 | for ( int j = 0; j < subpackagesList.length; j++ ) |
192 | |
{ |
193 | 1 | List excludes = getExcludedPackages( path, excludedPackages ); |
194 | 1 | excludedNames.addAll( excludes ); |
195 | |
} |
196 | 1 | } |
197 | |
|
198 | 1 | return excludedNames; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
protected static List getCompileArtifacts( Set artifacts ) |
208 | |
{ |
209 | 0 | return getCompileArtifacts( artifacts, false ); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
protected static List getCompileArtifacts( Set artifacts, boolean withTestScope ) |
219 | |
{ |
220 | 0 | List list = new ArrayList( artifacts.size() ); |
221 | |
|
222 | 0 | for ( Iterator i = artifacts.iterator(); i.hasNext(); ) |
223 | |
{ |
224 | 0 | Artifact a = (Artifact) i.next(); |
225 | |
|
226 | |
|
227 | 0 | if ( a.getArtifactHandler().isAddedToClasspath() ) |
228 | |
{ |
229 | |
|
230 | 0 | if ( withTestScope ) |
231 | |
{ |
232 | 0 | if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) |
233 | |
|| Artifact.SCOPE_PROVIDED.equals( a.getScope() ) |
234 | |
|| Artifact.SCOPE_SYSTEM.equals( a.getScope() ) |
235 | |
|| Artifact.SCOPE_TEST.equals( a.getScope() ) ) |
236 | |
{ |
237 | 0 | list.add( a ); |
238 | |
} |
239 | |
} |
240 | |
else |
241 | |
{ |
242 | 0 | if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) |
243 | |
|| Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) |
244 | |
{ |
245 | 0 | list.add( a ); |
246 | |
} |
247 | |
} |
248 | |
} |
249 | 0 | } |
250 | |
|
251 | 0 | return list; |
252 | |
} |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
protected static String quotedArgument( String value ) |
264 | |
{ |
265 | 506 | String arg = value; |
266 | |
|
267 | 506 | if ( StringUtils.isNotEmpty( arg ) ) |
268 | |
{ |
269 | 177 | if ( arg.indexOf( "'" ) != -1 ) |
270 | |
{ |
271 | 0 | arg = StringUtils.replace( arg, "'", "\\'" ); |
272 | |
} |
273 | 177 | arg = "'" + arg + "'"; |
274 | |
|
275 | |
|
276 | 177 | arg = StringUtils.replace( arg, "\n", " " ); |
277 | |
} |
278 | |
|
279 | 506 | return arg; |
280 | |
} |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
protected static String quotedPathArgument( String value ) |
290 | |
{ |
291 | 384 | String path = value; |
292 | |
|
293 | 384 | if ( StringUtils.isNotEmpty( path ) ) |
294 | |
{ |
295 | 176 | path = path.replace( '\\', '/' ); |
296 | 176 | if ( path.indexOf( "\'" ) != -1 ) |
297 | |
{ |
298 | 4 | String split[] = path.split( "\'" ); |
299 | 4 | path = ""; |
300 | |
|
301 | 12 | for ( int i = 0; i < split.length; i++ ) |
302 | |
{ |
303 | 8 | if ( i != split.length - 1 ) |
304 | |
{ |
305 | 4 | path = path + split[i] + "\\'"; |
306 | |
} |
307 | |
else |
308 | |
{ |
309 | 4 | path = path + split[i]; |
310 | |
} |
311 | |
} |
312 | |
} |
313 | 176 | path = "'" + path + "'"; |
314 | |
} |
315 | |
|
316 | 384 | return path; |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
protected static void copyJavadocResources( File outputDirectory, File javadocDir ) |
329 | |
throws IOException |
330 | |
{ |
331 | 0 | copyJavadocResources( outputDirectory, javadocDir, null ); |
332 | 0 | } |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
protected static void copyJavadocResources( File outputDirectory, File javadocDir, String excludedocfilessubdir ) |
345 | |
throws IOException |
346 | |
{ |
347 | 8 | if ( !javadocDir.isDirectory() ) |
348 | |
{ |
349 | 1 | return; |
350 | |
} |
351 | |
|
352 | 7 | List excludes = new ArrayList(); |
353 | 7 | excludes.addAll( Arrays.asList( FileUtils.getDefaultExcludes() ) ); |
354 | |
|
355 | 7 | if ( StringUtils.isNotEmpty( excludedocfilessubdir ) ) |
356 | |
{ |
357 | 2 | StringTokenizer st = new StringTokenizer( excludedocfilessubdir, ":" ); |
358 | |
String current; |
359 | 5 | while ( st.hasMoreTokens() ) |
360 | |
{ |
361 | 3 | current = st.nextToken(); |
362 | 3 | excludes.add( "**/" + current + "/**" ); |
363 | |
} |
364 | |
} |
365 | |
|
366 | 7 | List docFiles = |
367 | |
FileUtils.getDirectoryNames( javadocDir, "resources,**/doc-files", |
368 | |
StringUtils.join( excludes.iterator(), "," ), false, true ); |
369 | 7 | for ( Iterator it = docFiles.iterator(); it.hasNext(); ) |
370 | |
{ |
371 | 10 | String docFile = (String) it.next(); |
372 | |
|
373 | 10 | File docFileOutput = new File( outputDirectory, docFile ); |
374 | 10 | FileUtils.mkdir( docFileOutput.getAbsolutePath() ); |
375 | 10 | FileUtils.copyDirectoryStructure( new File( javadocDir, docFile ), docFileOutput ); |
376 | 10 | List files = |
377 | |
FileUtils.getFileAndDirectoryNames( docFileOutput, StringUtils.join( excludes.iterator(), "," ), |
378 | |
null, true, true, true, true ); |
379 | 10 | for ( Iterator it2 = files.iterator(); it2.hasNext(); ) |
380 | |
{ |
381 | 333 | File file = new File( it2.next().toString() ); |
382 | |
|
383 | 333 | if ( file.isDirectory() ) |
384 | |
{ |
385 | 25 | FileUtils.deleteDirectory( file ); |
386 | |
} |
387 | |
else |
388 | |
{ |
389 | 308 | file.delete(); |
390 | |
} |
391 | 333 | } |
392 | 10 | } |
393 | 7 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
protected static List getIncludedFiles( File sourceDirectory, String[] fileList, String[] excludePackages ) |
405 | |
{ |
406 | 42 | List files = new ArrayList(); |
407 | |
|
408 | 113 | for ( int j = 0; j < fileList.length; j++ ) |
409 | |
{ |
410 | 71 | boolean include = true; |
411 | 80 | for ( int k = 0; k < excludePackages.length && include; k++ ) |
412 | |
{ |
413 | |
|
414 | 9 | String[] excludeName = excludePackages[k].split( "[*]" ); |
415 | |
|
416 | 9 | if ( excludeName.length == 0 ) |
417 | |
{ |
418 | 0 | continue; |
419 | |
} |
420 | |
|
421 | 9 | if ( excludeName.length > 1 ) |
422 | |
{ |
423 | 5 | int u = 0; |
424 | 15 | while ( include && u < excludeName.length ) |
425 | |
{ |
426 | 10 | if ( !"".equals( excludeName[u].trim() ) && fileList[j].indexOf( excludeName[u] ) != -1 ) |
427 | |
{ |
428 | 1 | include = false; |
429 | |
} |
430 | 10 | u++; |
431 | |
} |
432 | 5 | } |
433 | |
else |
434 | |
{ |
435 | 4 | if ( fileList[j].startsWith( sourceDirectory.toString() + File.separatorChar + excludeName[0] ) ) |
436 | |
{ |
437 | 2 | if ( excludeName[0].endsWith( String.valueOf( File.separatorChar ) ) ) |
438 | |
{ |
439 | 2 | int i = fileList[j].lastIndexOf( File.separatorChar ); |
440 | 2 | String packageName = fileList[j].substring( 0, i + 1 ); |
441 | 2 | File currentPackage = new File( packageName ); |
442 | 2 | File excludedPackage = new File( sourceDirectory, excludeName[0] ); |
443 | 2 | if ( currentPackage.equals( excludedPackage ) |
444 | |
&& fileList[j].substring( i ).indexOf( ".java" ) != -1 ) |
445 | |
{ |
446 | 1 | include = true; |
447 | |
} |
448 | |
else |
449 | |
{ |
450 | 1 | include = false; |
451 | |
} |
452 | 2 | } |
453 | |
else |
454 | |
{ |
455 | 0 | include = false; |
456 | |
} |
457 | |
} |
458 | |
} |
459 | |
} |
460 | |
|
461 | 71 | if ( include ) |
462 | |
{ |
463 | 69 | files.add( quotedPathArgument( fileList[j] ) ); |
464 | |
} |
465 | |
} |
466 | |
|
467 | 42 | return files; |
468 | |
} |
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
protected static List getExcludedPackages( String sourceDirectory, String[] excludePackagenames ) |
479 | |
{ |
480 | 1 | List files = new ArrayList(); |
481 | 3 | for ( int i = 0; i < excludePackagenames.length; i++ ) |
482 | |
{ |
483 | 2 | String[] fileList = FileUtils.getFilesFromExtension( sourceDirectory, new String[] { "java" } ); |
484 | 14 | for ( int j = 0; j < fileList.length; j++ ) |
485 | |
{ |
486 | 12 | String[] excludeName = excludePackagenames[i].split( "[*]" ); |
487 | 12 | int u = 0; |
488 | 24 | while ( u < excludeName.length ) |
489 | |
{ |
490 | 12 | if ( !"".equals( excludeName[u].trim() ) && fileList[j].indexOf( excludeName[u] ) != -1 |
491 | |
&& sourceDirectory.indexOf( excludeName[u] ) == -1 ) |
492 | |
{ |
493 | 2 | files.add( fileList[j] ); |
494 | |
} |
495 | 12 | u++; |
496 | |
} |
497 | |
} |
498 | |
} |
499 | |
|
500 | 1 | List excluded = new ArrayList(); |
501 | 1 | for ( Iterator it = files.iterator(); it.hasNext(); ) |
502 | |
{ |
503 | 2 | String file = (String) it.next(); |
504 | 2 | int idx = file.lastIndexOf( File.separatorChar ); |
505 | 2 | String tmpStr = file.substring( 0, idx ); |
506 | 2 | tmpStr = tmpStr.replace( '\\', '/' ); |
507 | 2 | String[] srcSplit = tmpStr.split( sourceDirectory.replace( '\\', '/' ) + '/' ); |
508 | 2 | String excludedPackage = srcSplit[1].replace( '/', '.' ); |
509 | |
|
510 | 2 | if ( !excluded.contains( excludedPackage ) ) |
511 | |
{ |
512 | 2 | excluded.add( excludedPackage ); |
513 | |
} |
514 | 2 | } |
515 | |
|
516 | 1 | return excluded; |
517 | |
} |
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
protected static void addFilesFromSource( List files, File sourceDirectory, String[] excludePackages ) |
527 | |
{ |
528 | 46 | String[] fileList = FileUtils.getFilesFromExtension( sourceDirectory.getPath(), new String[] { "java" } ); |
529 | 46 | if ( fileList != null && fileList.length != 0 ) |
530 | |
{ |
531 | 42 | List tmpFiles = getIncludedFiles( sourceDirectory, fileList, excludePackages ); |
532 | 42 | files.addAll( tmpFiles ); |
533 | |
} |
534 | 46 | } |
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
protected static float getJavadocVersion( File javadocExe ) |
551 | |
throws IOException, CommandLineException, IllegalArgumentException, PatternSyntaxException |
552 | |
{ |
553 | 41 | if ( ( javadocExe == null ) || ( !javadocExe.exists() ) || ( !javadocExe.isFile() ) ) |
554 | |
{ |
555 | 0 | throw new IOException( "The javadoc executable '" + javadocExe + "' doesn't exist or is not a file. " ); |
556 | |
} |
557 | |
|
558 | 41 | Commandline cmd = new Commandline(); |
559 | 41 | cmd.setExecutable( javadocExe.getAbsolutePath() ); |
560 | 41 | cmd.setWorkingDirectory( javadocExe.getParentFile() ); |
561 | 41 | cmd.createArg().setValue( "-J-version" ); |
562 | |
|
563 | 41 | CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer(); |
564 | 41 | CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
565 | |
|
566 | 41 | int exitCode = CommandLineUtils.executeCommandLine( cmd, out, err ); |
567 | |
|
568 | 41 | if ( exitCode != 0 ) |
569 | |
{ |
570 | 0 | StringBuffer msg = new StringBuffer( "Exit code: " + exitCode + " - " + err.getOutput() ); |
571 | 0 | msg.append( '\n' ); |
572 | 0 | msg.append( "Command line was:" + CommandLineUtils.toString( cmd.getCommandline() ) ); |
573 | 0 | throw new CommandLineException( msg.toString() ); |
574 | |
} |
575 | |
|
576 | 41 | if ( StringUtils.isNotEmpty( err.getOutput() ) ) |
577 | |
{ |
578 | 41 | return parseJavadocVersion( err.getOutput() ); |
579 | |
} |
580 | 0 | else if ( StringUtils.isNotEmpty( out.getOutput() ) ) |
581 | |
{ |
582 | 0 | return parseJavadocVersion( out.getOutput() ); |
583 | |
} |
584 | |
|
585 | 0 | throw new IllegalArgumentException( "No output found from the command line 'javadoc -J-version'" ); |
586 | |
} |
587 | |
|
588 | |
|
589 | |
|
590 | |
|
591 | |
|
592 | |
|
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
|
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
|
610 | |
|
611 | |
|
612 | |
|
613 | |
|
614 | |
|
615 | |
|
616 | |
|
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
|
627 | |
|
628 | |
|
629 | |
protected static float parseJavadocVersion( String output ) |
630 | |
throws IllegalArgumentException, PatternSyntaxException |
631 | |
{ |
632 | 58 | if ( StringUtils.isEmpty( output ) ) |
633 | |
{ |
634 | 1 | throw new IllegalArgumentException( "The output could not be null." ); |
635 | |
} |
636 | |
|
637 | 57 | Pattern pattern = Pattern.compile( "(?s).*?([0-9]+\\.[0-9]+)(\\.([0-9]+))?.*" ); |
638 | |
|
639 | 57 | Matcher matcher = pattern.matcher( output ); |
640 | 57 | if ( !matcher.matches() ) |
641 | |
{ |
642 | 1 | throw new PatternSyntaxException( "Unrecognized version of Javadoc: '" + output + "'", pattern.pattern(), |
643 | |
pattern.toString().length() - 1 ); |
644 | |
} |
645 | |
|
646 | 56 | String version = matcher.group( 3 ); |
647 | 56 | if ( version == null ) |
648 | |
{ |
649 | 1 | version = matcher.group( 1 ); |
650 | |
} |
651 | |
else |
652 | |
{ |
653 | 55 | version = matcher.group( 1 ) + version; |
654 | |
} |
655 | |
|
656 | 56 | return Float.parseFloat( version ); |
657 | |
} |
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
|
672 | |
|
673 | |
|
674 | |
|
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
|
681 | |
|
682 | |
|
683 | |
|
684 | |
|
685 | |
|
686 | |
|
687 | |
|
688 | |
protected static String parseJavadocMemory( String memory ) |
689 | |
throws IllegalArgumentException |
690 | |
{ |
691 | 18 | if ( StringUtils.isEmpty( memory ) ) |
692 | |
{ |
693 | 1 | throw new IllegalArgumentException( "The memory could not be null." ); |
694 | |
} |
695 | |
|
696 | 17 | Pattern p = Pattern.compile( "^\\s*(\\d+)\\s*?\\s*$" ); |
697 | 17 | Matcher m = p.matcher( memory ); |
698 | 17 | if ( m.matches() ) |
699 | |
{ |
700 | 2 | return m.group( 1 ) + "m"; |
701 | |
} |
702 | |
|
703 | 15 | p = Pattern.compile( "^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
704 | 15 | m = p.matcher( memory ); |
705 | 15 | if ( m.matches() ) |
706 | |
{ |
707 | 2 | return m.group( 1 ) + "k"; |
708 | |
} |
709 | |
|
710 | 13 | p = Pattern.compile( "^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
711 | 13 | m = p.matcher( memory ); |
712 | 13 | if ( m.matches() ) |
713 | |
{ |
714 | 7 | return m.group( 1 ) + "m"; |
715 | |
} |
716 | |
|
717 | 6 | p = Pattern.compile( "^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
718 | 6 | m = p.matcher( memory ); |
719 | 6 | if ( m.matches() ) |
720 | |
{ |
721 | 2 | return ( Integer.parseInt( m.group( 1 ) ) * 1024 ) + "m"; |
722 | |
} |
723 | |
|
724 | 4 | p = Pattern.compile( "^\\s*(\\d+)\\s*t(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
725 | 4 | m = p.matcher( memory ); |
726 | 4 | if ( m.matches() ) |
727 | |
{ |
728 | 2 | return ( Integer.parseInt( m.group( 1 ) ) * 1024 * 1024 ) + "m"; |
729 | |
} |
730 | |
|
731 | 2 | throw new IllegalArgumentException( "Could convert not to a memory size: " + memory ); |
732 | |
} |
733 | |
|
734 | |
|
735 | |
|
736 | |
|
737 | |
|
738 | |
|
739 | |
|
740 | |
|
741 | |
|
742 | |
protected static void fetchURL( Settings settings, URL url ) |
743 | |
throws IOException |
744 | |
{ |
745 | 36 | if ( url == null ) |
746 | |
{ |
747 | 1 | throw new IllegalArgumentException( "The url is null" ); |
748 | |
} |
749 | |
|
750 | 35 | if ( "file".equals( url.getProtocol() ) ) |
751 | |
{ |
752 | 3 | InputStream in = null; |
753 | |
try |
754 | |
{ |
755 | 3 | in = url.openStream(); |
756 | |
} |
757 | |
finally |
758 | |
{ |
759 | 3 | IOUtil.close( in ); |
760 | 3 | } |
761 | |
|
762 | 3 | return; |
763 | |
} |
764 | |
|
765 | |
|
766 | 32 | HttpClient httpClient = new HttpClient( new MultiThreadedHttpConnectionManager() ); |
767 | 32 | httpClient.getHttpConnectionManager().getParams().setConnectionTimeout( DEFAULT_TIMEOUT ); |
768 | 32 | httpClient.getHttpConnectionManager().getParams().setSoTimeout( DEFAULT_TIMEOUT ); |
769 | 32 | httpClient.getParams().setBooleanParameter( HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true ); |
770 | |
|
771 | |
|
772 | 32 | httpClient.getParams().setParameter( HttpMethodParams.USER_AGENT, |
773 | |
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" ); |
774 | |
|
775 | 32 | if ( settings != null && settings.getActiveProxy() != null ) |
776 | |
{ |
777 | 8 | Proxy activeProxy = settings.getActiveProxy(); |
778 | |
|
779 | 8 | ProxyInfo proxyInfo = new ProxyInfo(); |
780 | 8 | proxyInfo.setNonProxyHosts( activeProxy.getNonProxyHosts() ); |
781 | |
|
782 | 8 | if ( StringUtils.isNotEmpty( activeProxy.getHost() ) |
783 | |
&& !ProxyUtils.validateNonProxyHosts( proxyInfo, url.getHost() ) ) |
784 | |
{ |
785 | 7 | httpClient.getHostConfiguration().setProxy( activeProxy.getHost(), activeProxy.getPort() ); |
786 | |
|
787 | 7 | if ( StringUtils.isNotEmpty( activeProxy.getUsername() ) && activeProxy.getPassword() != null ) |
788 | |
{ |
789 | 5 | Credentials credentials = |
790 | |
new UsernamePasswordCredentials( activeProxy.getUsername(), activeProxy.getPassword() ); |
791 | |
|
792 | 5 | httpClient.getState().setProxyCredentials( AuthScope.ANY, credentials ); |
793 | |
} |
794 | |
} |
795 | |
} |
796 | |
|
797 | 32 | GetMethod getMethod = new GetMethod( url.toString() ); |
798 | |
try |
799 | |
{ |
800 | |
int status; |
801 | |
try |
802 | |
{ |
803 | 32 | status = httpClient.executeMethod( getMethod ); |
804 | |
} |
805 | 1 | catch ( SocketTimeoutException e ) |
806 | |
{ |
807 | |
|
808 | 1 | status = httpClient.executeMethod( getMethod ); |
809 | 30 | } |
810 | |
|
811 | 30 | if ( status != HttpStatus.SC_OK ) |
812 | |
{ |
813 | 4 | throw new FileNotFoundException( url.toString() ); |
814 | |
} |
815 | |
} |
816 | |
finally |
817 | |
{ |
818 | 32 | getMethod.releaseConnection(); |
819 | 26 | } |
820 | 26 | } |
821 | |
|
822 | |
|
823 | |
|
824 | |
|
825 | |
|
826 | |
|
827 | |
|
828 | |
protected static boolean validateEncoding( String charsetName ) |
829 | |
{ |
830 | 119 | if ( StringUtils.isEmpty( charsetName ) ) |
831 | |
{ |
832 | 1 | return false; |
833 | |
} |
834 | |
|
835 | 118 | OutputStream ost = new ByteArrayOutputStream(); |
836 | 118 | OutputStreamWriter osw = null; |
837 | |
try |
838 | |
{ |
839 | 118 | osw = new OutputStreamWriter( ost, charsetName ); |
840 | |
} |
841 | 5 | catch ( UnsupportedEncodingException exc ) |
842 | |
{ |
843 | 5 | return false; |
844 | |
} |
845 | |
finally |
846 | |
{ |
847 | 118 | IOUtil.close( osw ); |
848 | 113 | } |
849 | |
|
850 | 113 | return true; |
851 | |
} |
852 | |
|
853 | |
|
854 | |
|
855 | |
|
856 | |
|
857 | |
|
858 | |
|
859 | |
|
860 | |
|
861 | |
protected static String hideProxyPassword( String cmdLine, Settings settings ) |
862 | |
{ |
863 | 38 | if ( cmdLine == null ) |
864 | |
{ |
865 | 0 | throw new IllegalArgumentException( "cmdLine could not be null" ); |
866 | |
} |
867 | |
|
868 | 38 | if ( settings == null ) |
869 | |
{ |
870 | 34 | return cmdLine; |
871 | |
} |
872 | |
|
873 | 4 | Proxy activeProxy = settings.getActiveProxy(); |
874 | 4 | if ( activeProxy != null && StringUtils.isNotEmpty( activeProxy.getHost() ) |
875 | |
&& StringUtils.isNotEmpty( activeProxy.getUsername() ) |
876 | |
&& StringUtils.isNotEmpty( activeProxy.getPassword() ) ) |
877 | |
{ |
878 | 3 | String pass = "-J-Dhttp.proxyPassword=\"" + activeProxy.getPassword() + "\""; |
879 | 3 | String hidepass = |
880 | |
"-J-Dhttp.proxyPassword=\"" + StringUtils.repeat( "*", activeProxy.getPassword().length() ) + "\""; |
881 | |
|
882 | 3 | return StringUtils.replace( cmdLine, pass, hidepass ); |
883 | |
} |
884 | |
|
885 | 1 | return cmdLine; |
886 | |
} |
887 | |
|
888 | |
|
889 | |
|
890 | |
|
891 | |
|
892 | |
|
893 | |
|
894 | |
|
895 | |
|
896 | |
|
897 | |
|
898 | |
|
899 | |
|
900 | |
|
901 | |
|
902 | |
protected static List getTagletClassNames( File jarFile ) |
903 | |
throws IOException, ClassNotFoundException, NoClassDefFoundError |
904 | |
{ |
905 | 2 | List classes = getClassNamesFromJar( jarFile ); |
906 | |
ClassLoader cl; |
907 | |
|
908 | |
|
909 | 2 | File tools = new File( System.getProperty( "java.home" ), "../lib/tools.jar" ); |
910 | 2 | if ( tools.exists() && tools.isFile() ) |
911 | |
{ |
912 | 0 | cl = new URLClassLoader( new URL[] { jarFile.toURI().toURL(), tools.toURI().toURL() }, null ); |
913 | |
} |
914 | |
else |
915 | |
{ |
916 | 2 | cl = new URLClassLoader( new URL[] { jarFile.toURI().toURL() }, null ); |
917 | |
} |
918 | |
|
919 | 2 | List tagletClasses = new ArrayList(); |
920 | |
|
921 | 2 | Class tagletClass = cl.loadClass( "com.sun.tools.doclets.Taglet" ); |
922 | 2 | for ( Iterator it = classes.iterator(); it.hasNext(); ) |
923 | |
{ |
924 | 24 | String s = (String) it.next(); |
925 | |
|
926 | 24 | Class c = cl.loadClass( s ); |
927 | |
|
928 | 24 | if ( tagletClass.isAssignableFrom( c ) && !Modifier.isAbstract( c.getModifiers() ) ) |
929 | |
{ |
930 | 20 | tagletClasses.add( c.getName() ); |
931 | |
} |
932 | 24 | } |
933 | |
|
934 | 2 | return tagletClasses; |
935 | |
} |
936 | |
|
937 | |
|
938 | |
|
939 | |
|
940 | |
|
941 | |
|
942 | |
|
943 | |
|
944 | |
|
945 | |
protected static void copyResource( URL url, File file ) |
946 | |
throws IOException |
947 | |
{ |
948 | 4 | if ( file == null ) |
949 | |
{ |
950 | 0 | throw new IOException( "The file " + file + " can't be null." ); |
951 | |
} |
952 | 4 | if ( url == null ) |
953 | |
{ |
954 | 0 | throw new IOException( "The url " + url + " could not be null." ); |
955 | |
} |
956 | |
|
957 | 4 | InputStream is = url.openStream(); |
958 | 4 | if ( is == null ) |
959 | |
{ |
960 | 0 | throw new IOException( "The resource " + url + " doesn't exists." ); |
961 | |
} |
962 | |
|
963 | 4 | if ( !file.getParentFile().exists() ) |
964 | |
{ |
965 | 0 | file.getParentFile().mkdirs(); |
966 | |
} |
967 | |
|
968 | 4 | FileOutputStream os = null; |
969 | |
try |
970 | |
{ |
971 | 4 | os = new FileOutputStream( file ); |
972 | |
|
973 | 4 | IOUtil.copy( is, os ); |
974 | |
} |
975 | |
finally |
976 | |
{ |
977 | 4 | IOUtil.close( is ); |
978 | |
|
979 | 4 | IOUtil.close( os ); |
980 | 4 | } |
981 | 4 | } |
982 | |
|
983 | |
|
984 | |
|
985 | |
|
986 | |
|
987 | |
|
988 | |
|
989 | |
|
990 | |
|
991 | |
|
992 | |
|
993 | |
|
994 | |
|
995 | |
|
996 | |
|
997 | |
|
998 | |
|
999 | |
protected static void invokeMaven( Log log, File localRepositoryDir, File projectFile, List goals, |
1000 | |
Properties properties, File invokerLog ) |
1001 | |
throws MavenInvocationException |
1002 | |
{ |
1003 | 4 | if ( projectFile == null ) |
1004 | |
{ |
1005 | 0 | throw new IllegalArgumentException( "projectFile should be not null." ); |
1006 | |
} |
1007 | 4 | if ( !projectFile.isFile() ) |
1008 | |
{ |
1009 | 0 | throw new IllegalArgumentException( projectFile.getAbsolutePath() + " is not a file." ); |
1010 | |
} |
1011 | 4 | if ( goals == null || goals.size() == 0 ) |
1012 | |
{ |
1013 | 0 | throw new IllegalArgumentException( "goals should be not empty." ); |
1014 | |
} |
1015 | 4 | if ( localRepositoryDir == null || !localRepositoryDir.isDirectory() ) |
1016 | |
{ |
1017 | 0 | throw new IllegalArgumentException( "localRepositoryDir '" + localRepositoryDir |
1018 | |
+ "' should be a directory." ); |
1019 | |
} |
1020 | |
|
1021 | 4 | String mavenHome = getMavenHome( log ); |
1022 | 4 | if ( StringUtils.isEmpty( mavenHome ) ) |
1023 | |
{ |
1024 | 0 | String msg = |
1025 | |
"Could NOT invoke Maven because no Maven Home is defined. You need to have set the M2_HOME " |
1026 | |
+ "system env variable or a maven.home Java system properties."; |
1027 | 0 | if ( log != null ) |
1028 | |
{ |
1029 | 0 | log.error( msg ); |
1030 | |
} |
1031 | |
else |
1032 | |
{ |
1033 | 0 | System.err.println( msg ); |
1034 | |
} |
1035 | 0 | return; |
1036 | |
} |
1037 | |
|
1038 | 4 | Invoker invoker = new DefaultInvoker(); |
1039 | 4 | invoker.setMavenHome( new File( mavenHome ) ); |
1040 | 4 | invoker.setLocalRepositoryDirectory( localRepositoryDir ); |
1041 | |
|
1042 | 4 | InvocationRequest request = new DefaultInvocationRequest(); |
1043 | 4 | request.setBaseDirectory( projectFile.getParentFile() ); |
1044 | 4 | request.setPomFile( projectFile ); |
1045 | 4 | if ( log != null ) |
1046 | |
{ |
1047 | 4 | request.setDebug( log.isDebugEnabled() ); |
1048 | |
} |
1049 | |
else |
1050 | |
{ |
1051 | 0 | request.setDebug( true ); |
1052 | |
} |
1053 | 4 | request.setGoals( goals ); |
1054 | 4 | if ( properties != null ) |
1055 | |
{ |
1056 | 2 | request.setProperties( properties ); |
1057 | |
} |
1058 | 4 | File javaHome = getJavaHome( log ); |
1059 | 4 | if ( javaHome != null ) |
1060 | |
{ |
1061 | 4 | request.setJavaHome( javaHome ); |
1062 | |
} |
1063 | |
|
1064 | 4 | if ( log != null && log.isDebugEnabled() ) |
1065 | |
{ |
1066 | 0 | log.debug( "Invoking Maven for the goals: " + goals + " with " |
1067 | |
+ ( properties == null ? "no properties" : "properties=" + properties ) ); |
1068 | |
} |
1069 | 4 | InvocationResult result = invoke( log, invoker, request, invokerLog, goals, properties, null ); |
1070 | |
|
1071 | 4 | if ( result.getExitCode() != 0 ) |
1072 | |
{ |
1073 | 0 | String invokerLogContent = readFile( invokerLog, "UTF-8" ); |
1074 | |
|
1075 | |
|
1076 | 0 | if ( invokerLogContent != null && ( invokerLogContent.indexOf( "Scanning for projects..." ) == -1 |
1077 | |
|| invokerLogContent.indexOf( OutOfMemoryError.class.getName() ) != -1 ) ) |
1078 | |
{ |
1079 | 0 | if ( log != null ) |
1080 | |
{ |
1081 | 0 | log.error( "Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS..." ); |
1082 | |
|
1083 | 0 | if ( log.isDebugEnabled() ) |
1084 | |
{ |
1085 | 0 | log.debug( "Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS..." ); |
1086 | |
} |
1087 | |
} |
1088 | 0 | result = invoke( log, invoker, request, invokerLog, goals, properties, "" ); |
1089 | |
} |
1090 | |
} |
1091 | |
|
1092 | 4 | if ( result.getExitCode() != 0 ) |
1093 | |
{ |
1094 | 0 | String invokerLogContent = readFile( invokerLog, "UTF-8" ); |
1095 | |
|
1096 | |
|
1097 | 0 | if ( invokerLogContent != null && ( invokerLogContent.indexOf( "Scanning for projects..." ) == -1 |
1098 | |
|| invokerLogContent.indexOf( OutOfMemoryError.class.getName() ) != -1 ) ) |
1099 | |
{ |
1100 | 0 | throw new MavenInvocationException( ERROR_INIT_VM ); |
1101 | |
} |
1102 | |
|
1103 | 0 | throw new MavenInvocationException( "Error when invoking Maven, consult the invoker log file: " |
1104 | |
+ invokerLog.getAbsolutePath() ); |
1105 | |
} |
1106 | 4 | } |
1107 | |
|
1108 | |
|
1109 | |
|
1110 | |
|
1111 | |
|
1112 | |
|
1113 | |
|
1114 | |
|
1115 | |
|
1116 | |
|
1117 | |
protected static String readFile( final File javaFile, final String encoding ) |
1118 | |
{ |
1119 | |
try |
1120 | |
{ |
1121 | 0 | return FileUtils.fileRead( javaFile, encoding ); |
1122 | |
} |
1123 | 0 | catch (IOException e ) |
1124 | |
{ |
1125 | 0 | return null; |
1126 | |
} |
1127 | |
} |
1128 | |
|
1129 | |
|
1130 | |
|
1131 | |
|
1132 | |
|
1133 | |
|
1134 | |
|
1135 | |
|
1136 | |
|
1137 | |
|
1138 | |
|
1139 | |
|
1140 | |
|
1141 | |
|
1142 | |
|
1143 | |
|
1144 | |
protected static String[] splitPath( final String path ) |
1145 | |
{ |
1146 | 9 | if ( path == null ) |
1147 | |
{ |
1148 | 0 | return null; |
1149 | |
} |
1150 | |
|
1151 | 9 | List subpaths = new ArrayList(); |
1152 | 9 | PathTokenizer pathTokenizer = new PathTokenizer( path ); |
1153 | 42 | while ( pathTokenizer.hasMoreTokens() ) |
1154 | |
{ |
1155 | 33 | subpaths.add( pathTokenizer.nextToken() ); |
1156 | |
} |
1157 | |
|
1158 | 9 | return (String[]) subpaths.toArray( new String[0] ); |
1159 | |
} |
1160 | |
|
1161 | |
|
1162 | |
|
1163 | |
|
1164 | |
|
1165 | |
|
1166 | |
|
1167 | |
|
1168 | |
|
1169 | |
|
1170 | |
|
1171 | |
|
1172 | |
|
1173 | |
|
1174 | |
|
1175 | |
|
1176 | |
|
1177 | |
protected static String unifyPathSeparator( final String path ) |
1178 | |
{ |
1179 | 48 | if ( path == null ) |
1180 | |
{ |
1181 | 40 | return null; |
1182 | |
} |
1183 | |
|
1184 | 8 | return StringUtils.join( splitPath( path ), File.pathSeparator ); |
1185 | |
} |
1186 | |
|
1187 | |
|
1188 | |
|
1189 | |
|
1190 | |
|
1191 | |
|
1192 | |
|
1193 | |
|
1194 | |
|
1195 | |
|
1196 | |
private static List getClassNamesFromJar( File jarFile ) |
1197 | |
throws IOException |
1198 | |
{ |
1199 | 2 | if ( jarFile == null || !jarFile.exists() || !jarFile.isFile() ) |
1200 | |
{ |
1201 | 0 | throw new IOException( "The jar '" + jarFile + "' doesn't exist or is not a file." ); |
1202 | |
} |
1203 | |
|
1204 | 2 | List classes = new ArrayList(); |
1205 | 2 | JarInputStream jarStream = null; |
1206 | |
|
1207 | |
try |
1208 | |
{ |
1209 | 2 | jarStream = new JarInputStream( new FileInputStream( jarFile ) ); |
1210 | 2 | JarEntry jarEntry = jarStream.getNextJarEntry(); |
1211 | 48 | while ( jarEntry != null ) |
1212 | |
{ |
1213 | 46 | if ( jarEntry == null ) |
1214 | |
{ |
1215 | 0 | break; |
1216 | |
} |
1217 | |
|
1218 | 46 | if ( jarEntry.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".class" ) ) |
1219 | |
{ |
1220 | 24 | String name = jarEntry.getName().substring( 0, jarEntry.getName().indexOf( "." ) ); |
1221 | |
|
1222 | 24 | classes.add( name.replaceAll( "/", "\\." ) ); |
1223 | |
} |
1224 | |
|
1225 | 46 | jarStream.closeEntry(); |
1226 | 46 | jarEntry = jarStream.getNextJarEntry(); |
1227 | |
} |
1228 | |
} |
1229 | |
finally |
1230 | |
{ |
1231 | 2 | IOUtil.close( jarStream ); |
1232 | 2 | } |
1233 | |
|
1234 | 2 | return classes; |
1235 | |
} |
1236 | |
|
1237 | |
|
1238 | |
|
1239 | |
|
1240 | |
|
1241 | |
|
1242 | |
|
1243 | |
|
1244 | |
|
1245 | |
|
1246 | |
|
1247 | |
|
1248 | |
|
1249 | |
private static InvocationResult invoke( Log log, Invoker invoker, InvocationRequest request, File invokerLog, |
1250 | |
List goals, Properties properties, String mavenOpts ) |
1251 | |
throws MavenInvocationException |
1252 | |
{ |
1253 | |
PrintStream ps; |
1254 | 4 | OutputStream os = null; |
1255 | 4 | if ( invokerLog != null ) |
1256 | |
{ |
1257 | 4 | if ( log != null && log.isDebugEnabled() ) |
1258 | |
{ |
1259 | 0 | log.debug( "Using " + invokerLog.getAbsolutePath() + " to log the invoker" ); |
1260 | |
} |
1261 | |
|
1262 | |
try |
1263 | |
{ |
1264 | 4 | if ( !invokerLog.exists() ) |
1265 | |
{ |
1266 | 4 | invokerLog.getParentFile().mkdirs(); |
1267 | |
} |
1268 | 4 | os = new FileOutputStream( invokerLog ); |
1269 | 4 | ps = new PrintStream( os, true, "UTF-8" ); |
1270 | |
} |
1271 | 0 | catch ( FileNotFoundException e ) |
1272 | |
{ |
1273 | 0 | if ( log != null && log.isErrorEnabled() ) |
1274 | |
{ |
1275 | 0 | log.error( "FileNotFoundException: " + e.getMessage() + ". Using System.out to log the invoker." ); |
1276 | |
} |
1277 | 0 | ps = System.out; |
1278 | |
} |
1279 | 0 | catch ( UnsupportedEncodingException e ) |
1280 | |
{ |
1281 | 0 | if ( log != null && log.isErrorEnabled() ) |
1282 | |
{ |
1283 | 0 | log.error( "UnsupportedEncodingException: " + e.getMessage() |
1284 | |
+ ". Using System.out to log the invoker." ); |
1285 | |
} |
1286 | 0 | ps = System.out; |
1287 | 4 | } |
1288 | |
} |
1289 | |
else |
1290 | |
{ |
1291 | 0 | if ( log != null && log.isDebugEnabled() ) |
1292 | |
{ |
1293 | 0 | log.debug( "Using System.out to log the invoker." ); |
1294 | |
} |
1295 | |
|
1296 | 0 | ps = System.out; |
1297 | |
} |
1298 | |
|
1299 | 4 | if ( mavenOpts != null ) |
1300 | |
{ |
1301 | 0 | request.setMavenOpts( mavenOpts ); |
1302 | |
} |
1303 | |
|
1304 | 4 | InvocationOutputHandler outputHandler = new PrintStreamHandler( ps, false ); |
1305 | 4 | request.setOutputHandler( outputHandler ); |
1306 | |
|
1307 | 4 | outputHandler.consumeLine( "Invoking Maven for the goals: " + goals + " with " |
1308 | |
+ ( properties == null ? "no properties" : "properties=" + properties ) ); |
1309 | 4 | outputHandler.consumeLine( "" ); |
1310 | 4 | outputHandler.consumeLine( "M2_HOME=" + getMavenHome( log ) ); |
1311 | 4 | outputHandler.consumeLine( "MAVEN_OPTS=" + getMavenOpts( log ) ); |
1312 | 4 | outputHandler.consumeLine( "JAVA_HOME=" + getJavaHome( log ) ); |
1313 | 4 | outputHandler.consumeLine( "JAVA_OPTS=" + getJavaOpts( log ) ); |
1314 | 4 | outputHandler.consumeLine( "" ); |
1315 | |
|
1316 | |
try |
1317 | |
{ |
1318 | 4 | return invoker.execute( request ); |
1319 | |
} |
1320 | |
finally |
1321 | |
{ |
1322 | 4 | IOUtil.close( os ); |
1323 | 4 | ps = null; |
1324 | |
} |
1325 | |
} |
1326 | |
|
1327 | |
|
1328 | |
|
1329 | |
|
1330 | |
|
1331 | |
|
1332 | |
|
1333 | |
private static String getMavenHome( Log log ) |
1334 | |
{ |
1335 | 8 | String mavenHome = System.getProperty( "maven.home" ); |
1336 | 8 | if ( mavenHome == null ) |
1337 | |
{ |
1338 | |
try |
1339 | |
{ |
1340 | 0 | mavenHome = CommandLineUtils.getSystemEnvVars().getProperty( "M2_HOME" ); |
1341 | |
} |
1342 | 0 | catch ( IOException e ) |
1343 | |
{ |
1344 | 0 | if ( log != null && log.isDebugEnabled() ) |
1345 | |
{ |
1346 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1347 | |
} |
1348 | 0 | } |
1349 | |
} |
1350 | |
|
1351 | 8 | File m2Home = new File( mavenHome ); |
1352 | 8 | if ( !m2Home.exists() ) |
1353 | |
{ |
1354 | 0 | if ( log != null && log.isErrorEnabled() ) |
1355 | |
{ |
1356 | 0 | log |
1357 | |
.error( "Cannot find Maven application directory. Either specify \'maven.home\' system property, or " |
1358 | |
+ "M2_HOME environment variable." ); |
1359 | |
} |
1360 | |
} |
1361 | |
|
1362 | 8 | return mavenHome; |
1363 | |
} |
1364 | |
|
1365 | |
|
1366 | |
|
1367 | |
|
1368 | |
|
1369 | |
|
1370 | |
private static String getMavenOpts( Log log ) |
1371 | |
{ |
1372 | 4 | String mavenOpts = null; |
1373 | |
try |
1374 | |
{ |
1375 | 4 | mavenOpts = CommandLineUtils.getSystemEnvVars().getProperty( "MAVEN_OPTS" ); |
1376 | |
} |
1377 | 0 | catch ( IOException e ) |
1378 | |
{ |
1379 | 0 | if ( log != null && log.isDebugEnabled() ) |
1380 | |
{ |
1381 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1382 | |
} |
1383 | 4 | } |
1384 | |
|
1385 | 4 | return mavenOpts; |
1386 | |
} |
1387 | |
|
1388 | |
|
1389 | |
|
1390 | |
|
1391 | |
|
1392 | |
|
1393 | |
|
1394 | |
|
1395 | |
private static File getJavaHome( Log log ) |
1396 | |
{ |
1397 | |
File javaHome; |
1398 | 8 | if ( SystemUtils.IS_OS_MAC_OSX ) |
1399 | |
{ |
1400 | 8 | javaHome = SystemUtils.getJavaHome(); |
1401 | |
} |
1402 | |
else |
1403 | |
{ |
1404 | 0 | javaHome = new File( SystemUtils.getJavaHome(), ".." ); |
1405 | |
} |
1406 | |
|
1407 | 8 | if ( javaHome == null || !javaHome.exists() ) |
1408 | |
{ |
1409 | |
try |
1410 | |
{ |
1411 | 0 | javaHome = new File( CommandLineUtils.getSystemEnvVars().getProperty( "JAVA_HOME" ) ); |
1412 | |
} |
1413 | 0 | catch ( IOException e ) |
1414 | |
{ |
1415 | 0 | if ( log != null && log.isDebugEnabled() ) |
1416 | |
{ |
1417 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1418 | |
} |
1419 | 0 | } |
1420 | |
} |
1421 | |
|
1422 | 8 | if ( javaHome == null || !javaHome.exists() ) |
1423 | |
{ |
1424 | 0 | if ( log != null && log.isErrorEnabled() ) |
1425 | |
{ |
1426 | 0 | log.error( "Cannot find Java application directory. Either specify \'java.home\' system property, or " |
1427 | |
+ "JAVA_HOME environment variable." ); |
1428 | |
} |
1429 | |
} |
1430 | |
|
1431 | 8 | return javaHome; |
1432 | |
} |
1433 | |
|
1434 | |
|
1435 | |
|
1436 | |
|
1437 | |
|
1438 | |
|
1439 | |
private static String getJavaOpts( Log log ) |
1440 | |
{ |
1441 | 4 | String javaOpts = null; |
1442 | |
try |
1443 | |
{ |
1444 | 4 | javaOpts = CommandLineUtils.getSystemEnvVars().getProperty( "JAVA_OPTS" ); |
1445 | |
} |
1446 | 0 | catch ( IOException e ) |
1447 | |
{ |
1448 | 0 | if ( log != null && log.isDebugEnabled() ) |
1449 | |
{ |
1450 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1451 | |
} |
1452 | 4 | } |
1453 | |
|
1454 | 4 | return javaOpts; |
1455 | |
} |
1456 | |
|
1457 | |
|
1458 | |
|
1459 | |
|
1460 | |
|
1461 | |
|
1462 | |
|
1463 | |
|
1464 | |
|
1465 | |
|
1466 | |
|
1467 | 0 | private static class PathTokenizer |
1468 | |
{ |
1469 | |
|
1470 | |
|
1471 | |
|
1472 | |
private StringTokenizer tokenizer; |
1473 | |
|
1474 | |
|
1475 | |
|
1476 | |
|
1477 | |
|
1478 | 9 | private String lookahead = null; |
1479 | |
|
1480 | |
|
1481 | |
|
1482 | |
|
1483 | |
|
1484 | |
|
1485 | 9 | private boolean onNetWare = Os.isFamily( "netware" ); |
1486 | |
|
1487 | |
|
1488 | |
|
1489 | |
|
1490 | |
|
1491 | |
private boolean dosStyleFilesystem; |
1492 | |
|
1493 | |
|
1494 | |
|
1495 | |
|
1496 | |
|
1497 | |
|
1498 | |
public PathTokenizer( String path ) |
1499 | 9 | { |
1500 | 9 | if ( onNetWare ) |
1501 | |
{ |
1502 | |
|
1503 | |
|
1504 | 0 | tokenizer = new StringTokenizer( path, ":;", true ); |
1505 | |
} |
1506 | |
else |
1507 | |
{ |
1508 | |
|
1509 | |
|
1510 | 9 | tokenizer = new StringTokenizer( path, ":;", false ); |
1511 | |
} |
1512 | 9 | dosStyleFilesystem = File.pathSeparatorChar == ';'; |
1513 | 9 | } |
1514 | |
|
1515 | |
|
1516 | |
|
1517 | |
|
1518 | |
|
1519 | |
|
1520 | |
|
1521 | |
|
1522 | |
|
1523 | |
public boolean hasMoreTokens() |
1524 | |
{ |
1525 | 42 | if ( lookahead != null ) |
1526 | |
{ |
1527 | 0 | return true; |
1528 | |
} |
1529 | |
|
1530 | 42 | return tokenizer.hasMoreTokens(); |
1531 | |
} |
1532 | |
|
1533 | |
|
1534 | |
|
1535 | |
|
1536 | |
|
1537 | |
|
1538 | |
|
1539 | |
|
1540 | |
|
1541 | |
public String nextToken() |
1542 | |
throws NoSuchElementException |
1543 | |
{ |
1544 | 33 | String token = null; |
1545 | 33 | if ( lookahead != null ) |
1546 | |
{ |
1547 | 0 | token = lookahead; |
1548 | 0 | lookahead = null; |
1549 | |
} |
1550 | |
else |
1551 | |
{ |
1552 | 33 | token = tokenizer.nextToken().trim(); |
1553 | |
} |
1554 | |
|
1555 | 33 | if ( !onNetWare ) |
1556 | |
{ |
1557 | 33 | if ( token.length() == 1 && Character.isLetter( token.charAt( 0 ) ) && dosStyleFilesystem |
1558 | |
&& tokenizer.hasMoreTokens() ) |
1559 | |
{ |
1560 | |
|
1561 | |
|
1562 | 0 | String nextToken = tokenizer.nextToken().trim(); |
1563 | 0 | if ( nextToken.startsWith( "\\" ) || nextToken.startsWith( "/" ) ) |
1564 | |
{ |
1565 | |
|
1566 | |
|
1567 | |
|
1568 | 0 | token += ":" + nextToken; |
1569 | |
} |
1570 | |
else |
1571 | |
{ |
1572 | |
|
1573 | 0 | lookahead = nextToken; |
1574 | |
} |
1575 | 0 | } |
1576 | |
} |
1577 | |
else |
1578 | |
{ |
1579 | |
|
1580 | |
|
1581 | 0 | if ( token.equals( File.pathSeparator ) || token.equals( ":" ) ) |
1582 | |
{ |
1583 | |
|
1584 | 0 | token = tokenizer.nextToken().trim(); |
1585 | |
} |
1586 | |
|
1587 | 0 | if ( tokenizer.hasMoreTokens() ) |
1588 | |
{ |
1589 | |
|
1590 | 0 | String nextToken = tokenizer.nextToken().trim(); |
1591 | |
|
1592 | |
|
1593 | 0 | if ( !nextToken.equals( File.pathSeparator ) ) |
1594 | |
{ |
1595 | 0 | if ( nextToken.equals( ":" ) ) |
1596 | |
{ |
1597 | 0 | if ( !token.startsWith( "/" ) && !token.startsWith( "\\" ) && !token.startsWith( "." ) |
1598 | |
&& !token.startsWith( ".." ) ) |
1599 | |
{ |
1600 | |
|
1601 | 0 | String oneMore = tokenizer.nextToken().trim(); |
1602 | 0 | if ( !oneMore.equals( File.pathSeparator ) ) |
1603 | |
{ |
1604 | 0 | token += ":" + oneMore; |
1605 | |
} |
1606 | |
else |
1607 | |
{ |
1608 | 0 | token += ":"; |
1609 | 0 | lookahead = oneMore; |
1610 | |
} |
1611 | 0 | } |
1612 | |
|
1613 | |
|
1614 | |
} |
1615 | |
else |
1616 | |
{ |
1617 | |
|
1618 | 0 | lookahead = nextToken; |
1619 | |
} |
1620 | |
} |
1621 | |
} |
1622 | |
} |
1623 | 33 | return token; |
1624 | |
} |
1625 | |
} |
1626 | |
|
1627 | |
static List<String> toList( String src ) |
1628 | |
{ |
1629 | 42 | return toList( src, null, null ); |
1630 | |
} |
1631 | |
|
1632 | |
static List<String> toList( String src, String elementPrefix, String elementSuffix ) |
1633 | |
{ |
1634 | 42 | if ( StringUtils.isEmpty( src ) ) |
1635 | |
{ |
1636 | 40 | return null; |
1637 | |
} |
1638 | |
|
1639 | 2 | List<String> result = new ArrayList<String>(); |
1640 | |
|
1641 | 2 | StringTokenizer st = new StringTokenizer( src, "[,:;]" ); |
1642 | 2 | StringBuilder sb = new StringBuilder( 256 ); |
1643 | 6 | while ( st.hasMoreTokens() ) |
1644 | |
{ |
1645 | 4 | sb.setLength( 0 ); |
1646 | 4 | if ( StringUtils.isNotEmpty( elementPrefix ) ) |
1647 | |
{ |
1648 | 0 | sb.append( elementPrefix ); |
1649 | |
} |
1650 | |
|
1651 | 4 | sb.append( st.nextToken() ); |
1652 | |
|
1653 | 4 | if ( StringUtils.isNotEmpty( elementSuffix ) ) |
1654 | |
{ |
1655 | 0 | sb.append( elementSuffix ); |
1656 | |
} |
1657 | |
|
1658 | 4 | result.add( sb.toString() ); |
1659 | |
} |
1660 | |
|
1661 | 2 | return result; |
1662 | |
} |
1663 | |
|
1664 | |
static <T> List<T> toList( T[] multiple ) |
1665 | |
{ |
1666 | 252 | return toList( null, multiple ); |
1667 | |
} |
1668 | |
|
1669 | |
static <T> List<T> toList( T single, T[] multiple ) |
1670 | |
{ |
1671 | 336 | if ( single == null && ( multiple == null || multiple.length < 1 ) ) |
1672 | |
{ |
1673 | 328 | return null; |
1674 | |
} |
1675 | |
|
1676 | 8 | List<T> result = new ArrayList<T>(); |
1677 | 8 | if ( single != null ) |
1678 | |
{ |
1679 | 2 | result.add( single ); |
1680 | |
} |
1681 | |
|
1682 | 8 | if ( multiple != null && multiple.length > 0 ) |
1683 | |
{ |
1684 | 6 | result.addAll( Arrays.asList( multiple ) ); |
1685 | |
} |
1686 | |
|
1687 | 8 | return result; |
1688 | |
} |
1689 | |
|
1690 | |
|
1691 | |
public static String toRelative( File basedir, String absolutePath ) |
1692 | |
{ |
1693 | |
String relative; |
1694 | |
|
1695 | 16 | absolutePath = absolutePath.replace( '\\', '/' ); |
1696 | 16 | String basedirPath = basedir.getAbsolutePath().replace( '\\', '/' ); |
1697 | |
|
1698 | 16 | if ( absolutePath.startsWith( basedirPath ) ) |
1699 | |
{ |
1700 | 16 | relative = absolutePath.substring( basedirPath.length() ); |
1701 | 16 | if ( relative.startsWith( "/" ) ) |
1702 | |
{ |
1703 | 16 | relative = relative.substring( 1 ); |
1704 | |
} |
1705 | 16 | if ( relative.length() <= 0 ) |
1706 | |
{ |
1707 | 0 | relative = "."; |
1708 | |
} |
1709 | |
} |
1710 | |
else |
1711 | |
{ |
1712 | 0 | relative = absolutePath; |
1713 | |
} |
1714 | |
|
1715 | 16 | return relative; |
1716 | |
} |
1717 | |
|
1718 | |
|
1719 | |
|
1720 | |
|
1721 | |
public static boolean isNotEmpty( final Collection<?> collection ) |
1722 | |
{ |
1723 | 74 | return collection != null && !collection.isEmpty(); |
1724 | |
} |
1725 | |
|
1726 | |
|
1727 | |
|
1728 | |
|
1729 | |
public static boolean isEmpty( final Collection<?> collection ) |
1730 | |
{ |
1731 | 142 | return collection == null || collection.isEmpty(); |
1732 | |
} |
1733 | |
|
1734 | |
} |