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