1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.polago.maven.plugins.mergeproperties;
21
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.Properties;
26 import java.util.TimeZone;
27
28 /**
29 * This class is duplicated from maven-model-builder from maven core. (See MRESOURCES-99).
30 */
31 public class MavenBuildTimestamp {
32 /**
33 * ISO 8601-compliant timestamp for machine readability.
34 */
35 public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
36
37 /**
38 * The property name.
39 */
40 public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format";
41
42 /**
43 * The default time zone {@code Etc/UTC}.
44 */
45 public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
46
47 private String formattedTimestamp;
48
49 /**
50 * Create an instance.
51 */
52 public MavenBuildTimestamp() {
53 this(new Date());
54 }
55
56 /**
57 * @param time The time to use.
58 */
59 public MavenBuildTimestamp(Date time) {
60 this(time, DEFAULT_BUILD_TIMESTAMP_FORMAT);
61 }
62
63 /**
64 * @param time The time to use.
65 * @param properties the properties which can be define. can be {@code null}
66 */
67 public MavenBuildTimestamp(Date time, Properties properties) {
68 this(time, properties != null ? properties.getProperty(BUILD_TIMESTAMP_FORMAT_PROPERTY) : null);
69 }
70
71 /**
72 * @param time The time to use.
73 * @param timestampFormat The format for {@link SimpleDateFormat}.
74 */
75 public MavenBuildTimestamp(Date time, String timestampFormat) {
76 SimpleDateFormat dateFormat;
77
78 if (timestampFormat == null) {
79 dateFormat = new SimpleDateFormat(DEFAULT_BUILD_TIMESTAMP_FORMAT);
80 } else {
81 dateFormat = new SimpleDateFormat(timestampFormat);
82 }
83
84 dateFormat.setCalendar(new GregorianCalendar());
85 dateFormat.setTimeZone(DEFAULT_BUILD_TIME_ZONE);
86
87 if (time == null) {
88 formattedTimestamp = dateFormat.format(new Date());
89 } else {
90 formattedTimestamp = dateFormat.format(time);
91 }
92
93 }
94
95 /**
96 * @return The formatted time stamp.
97 */
98 public String formattedTimestamp() {
99 return formattedTimestamp;
100 }
101 }