MNG-5452: ${maven.build.timestamp} should use UTC instead of local timezone (or be configurable)

This commit is contained in:
Jason van Zyl 2014-06-12 14:09:19 -04:00
parent f1dcec88ae
commit 2a4d172600
6 changed files with 128 additions and 59 deletions

View File

@ -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

View File

@ -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 <properties>} 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<String> PROJECT_PREFIXES = Arrays.asList( "pom.", "project." );
private static final Collection<String> 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 );

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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 ) );
}

View File

@ -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" ) );
}
}