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.Properties;
9 import java.util.stream.Collectors;
11 import org.eclipse.core.runtime.IProduct;
12 import org.eclipse.core.runtime.Platform;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
17 * @author Jani Simomaa
20 public final class BuildInfo {
22 private static final Logger LOGGER = LoggerFactory.getLogger(BuildInfo.class);
23 private static final Properties PROPS;
25 public static final String DEFAULT_BUILDINFO_PROPERTIES_FILE = "buildinfo.properties";
26 public static final String BUILDINFO_PROPERTIES_PROP = "buildinfo.properties.location";
28 public static final String BUILD_INFO_PROPERTIES_FILE = System.getProperty(BUILDINFO_PROPERTIES_PROP,
29 DEFAULT_BUILDINFO_PROPERTIES_FILE);
32 if (LOGGER.isDebugEnabled())
33 LOGGER.debug("Trying to load build information from {}", BUILD_INFO_PROPERTIES_FILE);
34 Properties buildinfo = new Properties();
35 Path propertiesFile = Paths.get(BUILD_INFO_PROPERTIES_FILE);
36 if (Files.exists(propertiesFile)) {
38 buildinfo.load(Files.newInputStream(propertiesFile));
39 if (LOGGER.isDebugEnabled())
40 LOGGER.debug(buildinfo.toString());
41 } catch (IOException e) {
42 LOGGER.error("Could not load build information from {}", propertiesFile.toAbsolutePath(), e);
45 if (LOGGER.isDebugEnabled())
46 LOGGER.debug("Build information not loaded as {} does not exist", propertiesFile.toAbsolutePath());
48 // add information from .product if possible
49 collectInformationFromActiveProduct(buildinfo);
54 private static void collectInformationFromActiveProduct(Properties buildinfo) {
55 IProduct product = Platform.getProduct();
56 if (product != null) {
57 buildinfo.setProperty("product.name", product.getName());
58 buildinfo.setProperty("product.application", product.getApplication());
59 buildinfo.setProperty("product.description", product.getDescription());
60 buildinfo.setProperty("product.id", product.getId());
68 public static String get(String key) {
69 return PROPS.getProperty(key, "<not-set>");
75 public static String all() {
76 return PROPS.toString();
82 public static Map<String, String> asMap() {
83 return PROPS.entrySet().stream().collect(Collectors.toMap(k -> String.valueOf(k), v -> String.valueOf(v)));