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