1 package org.apache.maven.plugins.artifact.buildinfo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36
37 class MavenVersion
38 {
39 static String createMavenVersionString()
40 {
41 return createMavenVersionString( getBuildProperties() );
42 }
43
44
45
46
47
48
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 }