1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.resources;
20
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.GregorianCalendar;
24 import java.util.Properties;
25 import java.util.TimeZone;
26
27
28
29
30 public class MavenBuildTimestamp {
31
32
33
34 public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
35
36
37
38
39 public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format";
40
41
42
43
44 public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
45
46 private String formattedTimestamp;
47
48
49
50
51 public MavenBuildTimestamp() {
52 this(new Date());
53 }
54
55
56
57
58 public MavenBuildTimestamp(Date time) {
59 this(time, DEFAULT_BUILD_TIMESTAMP_FORMAT);
60 }
61
62
63
64
65
66 public MavenBuildTimestamp(Date time, Properties properties) {
67 this(time, properties != null ? properties.getProperty(BUILD_TIMESTAMP_FORMAT_PROPERTY) : null);
68 }
69
70
71
72
73
74 public MavenBuildTimestamp(Date time, String timestampFormat) {
75 SimpleDateFormat dateFormat;
76
77 if (timestampFormat == null) {
78 dateFormat = new SimpleDateFormat(DEFAULT_BUILD_TIMESTAMP_FORMAT);
79 } else {
80 dateFormat = new SimpleDateFormat(timestampFormat);
81 }
82
83 dateFormat.setCalendar(new GregorianCalendar());
84 dateFormat.setTimeZone(DEFAULT_BUILD_TIME_ZONE);
85
86 if (time == null) {
87 formattedTimestamp = dateFormat.format(new Date());
88 } else {
89 formattedTimestamp = dateFormat.format(time);
90 }
91 }
92
93
94
95
96 public String formattedTimestamp() {
97 return formattedTimestamp;
98 }
99 }