1 package org.apache.maven.plugins.resources;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * This class is duplicated from maven-model-builder from maven core. (See MRESOURCES-99).
30 */
31 public class MavenBuildTimestamp
32 {
33 /**
34 * ISO 8601-compliant timestamp for machine readability
35 */
36 public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
37
38 /**
39 * The property name.
40 */
41 public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format";
42
43 /**
44 * The default time zone {@code Etc/UTC}.
45 */
46 public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone( "Etc/UTC" );
47
48 private String formattedTimestamp;
49
50 /**
51 * Create an instance.
52 */
53 public MavenBuildTimestamp()
54 {
55 this( new Date() );
56 }
57
58 /**
59 * @param time The time to use.
60 */
61 public MavenBuildTimestamp( Date time )
62 {
63 this( time, DEFAULT_BUILD_TIMESTAMP_FORMAT );
64 }
65
66 /**
67 * @param time The time to use.
68 * @param properties the properties which can be define. can be {@code null}
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 * @param time The time to use.
77 * @param timestampFormat The format for {@link SimpleDateFormat}.
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 * @return The formatted time stamp.
108 */
109 public String formattedTimestamp()
110 {
111 return formattedTimestamp;
112 }
113 }