View Javadoc

1   package org.codehaus.mojo.build;
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.Calendar;
24  import java.util.Date;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.project.MavenProject;
30  
31  /**
32   * This mojo is designed to give you a timestamp available through one or more properties.
33   * Only a single timestamp is created for each execution of the mojo.  This timestamp can
34   * be format into one or more strings which are then saved to properties. 
35   * 
36   * @author pgier
37   * @version $Id: CreateTimestampMojo.java 14068 2011-05-19 13:31:41Z afloom $
38   * @goal create-timestamp
39   * @phase initialize
40   * @since 1.0-beta-5
41   * @requiresProject
42   * @description create a timestamp property
43   */
44  public class CreateTimestampMojo
45      extends AbstractMojo
46  {
47  
48      /**
49       * The maven project.
50       * 
51       * @parameter expression="${project}"
52       * @readonly
53       */
54      private MavenProject project;
55  
56      /**
57       * Contains the full list of projects in the reactor.
58       *
59       * @parameter expression="${reactorProjects}"
60       * @readonly
61       */
62      private List reactorProjects;
63  
64      /**
65       * You can rename the timestamp property name to another property name if desired.
66       * 
67       * @parameter expression="${maven.buildNumber.timestampPropertyName}" default-value="timestamp"
68       */
69      private String timestampPropertyName;
70      
71      /**
72       * Apply this java.text.SimpleDateFormat to the timestamp. By default, no formatting is done
73       * but the raw number value (milliseconds since January 1, 1970, 00:00:00 GMT) is used.
74       * 
75       * @parameter expression="${maven.buildNumber.timestampFormat}" default-value=""
76       */
77      private String timestampFormat;
78  
79      public void execute()
80      {
81          String timestampString = project.getProperties().getProperty( timestampPropertyName );
82          
83          // Check if the plugin has already run in the current build.
84          if ( timestampString != null )
85          {
86              getLog().debug( "Using previously created timestamp." );
87              return;
88          }
89          
90          Calendar cal = Calendar.getInstance();
91          Date now = cal.getTime();
92          
93          if ( timestampFormat == null || timestampFormat.equals( "" ))
94          {
95              timestampString = String.valueOf( now.getTime() );
96          }
97          else
98          {
99              SimpleDateFormat dateFormat = new SimpleDateFormat( timestampFormat );
100             timestampString = dateFormat.format( now );            
101         }
102         
103         getLog().debug( "Storing timestamp property: " + timestampPropertyName + " " + timestampString );
104         
105         Iterator projIter = reactorProjects.iterator();
106         while ( projIter.hasNext() )
107         {
108             MavenProject nextProj = (MavenProject) projIter.next();
109             nextProj.getProperties().setProperty( this.timestampPropertyName, timestampString );
110         }
111         
112     }
113      
114 }