1 package org.apache.maven.plugins.artifact.buildinfo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.shared.utils.StringUtils;
27 import org.apache.maven.shared.utils.cli.CommandLineException;
28 import org.apache.maven.shared.utils.cli.CommandLineUtils;
29 import org.apache.maven.shared.utils.cli.Commandline;
30 import org.apache.maven.shared.utils.cli.StreamConsumer;
31 import org.apache.maven.toolchain.Toolchain;
32
33
34
35
36 class JdkToolchainUtil
37 {
38 static String getJavaVersion( Toolchain toolchain )
39 {
40 String version = "unknown";
41 String java = toolchain.findTool( "java" );
42 if ( java != null )
43 {
44 try
45 {
46 Commandline cl = new Commandline( java + " -version" );
47 LineConsumer out = new LineConsumer();
48 LineConsumer err = new LineConsumer();
49 CommandLineUtils.executeCommandLine( cl, out, err );
50 version = StringUtils.join( err.getLines().iterator(), ":" );
51 if ( version == null )
52 {
53 version = "unable to detect...";
54 }
55 }
56 catch ( CommandLineException cle )
57 {
58 version = cle.toString();
59 }
60 }
61 return version;
62 }
63
64 private static class LineConsumer implements StreamConsumer
65 {
66 private List<String> lines = new ArrayList<>();
67
68 @Override
69 public void consumeLine( String line )
70 throws IOException
71 {
72 lines.add( line );
73 }
74
75 List<String> getLines()
76 {
77 return lines;
78 }
79 }
80 }