001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.info;
018
019import java.time.DateTimeException;
020import java.time.Instant;
021import java.time.format.DateTimeFormatter;
022import java.util.Properties;
023
024/**
025 * Provide build-related information such as group and artifact.
026 *
027 * @author Stephane Nicoll
028 * @since 1.4.0
029 */
030public class BuildProperties extends InfoProperties {
031
032        /**
033         * Create an instance with the specified entries.
034         * @param entries the information to expose
035         */
036        public BuildProperties(Properties entries) {
037                super(processEntries(entries));
038        }
039
040        /**
041         * Return the groupId of the project or {@code null}.
042         * @return the group
043         */
044        public String getGroup() {
045                return get("group");
046        }
047
048        /**
049         * Return the artifactId of the project or {@code null}.
050         * @return the artifact
051         */
052        public String getArtifact() {
053                return get("artifact");
054        }
055
056        /**
057         * Return the name of the project or {@code null}.
058         * @return the name
059         */
060        public String getName() {
061                return get("name");
062        }
063
064        /**
065         * Return the version of the project or {@code null}.
066         * @return the version
067         */
068        public String getVersion() {
069                return get("version");
070        }
071
072        /**
073         * Return the timestamp of the build or {@code null}.
074         * <p>
075         * If the original value could not be parsed properly, it is still available with the
076         * {@code time} key.
077         * @return the build time
078         * @see #get(String)
079         */
080        public Instant getTime() {
081                return getInstant("time");
082        }
083
084        private static Properties processEntries(Properties properties) {
085                coerceDate(properties, "time");
086                return properties;
087        }
088
089        private static void coerceDate(Properties properties, String key) {
090                String value = properties.getProperty(key);
091                if (value != null) {
092                        try {
093                                String updatedValue = String.valueOf(DateTimeFormatter.ISO_INSTANT
094                                                .parse(value, Instant::from).toEpochMilli());
095                                properties.setProperty(key, updatedValue);
096                        }
097                        catch (DateTimeException ex) {
098                                // Ignore and store the original value
099                        }
100                }
101        }
102
103}