3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
8 import java.util.Map.Entry;
9 import java.util.Properties;
10 import java.util.TreeMap;
11 import java.util.stream.Collectors;
13 import org.eclipse.core.runtime.IProduct;
14 import org.eclipse.core.runtime.Platform;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * @author Jani Simomaa
22 public final class BuildInfo {
24 private static final Logger LOGGER = LoggerFactory.getLogger(BuildInfo.class);
25 private static final Properties PROPS;
26 private static final String ALL;
28 public static final String DEFAULT_BUILDINFO_PROPERTIES_FILE = "buildinfo.properties";
29 public static final String BUILDINFO_PROPERTIES_PROP = "buildinfo.properties.location";
31 public static final String BUILD_INFO_PROPERTIES_FILE = System.getProperty(BUILDINFO_PROPERTIES_PROP,
32 DEFAULT_BUILDINFO_PROPERTIES_FILE);
35 if (LOGGER.isDebugEnabled())
36 LOGGER.debug("Trying to load build information from {}", BUILD_INFO_PROPERTIES_FILE);
37 Properties buildinfo = new Properties();
38 Path propertiesFile = Paths.get(BUILD_INFO_PROPERTIES_FILE);
39 if (Files.exists(propertiesFile)) {
41 buildinfo.load(Files.newInputStream(propertiesFile));
42 if (LOGGER.isDebugEnabled())
43 LOGGER.debug(buildinfo.toString());
44 } catch (IOException e) {
45 LOGGER.error("Could not load build information from {}", propertiesFile.toAbsolutePath(), e);
48 if (LOGGER.isDebugEnabled())
49 LOGGER.debug("Build information not loaded as {} does not exist", propertiesFile.toAbsolutePath());
51 // add information from .product if possible
52 collectInformationFromActiveProduct(buildinfo);
55 StringBuilder sb = new StringBuilder();
56 TreeMap<String, String> orderedProps = new TreeMap<>((o1, o2) -> o1.compareTo(o2));
57 for (Entry<Object, Object> entry : PROPS.entrySet())
58 orderedProps.put(entry.getKey().toString(), entry.getValue().toString());
59 for (Entry<String, String> entry : orderedProps.entrySet()) {
60 sb.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
65 private static void collectInformationFromActiveProduct(Properties buildinfo) {
66 IProduct product = Platform.getProduct();
67 if (product != null) {
68 String productName = product.getName() != null ? product.getName() : "<not-set>";
69 String productApplication = product.getApplication() != null ? product.getApplication() : "<not-set>";
70 String productDescription = product.getDescription() != null ? product.getDescription() : "<not-set>";
71 String productId = product.getId() != null ? product.getId() : "<not-set>";
73 buildinfo.setProperty("product.name", productName);
74 buildinfo.setProperty("product.application", productApplication);
75 buildinfo.setProperty("product.description", productDescription);
76 buildinfo.setProperty("product.id", productId);
84 public static String get(String key) {
85 return PROPS.getProperty(key, "<not-set>");
91 public static String all() {
98 public static Map<String, String> asMap() {
99 return PROPS.entrySet().stream().collect(Collectors.toMap(k -> String.valueOf(k), v -> String.valueOf(v)));