001    package org.apache.maven.cli;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.text.SimpleDateFormat;
025    import java.util.Date;
026    import java.util.Locale;
027    import java.util.Properties;
028    
029    import org.codehaus.plexus.util.IOUtil;
030    import org.codehaus.plexus.util.Os;
031    import org.slf4j.Logger;
032    
033    /**
034     * Utility class used to report errors, statistics, application version info, etc.
035     *
036     * @author jdcasey
037     *
038     */
039    public final class CLIReportingUtils
040    {
041    
042        public static final long MB = 1024 * 1024;
043    
044        public static final int MS_PER_SEC = 1000;
045    
046        public static final int SEC_PER_MIN = 60;
047        
048        public static final String BUILD_VERSION_PROPERTY = "version";
049    
050        public static String showVersion()
051        {
052            final String LS = System.getProperty( "line.separator" );
053            Properties properties = getBuildProperties();
054            StringBuffer version = new StringBuffer();
055            version.append( createMavenVersionString( properties ) ).append( LS );
056            version.append( reduce( properties.getProperty( "distributionShortName" ) + " home: "
057                                + System.getProperty( "maven.home", "<unknown maven home>" ) ) ).append( LS );
058            version.append( "Java version: " + System.getProperty( "java.version", "<unknown java version>" )
059                                + ", vendor: " + System.getProperty( "java.vendor", "<unknown vendor>" ) ).append( LS );
060            version.append( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) ).append( LS );
061            version.append( "Default locale: " + Locale.getDefault() + ", platform encoding: "
062                                + System.getProperty( "file.encoding", "<unknown encoding>" ) ).append( LS );
063            version.append( "OS name: \"" + Os.OS_NAME + "\", version: \"" + Os.OS_VERSION + "\", arch: \"" + Os.OS_ARCH
064                + "\", family: \"" + Os.OS_FAMILY + "\"" );
065            return version.toString();
066        }
067    
068        /**
069         * Create a human readable string containing the Maven version, buildnumber, and time of build
070         * 
071         * @param buildProperties The build properties
072         * @return Readable build info
073         */
074        static String createMavenVersionString( Properties buildProperties )
075        {
076            String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
077            String version = reduce( buildProperties.getProperty( BUILD_VERSION_PROPERTY ) );
078            String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
079            String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
080    
081            String msg = distributionName + " ";
082            msg += ( version != null ? version : "<version unknown>" );
083            if ( rev != null || timestamp != null )
084            {
085                msg += " (";
086                msg += ( rev != null ? rev : "" );
087                if ( timestamp != null )
088                {
089                    SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" );
090                    String ts = fmt.format( new Date( Long.valueOf( timestamp ) ) );
091                    msg += ( rev != null ? "; " : "" ) + ts;
092                }
093                msg += ")";
094            }
095            return msg;
096        }
097    
098        private static String reduce( String s )
099        {
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    }