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.lineSeparator();
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      public static String showVersionMinimal()
79      {
80          Properties properties = getBuildProperties();
81          String version = reduce( properties.getProperty( BUILD_VERSION_PROPERTY ) );
82          return ( version != null ? version : "<version unknown>" );
83      }
84  
85      /**
86       * Create a human readable string containing the Maven version, buildnumber, and time of build
87       *
88       * @param buildProperties The build properties
89       * @return Readable build info
90       */
91      static String createMavenVersionString( Properties buildProperties )
92      {
93          String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
94          String version = reduce( buildProperties.getProperty( BUILD_VERSION_PROPERTY ) );
95          String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
96          String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
97  
98          String msg = distributionName + " ";
99          msg += ( version != null ? version : "<version unknown>" );
100         if ( rev != null || timestamp != null )
101         {
102             msg += " (";
103             msg += ( rev != null ? rev : "" );
104             if ( StringUtils.isNotBlank( timestamp ) )
105             {
106                 String ts = formatTimestamp( Long.parseLong( timestamp ) );
107                 msg += ( rev != null ? "; " : "" ) + ts;
108             }
109             msg += ")";
110         }
111         return msg;
112     }
113 
114     private static String reduce( String s )
115     {
116         return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
117     }
118 
119     static Properties getBuildProperties()
120     {
121         Properties properties = new Properties();
122 
123         try ( InputStream resourceAsStream = MavenCli.class.getResourceAsStream(
124             "/org/apache/maven/messages/build.properties" ) )
125         {
126 
127             if ( resourceAsStream != null )
128             {
129                 properties.load( resourceAsStream );
130             }
131         }
132         catch ( IOException e )
133         {
134             System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
135         }
136 
137         return properties;
138     }
139 
140     public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
141     {
142         if ( showStackTrace )
143         {
144             logger.error( message, e );
145         }
146         else
147         {
148             logger.error( message );
149 
150             if ( e != null )
151             {
152                 logger.error( e.getMessage() );
153 
154                 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
155                 {
156                     logger.error( "Caused by: {}", cause.getMessage() );
157                 }
158             }
159         }
160     }
161 
162     public static String formatTimestamp( long timestamp )
163     {
164         SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssXXX" );
165         return sdf.format( new Date( timestamp ) );
166     }
167 
168     public static String formatDuration( long duration )
169     {
170         // CHECKSTYLE_OFF: MagicNumber
171         long ms = duration % 1000;
172         long s = ( duration / ONE_SECOND ) % 60;
173         long m = ( duration / ONE_MINUTE ) % 60;
174         long h = ( duration / ONE_HOUR ) % 24;
175         long d = duration / ONE_DAY;
176         // CHECKSTYLE_ON: MagicNumber
177 
178         String format;
179         if ( d > 0 )
180         {
181             // Length 11+ chars
182             format = "%d d %02d:%02d h";
183         }
184         else if ( h > 0 )
185         {
186             // Length 7 chars
187             format = "%2$02d:%3$02d h";
188         }
189         else if ( m > 0 )
190         {
191             // Length 9 chars
192             format = "%3$02d:%4$02d min";
193         }
194         else
195         {
196             // Length 7-8 chars
197             format = "%4$d.%5$03d s";
198         }
199 
200         return String.format( format, d, h, m, s, ms );
201     }
202 
203 }