From 2a4d172600a6252da662edb450e9513067d6ef9e Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Thu, 12 Jun 2014 14:09:19 -0400 Subject: [PATCH] MNG-5452: ${maven.build.timestamp} should use UTC instead of local timezone (or be configurable) --- .../java/org/apache/maven/DefaultMaven.java | 10 +-- .../AbstractStringBasedModelInterpolator.java | 42 ++++------- .../BuildTimestampValueSource.java | 26 ++----- .../interpolation/MavenBuildTimestamp.java | 69 +++++++++++++++++++ .../AbstractModelInterpolatorTest.java | 2 +- .../MavenBuildTimestampTest.java | 38 ++++++++++ 6 files changed, 128 insertions(+), 59 deletions(-) create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/interpolation/MavenBuildTimestamp.java create mode 100644 maven-model-builder/src/test/java/org/apache/maven/model/interpolation/MavenBuildTimestampTest.java diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index d8425b5d3a..06d61aa9e4 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -35,6 +35,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.TimeZone; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; @@ -54,6 +55,7 @@ import org.apache.maven.model.building.ModelProblem; import org.apache.maven.model.building.ModelProblemUtils; import org.apache.maven.model.building.ModelSource; import org.apache.maven.model.building.UrlModelSource; +import org.apache.maven.model.interpolation.MavenBuildTimestamp; import org.apache.maven.plugin.LegacySupport; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuilder; @@ -206,14 +208,8 @@ public class DefaultMaven // private MavenExecutionResult doExecute( MavenExecutionRequest request ) { - if ( request.getStartTime() != null ) - { - request.getSystemProperties().put( "${build.timestamp}", - new SimpleDateFormat( "yyyyMMdd-hhmm" ).format( request.getStartTime() ) ); - } - request.setStartTime( new Date() ); - + MavenExecutionResult result = new DefaultMavenExecutionResult(); try diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java index 863eb44ef9..3ad2348e37 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java @@ -19,10 +19,20 @@ package org.apache.maven.model.interpolation; * under the License. */ +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; + import org.apache.maven.model.Model; import org.apache.maven.model.building.ModelBuildingRequest; -import org.apache.maven.model.building.ModelProblemCollector; import org.apache.maven.model.building.ModelProblem.Severity; +import org.apache.maven.model.building.ModelProblem.Version; +import org.apache.maven.model.building.ModelProblemCollector; +import org.apache.maven.model.building.ModelProblemCollectorRequest; import org.apache.maven.model.path.PathTranslator; import org.apache.maven.model.path.UrlNormalizer; import org.codehaus.plexus.component.annotations.Requirement; @@ -38,16 +48,6 @@ import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper; import org.codehaus.plexus.interpolation.RecursionInterceptor; import org.codehaus.plexus.interpolation.ValueSource; -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import org.apache.maven.model.building.ModelProblem.Version; -import org.apache.maven.model.building.ModelProblemCollectorRequest; - /** * Use a regular expression search to find and resolve expressions within the POM. * @@ -56,18 +56,6 @@ import org.apache.maven.model.building.ModelProblemCollectorRequest; public abstract class AbstractStringBasedModelInterpolator implements ModelInterpolator { - - /** - * The default format used for build timestamps. - */ - static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyyMMdd-HHmm"; - - /** - * The name of a property that if present in the model's {@code } section specifies a custom format for - * build timestamps. See {@link java.text.SimpleDateFormat} for details on the format. - */ - private static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format"; - private static final List PROJECT_PREFIXES = Arrays.asList( "pom.", "project." ); private static final Collection TRANSLATED_PATH_EXPRESSIONS; @@ -168,13 +156,7 @@ public abstract class AbstractStringBasedModelInterpolator } }, PROJECT_PREFIXES, false ); valueSources.add( baseUriValueSource ); - - String timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT; - if ( modelProperties != null ) - { - timestampFormat = modelProperties.getProperty( BUILD_TIMESTAMP_FORMAT_PROPERTY, timestampFormat ); - } - valueSources.add( new BuildTimestampValueSource( config.getBuildStartTime(), timestampFormat ) ); + valueSources.add( new BuildTimestampValueSource( config.getBuildStartTime(), modelProperties ) ); } valueSources.add( modelValueSource1 ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java index 07787ebb62..4542a3fe5b 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java @@ -19,44 +19,28 @@ package org.apache.maven.model.interpolation; * under the License. */ -import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Properties; import org.codehaus.plexus.interpolation.AbstractValueSource; -/** - * - */ class BuildTimestampValueSource extends AbstractValueSource { - - private final Date startTime; - - private final String format; + private final MavenBuildTimestamp mavenBuildTimestamp; - private String formattedDate; - - public BuildTimestampValueSource( Date startTime, String format ) + public BuildTimestampValueSource( Date startTime, Properties properties ) { super( false ); - this.startTime = startTime; - this.format = format; + this.mavenBuildTimestamp = new MavenBuildTimestamp( startTime, properties ); } public Object getValue( String expression ) { if ( "build.timestamp".equals( expression ) || "maven.build.timestamp".equals( expression ) ) { - if ( formattedDate == null && startTime != null ) - { - formattedDate = new SimpleDateFormat( format ).format( startTime ); - } - - return formattedDate; + return mavenBuildTimestamp.formattedTimestamp(); } - return null; } - } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/MavenBuildTimestamp.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/MavenBuildTimestamp.java new file mode 100644 index 0000000000..d3425667b4 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/MavenBuildTimestamp.java @@ -0,0 +1,69 @@ +package org.apache.maven.model.interpolation; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; +import java.util.TimeZone; + +public class MavenBuildTimestamp +{ + public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyyMMdd-HHmm"; + + public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format"; + + private String formattedTimestamp; + + public MavenBuildTimestamp() + { + this( new Date() ); + } + + public MavenBuildTimestamp( Date time ) + { + this( time, DEFAULT_BUILD_TIMESTAMP_FORMAT ); + } + + public MavenBuildTimestamp( Date time, Properties properties ) + { + this( time, properties != null ? properties.getProperty( BUILD_TIMESTAMP_FORMAT_PROPERTY ) : null ); + } + + public MavenBuildTimestamp( Date time, String timestampFormat ) + { + if ( timestampFormat == null ) + { + timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT; + } + if ( time == null ) + { + time = new Date(); + } + SimpleDateFormat dateFormat = new SimpleDateFormat( timestampFormat ); + dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); + formattedTimestamp = dateFormat.format( time ); + } + + public String formattedTimestamp() + { + return formattedTimestamp; + } +} diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java index cb34ddf7ff..97c0715f50 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java @@ -108,7 +108,7 @@ public abstract class AbstractModelInterpolatorTest Date secondTestDate = cal.getTime(); SimpleDateFormat format = - new SimpleDateFormat( AbstractStringBasedModelInterpolator.DEFAULT_BUILD_TIMESTAMP_FORMAT ); + new SimpleDateFormat( MavenBuildTimestamp.DEFAULT_BUILD_TIMESTAMP_FORMAT ); assertEquals( "19761111-0016", format.format( firstTestDate ) ); assertEquals( "19761111-2316", format.format( secondTestDate ) ); } diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/MavenBuildTimestampTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/MavenBuildTimestampTest.java new file mode 100644 index 0000000000..83038b8a44 --- /dev/null +++ b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/MavenBuildTimestampTest.java @@ -0,0 +1,38 @@ +package org.apache.maven.model.interpolation; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Date; +import java.util.Properties; + +import junit.framework.TestCase; + +public class MavenBuildTimestampTest + extends TestCase +{ + public void testMavenBuildTimestampUsesUTC() + { + Properties interpolationProperties = new Properties(); + interpolationProperties.setProperty( "maven.build.timestamp.format", "yyyyMMdd-HHmm:z" ); + MavenBuildTimestamp timestamp = new MavenBuildTimestamp( new Date(), interpolationProperties ); + String formattedTimestamp = timestamp.formattedTimestamp(); + assertTrue( "We expect the UTC marker at the end of the timestamp.", formattedTimestamp.endsWith( "UTC" ) ); + } +}