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