Skip to content

Commit f7b1671

Browse files
author
TheSnoozer
committed
Merge pull request #210 from TheSnoozer/master
Fix for #204: added the ability to set the timezone along with dateFormat
2 parents dacf4a0 + 541a98e commit f7b1671

File tree

7 files changed

+94
-2
lines changed

7 files changed

+94
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ It's really simple to setup this plugin; below is a sample pom that you may base
132132
<!-- that's the default value -->
133133
<dateFormat>dd.MM.yyyy '@' HH:mm:ss z</dateFormat>
134134

135+
<!-- @since 2.2.0 -->
136+
<!--
137+
If you want to set the timezone of the dateformat to anything in particular you can do this by using this option.
138+
As a general warning try to avoid three-letter time zone IDs because the same abbreviation are often used for multiple time zones.
139+
The default value we'll use the timezone use the timezone that's shipped with java (java.util.TimeZone.getDefault().getID()).
140+
*Note*: If you plan to set the java's timezone by using `MAVEN_OPTS=-Duser.timezone=UTC mvn clean package`, `mvn clean package -Duser.timezone=UTC` or any other configuration keep in mind that this option will override those settings and will not take other configurations into account!
141+
-->
142+
<dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
143+
135144
<!-- false is default here, it prints some more information during the build -->
136145
<verbose>false</verbose>
137146

@@ -631,6 +640,7 @@ Optional parameters:
631640
* **dotGitDirectory** - `(default: ${project.basedir}/.git)` the location of your .git folder. `${project.basedir}/.git` is the default value and will most probably be ok for single module projects, in other cases please use `../` to get higher up in the dir tree. An example would be: `${project.basedir}/../.git` which I'm currently using in my projects :-)
632641
* **prefix** - `(default: git)` is the "namespace" for all exposed properties
633642
* **dateFormat** - `(default: dd.MM.yyyy '@' HH:mm:ss z)` is a normal SimpleDateFormat String and will be used to represent git.build.time and git.commit.time
643+
* **dateFormatTimeZone** - `(default: empty)` *(available since v2.2.0)* is a TimeZone String (e.g. 'America/Los_Angeles', 'GMT+10', 'PST') and can be used to set the timezone of the *dateFormat* to anything in particular. As a general warning try to avoid three-letter time zone IDs because the same abbreviation are often used for multiple time zones. The default value we'll use the timezone use the timezone that's shipped with java (java.util.TimeZone.getDefault().getID()). *Note*: If you plan to set the java's timezone by using `MAVEN_OPTS=-Duser.timezone=UTC mvn clean package`, `mvn clean package -Duser.timezone=UTC` or any other configuration keep in mind that this option will override those settings and will not take other configurations into account!
634644
* **verbose** - `(default: false)` if true the plugin will print a summary of all collected properties when it's done
635645
* **generateGitPropertiesFile** -`(default: false)` this is false by default, forces the plugin to generate the git.properties file
636646
* **generateGitPropertiesFilename** - `(default: ${project.build.outputDirectory}/git.properties)` - The path for the to be generated properties file. The path can be relative to ${project.basedir} (e.g. target/classes/git.properties) or can be a full path (e.g. ${project.build.outputDirectory}/git.properties).

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315
<generateGitPropertiesFile>true</generateGitPropertiesFile>
316316
<generateGitPropertiesFilename>target/testing.properties</generateGitPropertiesFilename>
317317
<dateFormat>dd.MM.yyyy '@' HH:mm:ss z</dateFormat>
318+
<dateFormatTimeZone>GMT-08:00</dateFormatTimeZone>
318319
<useNativeGit>false</useNativeGit>
319320
<abbrevLength>7</abbrevLength>
320321
<format>properties</format>

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.List;
5050
import java.util.Map;
5151
import java.util.Properties;
52+
import java.util.TimeZone;
5253

5354
/**
5455
* Goal which puts git build-time information into property files or maven's properties.
@@ -224,6 +225,20 @@ public class GitCommitIdMojo extends AbstractMojo {
224225
@SuppressWarnings("UnusedDeclaration")
225226
private String dateFormat;
226227

228+
/**
229+
* The timezone used in the date format that's used for any dates exported by this plugin.
230+
* It should be a valid Timezone string (e.g. 'America/Los_Angeles', 'GMT+10', 'PST').
231+
* As a general warning try to avoid three-letter time zone IDs because the same abbreviation are often used for multiple time zones.
232+
* Please review https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html for more information on this issue.
233+
* will use the timezone that's shipped with java as a default (java.util.TimeZone.getDefault().getID())
234+
*
235+
* @parameter
236+
*/
237+
@SuppressWarnings("UnusedDeclaration")
238+
private String dateFormatTimeZone;
239+
240+
241+
227242
/**
228243
* Specifies whether the plugin should fail if it can't find the .git directory. The default
229244
* value is true.
@@ -516,6 +531,9 @@ private boolean isOurProperty(@NotNull String keyString) {
516531
void loadBuildVersionAndTimeData(@NotNull Properties properties) {
517532
Date buildDate = new Date();
518533
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
534+
if(dateFormatTimeZone != null){
535+
smf.setTimeZone(TimeZone.getTimeZone(dateFormatTimeZone));
536+
}
519537
put(properties, BUILD_TIME, smf.format(buildDate));
520538
put(properties, BUILD_VERSION, project.getVersion());
521539
}
@@ -568,6 +586,7 @@ void loadGitDataWithNativeGit(@NotNull Properties properties) throws IOException
568586
.setPrefixDot(prefixDot)
569587
.setAbbrevLength(abbrevLength)
570588
.setDateFormat(dateFormat)
589+
.setDateFormatTimeZone(dateFormatTimeZone)
571590
.setGitDescribe(gitDescribe);
572591

573592
nativeGitProvider.loadGitData(properties);
@@ -580,6 +599,7 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
580599
.setPrefixDot(prefixDot)
581600
.setAbbrevLength(abbrevLength)
582601
.setDateFormat(dateFormat)
602+
.setDateFormatTimeZone(dateFormatTimeZone)
583603
.setGitDescribe(gitDescribe);
584604

585605
jGitProvider.loadGitData(properties);

src/main/java/pl/project13/maven/git/GitDataProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.io.IOException;
2626
import java.util.Map;
2727
import java.util.Properties;
28+
import java.util.TimeZone;
29+
import java.text.SimpleDateFormat;
2830

2931
import static com.google.common.base.Strings.isNullOrEmpty;
3032

@@ -41,6 +43,8 @@ public abstract class GitDataProvider {
4143

4244
protected String dateFormat;
4345

46+
protected String dateFormatTimeZone;
47+
4448
protected GitDescribeConfig gitDescribe = new GitDescribeConfig();
4549

4650
public GitDataProvider(@NotNull LoggerBridge loggerBridge) {
@@ -67,6 +71,11 @@ public GitDataProvider setDateFormat(String dateFormat) {
6771
return this;
6872
}
6973

74+
public GitDataProvider setDateFormatTimeZone(String dateFormatTimeZone){
75+
this.dateFormatTimeZone = dateFormatTimeZone;
76+
return this;
77+
}
78+
7079
protected abstract void init() throws MojoExecutionException;
7180
protected abstract String getBuildAuthorName();
7281
protected abstract String getBuildAuthorEmail();
@@ -190,6 +199,14 @@ protected String determineBranchNameOnBuildServer(Map<String, String> env) throw
190199
}
191200
}
192201

202+
protected SimpleDateFormat getSimpleDateFormatWithTimeZone(){
203+
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
204+
if(dateFormatTimeZone != null){
205+
smf.setTimeZone(TimeZone.getTimeZone(dateFormatTimeZone));
206+
}
207+
return smf;
208+
}
209+
193210
void log(String... parts) {
194211
loggerBridge.log((Object[]) parts);
195212
}

src/main/java/pl/project13/maven/git/JGitProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected String getCommitMessageShort() {
176176
protected String getCommitTime() {
177177
long timeSinceEpoch = headCommit.getCommitTime();
178178
Date commitDate = new Date(timeSinceEpoch * 1000); // git is "by sec" and java is "by ms"
179-
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
179+
SimpleDateFormat smf = getSimpleDateFormatWithTimeZone();
180180
return smf.format(commitDate);
181181
}
182182

src/main/java/pl/project13/maven/git/NativeGitProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected String getCommitMessageShort() {
206206
@Override
207207
protected String getCommitTime() {
208208
String value = runQuietGitCommand(canonical, "log -1 --pretty=format:%ct");
209-
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
209+
SimpleDateFormat smf = getSimpleDateFormatWithTimeZone();
210210
return smf.format(Long.parseLong(value)*1000L);
211211
}
212212

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,50 @@ public void shouldGenerateClosestTagInformationWhenCommitHasTwoTags(boolean useN
748748
assertThat(targetProject.getProperties().getProperty("git.closest.tag.commit.count")).isEqualTo("0");
749749
}
750750

751+
@Test
752+
@Parameters(method = "useNativeGit")
753+
public void shouldUseDateFormatTimeZone(boolean useNativeGit) throws Exception {
754+
// given
755+
mavenSandbox.withParentProject("my-pom-project", "pom")
756+
.withChildProject("my-jar-module", "jar")
757+
.withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG_DIRTY)
758+
.create(CleanUp.CLEANUP_FIRST);
759+
MavenProject targetProject = mavenSandbox.getChildProject();
760+
761+
setProjectToExecuteMojoIn(targetProject);
762+
763+
// RFC 822 time zone: Sign TwoDigitHours Minutes
764+
String dateFormat = "Z"; // we want only the timezone (formated in RFC 822) out of the dateformat (easier for asserts)
765+
String expectedTimeZoneOffset = "+0200";
766+
String executionTimeZoneOffset = "-0800";
767+
TimeZone expectedTimeZone = TimeZone.getTimeZone("GMT" + expectedTimeZoneOffset);
768+
TimeZone executionTimeZone = TimeZone.getTimeZone("GMT" + executionTimeZoneOffset);
769+
770+
GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(true, 7);
771+
alterMojoSettings("gitDescribe", gitDescribeConfig);
772+
alterMojoSettings("useNativeGit", useNativeGit);
773+
alterMojoSettings("dateFormat", dateFormat);
774+
alterMojoSettings("dateFormatTimeZone", expectedTimeZone.getID());
775+
776+
// override the default timezone for execution and testing
777+
TimeZone currentDefaultTimeZone = TimeZone.getDefault();
778+
TimeZone.setDefault(executionTimeZone);
779+
780+
// when
781+
mojo.execute();
782+
783+
// then
784+
Properties properties = targetProject.getProperties();
785+
assertThat(properties.stringPropertyNames()).contains("git.commit.time");
786+
assertThat(properties.getProperty("git.commit.time")).isEqualTo(expectedTimeZoneOffset);
787+
788+
assertThat(properties.stringPropertyNames()).contains("git.build.time");
789+
assertThat(properties.getProperty("git.build.time")).isEqualTo(expectedTimeZoneOffset);
790+
791+
// set the timezone back
792+
TimeZone.setDefault(currentDefaultTimeZone);
793+
}
794+
751795
private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) {
752796
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
753797
gitDescribeConfig.setTags(true);

0 commit comments

Comments
 (0)