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 static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Locale;
29  import java.util.Properties;
30  
31  import org.apache.commons.lang3.StringUtils;
32  import org.codehaus.plexus.util.Os;
33  import org.slf4j.Logger;
34  
35  /**
36   * Utility class used to report errors, statistics, application version info, etc.
37   *
38   * @author jdcasey
39   */
40  public final class CLIReportingUtils
41  {
42      // CHECKSTYLE_OFF: MagicNumber
43      public static final long MB = 1024 * 1024;
44  
45      private static final long ONE_SECOND = 1000L;
46  
47      private static final long ONE_MINUTE = 60 * ONE_SECOND;
48  
49      private static final long ONE_HOUR = 60 * ONE_MINUTE;
50  
51      private static final long ONE_DAY = 24 * ONE_HOUR;
52      // CHECKSTYLE_ON: MagicNumber
53  
54      public static final String BUILD_VERSION_PROPERTY = "version";
55  
56      public static String showVersion()
57      {
58          final String ls = System.getProperty( "line.separator" );
59          Properties properties = getBuildProperties();
60          StringBuilder version = new StringBuilder( 256 );
61          version.append( buffer().strong( createMavenVersionString( properties ) ) ).append( ls );
62          version.append( reduce(
63              properties.getProperty( "distributionShortName" ) + " home: " + System.getProperty( "maven.home",
64                                                                                                  "<unknown Maven "
65                                                                                                      + "home>" ) ) )
66              .append( ls );
67          version.append( "Java version: " ).append(
68              System.getProperty( "java.version", "<unknown Java version>" ) ).append( ", vendor: " ).append(
69              System.getProperty( "java.vendor", "<unknown vendor>" ) ).append( ", runtime: " ).append(
70              System.getProperty( "java.home", "<unknown runtime>" ) ).append( ls );
71          version.append( "Default locale: " ).append( Locale.getDefault() ).append( ", platform encoding: " ).append(
72              System.getProperty( "file.encoding", "<unknown encoding>" ) ).append( ls );
73          version.append( "OS name: \"" ).append( Os.OS_NAME ).append( "\", version: \"" ).append( Os.OS_VERSION ).append(
74              "\", arch: \"" ).append( Os.OS_ARCH ).append( "\", family: \"" ).append( Os.OS_FAMILY ).append( '\"' );
75          return version.toString();
76      }
77  
78      /**
79       * Create a human readable string containing the Maven version, buildnumber, and time of build
80       *
81       * @param buildProperties The build properties
82       * @return Readable build info
83       */
84      static String createMavenVersionString( Properties buildProperties )
85      {
86          String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
87          String version = reduce( buildProperties.getProperty( BUILD_VERSION_PROPERTY ) );
88          String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
89          String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
90  
91          String msg = distributionName + " ";
92          msg += ( version != null ? version : "<version unknown>" );
93          if ( rev != null || timestamp != null )
94          {
95              msg += " (";
96              msg += ( rev != null ? rev : "" );
97              if ( StringUtils.isNotBlank( timestamp ) )
98              {
99                  String ts = formatTimestamp( Long.valueOf( timestamp ) );
100                 msg += ( rev != null ? "; " : "" ) + ts;
101             }
102             msg += ")";
103         }
104         return msg;
105     }
106 
107     private static String reduce( String s )
108     {
109         return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
110     }
111 
112     static Properties getBuildProperties()
113     {
114         Properties properties = new Properties();
115 
116         try ( InputStream resourceAsStream = MavenCli.class.getResourceAsStream(
117             "/org/apache/maven/messages/build.properties" ) )
118         {
119 
120             if ( resourceAsStream != null )
121             {
122                 properties.load( resourceAsStream );
123             }
124         }
125         catch ( IOException e )
126         {
127             System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
128         }
129 
130         return properties;
131     }
132 
133     public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
134     {
135         if ( showStackTrace )
136         {
137             logger.error( message, e );
138         }
139         else
140         {
141             logger.error( message );
142 
143             if ( e != null )
144             {
145                 logger.error( e.getMessage() );
146 
147                 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
148                 {
149                     logger.error( "Caused by: {}", cause.getMessage() );
150                 }
151             }
152         }
153     }
154 
155     public static String formatTimestamp( long timestamp )
156     {
157         SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssXXX" );
158         return sdf.format( new Date( timestamp ) );
159     }
160 
161     public static String formatDuration( long duration )
162     {
163         // CHECKSTYLE_OFF: MagicNumber
164         long ms = duration % 1000;
165         long s = ( duration / ONE_SECOND ) % 60;
166         long m = ( duration / ONE_MINUTE ) % 60;
167         long h = ( duration / ONE_HOUR ) % 24;
168         long d = duration / ONE_DAY;
169         // CHECKSTYLE_ON: MagicNumber
170 
171         String format;
172         if ( d > 0 )
173         {
174             // Length 11+ chars
175             format = "%d d %02d:%02d h";
176         }
177         else if ( h > 0 )
178         {
179             // Length 7 chars
180             format = "%2$02d:%3$02d h";
181         }
182         else if ( m > 0 )
183         {
184             // Length 9 chars
185             format = "%3$02d:%4$02d min";
186         }
187         else
188         {
189             // Length 7-8 chars
190             format = "%4$d.%5$03d s";
191         }
192 
193         return String.format( format, d, h, m, s, ms );
194     }
195 
196 }