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 org.apache.maven.cli.MavenCli;
23  import org.apache.maven.shared.utils.StringUtils;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.text.SimpleDateFormat;
28  import java.util.Date;
29  import java.util.Properties;
30  
31  /**
32   * Helper to calculate the full Maven version string, as done in Maven Core
33   * in CLIReportingUtils.
34   * @see org.apache.maven.cli.CLIReportingUtils
35   */
36  
37  class MavenVersion
38  {
39      static String createMavenVersionString()
40      {
41          return createMavenVersionString( getBuildProperties() );
42      }
43  
44      /**
45       * Create a human readable string containing the Maven version, buildnumber, and time of build
46       *
47       * @param buildProperties The build properties
48       * @return Readable build info
49       */
50      private static String createMavenVersionString( Properties buildProperties )
51      {
52          String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
53          String version = reduce( buildProperties.getProperty( "version" ) );
54          String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
55          String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
56  
57          String msg = distributionName + " ";
58          msg += ( version != null ? version : "<version unknown>" );
59          if ( rev != null || timestamp != null )
60          {
61              msg += " (";
62              msg += ( rev != null ? rev : "" );
63              if ( StringUtils.isNotBlank( timestamp ) )
64              {
65                  String ts = formatTimestamp( Long.valueOf( timestamp ) );
66                  msg += ( rev != null ? "; " : "" ) + ts;
67              }
68              msg += ")";
69          }
70          return msg;
71      }
72  
73      private static String reduce( String s )
74      {
75          return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
76      }
77  
78      private static Properties getBuildProperties()
79      {
80          Properties properties = new Properties();
81  
82          try ( InputStream resourceAsStream = MavenCli.class.getResourceAsStream(
83                  "/org/apache/maven/messages/build.properties" ) )
84          {
85  
86              if ( resourceAsStream != null )
87              {
88                  properties.load( resourceAsStream );
89              }
90          }
91          catch ( IOException e )
92          {
93              System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
94          }
95  
96          return properties;
97      }
98  
99      private static String formatTimestamp( long timestamp )
100     {
101         SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssXXX" );
102         return sdf.format( new Date( timestamp ) );
103     }
104 }