View Javadoc

1   package org.apache.maven.cli;
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.io.InputStream;
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.Locale;
27  import java.util.Properties;
28  
29  import org.codehaus.plexus.util.IOUtil;
30  import org.codehaus.plexus.util.Os;
31  import org.slf4j.Logger;
32  
33  /**
34   * Utility class used to report errors, statistics, application version info, etc.
35   *
36   * @author jdcasey
37   *
38   */
39  public final class CLIReportingUtils
40  {
41  
42      public static final long MB = 1024 * 1024;
43  
44      public static final int MS_PER_SEC = 1000;
45  
46      public static final int SEC_PER_MIN = 60;
47      
48      public static final String BUILD_VERSION_PROPERTY = "version";
49  
50      public static String showVersion()
51      {
52          final String LS = System.getProperty( "line.separator" );
53          Properties properties = getBuildProperties();
54          StringBuffer version = new StringBuffer();
55          version.append( createMavenVersionString( properties ) ).append( LS );
56          version.append( reduce( properties.getProperty( "distributionShortName" ) + " home: "
57                              + System.getProperty( "maven.home", "<unknown maven home>" ) ) ).append( LS );
58          version.append( "Java version: " + System.getProperty( "java.version", "<unknown java version>" )
59                              + ", vendor: " + System.getProperty( "java.vendor", "<unknown vendor>" ) ).append( LS );
60          version.append( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) ).append( LS );
61          version.append( "Default locale: " + Locale.getDefault() + ", platform encoding: "
62                              + System.getProperty( "file.encoding", "<unknown encoding>" ) ).append( LS );
63          version.append( "OS name: \"" + Os.OS_NAME + "\", version: \"" + Os.OS_VERSION + "\", arch: \"" + Os.OS_ARCH
64              + "\", family: \"" + Os.OS_FAMILY + "\"" );
65          return version.toString();
66      }
67  
68      /**
69       * Create a human readable string containing the Maven version, buildnumber, and time of build
70       * 
71       * @param buildProperties The build properties
72       * @return Readable build info
73       */
74      static String createMavenVersionString( Properties buildProperties )
75      {
76          String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
77          String version = reduce( buildProperties.getProperty( BUILD_VERSION_PROPERTY ) );
78          String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
79          String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
80  
81          String msg = distributionName + " ";
82          msg += ( version != null ? version : "<version unknown>" );
83          if ( rev != null || timestamp != null )
84          {
85              msg += " (";
86              msg += ( rev != null ? rev : "" );
87              if ( timestamp != null )
88              {
89                  SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" );
90                  String ts = fmt.format( new Date( Long.valueOf( timestamp ) ) );
91                  msg += ( rev != null ? "; " : "" ) + ts;
92              }
93              msg += ")";
94          }
95          return msg;
96      }
97  
98      private static String reduce( String s )
99      {
100         return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
101     }
102 
103     static Properties getBuildProperties()
104     {
105         Properties properties = new Properties();
106         InputStream resourceAsStream = null;
107         try
108         {
109             resourceAsStream = MavenCli.class.getResourceAsStream( "/org/apache/maven/messages/build.properties" );
110 
111             if ( resourceAsStream != null )
112             {
113                 properties.load( resourceAsStream );
114             }
115         }
116         catch ( IOException e )
117         {
118             System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
119         }
120         finally
121         {
122             IOUtil.close( resourceAsStream );
123         }
124 
125         return properties;
126     }
127 
128     public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
129     {
130         if ( showStackTrace )
131         {
132             logger.error( message, e );
133         }
134         else
135         {
136             logger.error( message );
137 
138             if ( e != null )
139             {
140                 logger.error( e.getMessage() );
141 
142                 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
143                 {
144                     logger.error( "Caused by: " + cause.getMessage() );
145                 }
146             }
147         }
148     }
149 
150 }