1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
30
31 public class MavenBuildTimestamp {
32
33
34
35 public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
36
37
38
39
40 public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format";
41
42
43
44
45 public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
46
47 private String formattedTimestamp;
48
49
50
51
52 public MavenBuildTimestamp() {
53 this(new Date());
54 }
55
56
57
58
59 public MavenBuildTimestamp(Date time) {
60 this(time, DEFAULT_BUILD_TIMESTAMP_FORMAT);
61 }
62
63
64
65
66
67 public MavenBuildTimestamp(Date time, Properties properties) {
68 this(time, properties != null ? properties.getProperty(BUILD_TIMESTAMP_FORMAT_PROPERTY) : null);
69 }
70
71
72
73
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
97
98 public String formattedTimestamp() {
99 return formattedTimestamp;
100 }
101 }