All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.mojo.build.CreateTimestampMojo Maven / Gradle / Ivy

package org.codehaus.mojo.build;

/*
 * 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.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

/**
 * This mojo is designed to give you a timestamp available through one or more properties. Only a single timestamp is
 * created for each execution of the mojo. This timestamp can be format into one or more strings which are then saved to
 * properties.
 * 
 * @author pgier
 * @version $Id: CreateTimestampMojo.java 19569 2014-03-21 21:27:32Z baptiste $
 * @since 1.0-beta-5
 * @description create a timestamp property
 */
@Mojo( name = "create-timestamp", defaultPhase = LifecyclePhase.INITIALIZE, requiresProject = true, threadSafe = true )
public class CreateTimestampMojo
    extends AbstractMojo
{
    /**
     * Whether to skip this execution.
     * 
     * @since 1.3
     */
    @Parameter( property = "maven.buildNumber.skip", defaultValue = "false" )
    private boolean skip;

    /**
     * The maven project.
     */
    @Parameter( defaultValue = "${project}", required = true, readonly = true )
    private MavenProject project;

    /**
     * Contains the full list of projects in the reactor.
     * 
     */
    @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
    private List reactorProjects;

    /**
     * You can rename the timestamp property name to another property name if desired.
     */
    @Parameter( property = "maven.buildNumber.timestampPropertyName", defaultValue = "timestamp" )
    private String timestampPropertyName;

    /**
     * Apply this java.text.SimpleDateFormat to the timestamp. By default, no formatting is done but the raw number
     * value (milliseconds since January 1, 1970, 00:00:00 GMT) is used.
     */
    @Parameter( property = "maven.buildNumber.timestampFormat", defaultValue = "" )
    private String timestampFormat;

    public void execute()
    {
        if ( skip )
        {
            getLog().info( "Skipping execution." );
            return;
        }

        String timestampString = project.getProperties().getProperty( timestampPropertyName );

        // Check if the plugin has already run in the current build.
        if ( timestampString != null )
        {
            getLog().debug( "Using previously created timestamp." );
            return;
        }

        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();

        if ( timestampFormat == null || timestampFormat.equals( "" ) )
        {
            timestampString = String.valueOf( now.getTime() );
        }
        else
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat( timestampFormat );
            timestampString = dateFormat.format( now );
        }

        getLog().debug( "Storing timestamp property: " + timestampPropertyName + " " + timestampString );

        Iterator projIter = reactorProjects.iterator();
        while ( projIter.hasNext() )
        {
            MavenProject nextProj = (MavenProject) projIter.next();
            nextProj.getProperties().setProperty( this.timestampPropertyName, timestampString );
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy