View Javadoc
1   package org.apache.maven.plugins.artifact.buildinfo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A helper to get JDK version from a JDK toolchain.
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  }