1 package org.apache.maven.plugins.resources;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
36 public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
37
38
39
40
41 public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format";
42
43
44
45
46 public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone( "Etc/UTC" );
47
48 private String formattedTimestamp;
49
50
51
52
53 public MavenBuildTimestamp()
54 {
55 this( new Date() );
56 }
57
58
59
60
61 public MavenBuildTimestamp( Date time )
62 {
63 this( time, DEFAULT_BUILD_TIMESTAMP_FORMAT );
64 }
65
66
67
68
69
70 public MavenBuildTimestamp( Date time, Properties properties )
71 {
72 this( time, properties != null ? properties.getProperty( BUILD_TIMESTAMP_FORMAT_PROPERTY ) : null );
73 }
74
75
76
77
78
79 public MavenBuildTimestamp( Date time, String timestampFormat )
80 {
81 SimpleDateFormat dateFormat;
82
83 if ( timestampFormat == null )
84 {
85 dateFormat = new SimpleDateFormat( DEFAULT_BUILD_TIMESTAMP_FORMAT );
86 }
87 else
88 {
89 dateFormat = new SimpleDateFormat( timestampFormat );
90 }
91
92 dateFormat.setCalendar( new GregorianCalendar() );
93 dateFormat.setTimeZone( DEFAULT_BUILD_TIME_ZONE );
94
95 if ( time == null )
96 {
97 formattedTimestamp = dateFormat.format( new Date() );
98 }
99 else
100 {
101 formattedTimestamp = dateFormat.format( time );
102 }
103
104 }
105
106
107
108
109 public String formattedTimestamp()
110 {
111 return formattedTimestamp;
112 }
113 }