View Javadoc

1   /*
2    * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.plugin.buildmetadata;
17  
18  import java.util.Date;
19  import java.util.Properties;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.MojoFailureException;
23  import org.codehaus.plexus.util.StringUtils;
24  
25  import de.smartics.maven.plugin.buildmetadata.common.Constant;
26  import de.smartics.maven.plugin.buildmetadata.io.BuildPropertiesFileHelper;
27  
28  /**
29   * Adds the build time to the properties file and runs all providers flagged
30   * with
31   * {@link de.smartics.maven.plugin.buildmetadata.data.Provider#RUN_AT_BUILD_POINT}
32   * .
33   *
34   * @goal build-point
35   * @phase prepare-package
36   * @requiresProject
37   * @threadSafe
38   * @since 1.0
39   * @description Provides the duration of the build at the given point to the
40   *              build properties and runs all providers flagged with
41   *              'runAtBuildPoint' with a value of 'true'. The mojo is run at the
42   *              specified point. Note that dependent on the phase, the build
43   *              information (such as the duration of the build) may or may not
44   *              packaged with the artifacts.
45   */
46  public final class BuildPointMojo extends AbstractBuildMojo
47  {
48    // ********************************* Fields *********************************
49  
50    // --- constants ------------------------------------------------------------
51  
52    /**
53     * Constant to store the current time stamp. Must be removed before the build
54     * properties are written to the project.
55     * <p>
56     * The value of this constant is {@value}.
57     * </p>
58     */
59    public static final String TMP_BUILD_END = "build.tmp.timestamp";
60  
61    // --- members --------------------------------------------------------------
62  
63    /**
64     * The name of the build point to append to the duration property name. If
65     * blank, the duration property will be stored as <code>build.duration</code>.
66     * <p>
67     * This way build durations may be taken from different phases of the build.
68     * </p>
69     *
70     * @parameter
71     * @since 1.0
72     */
73    private String name;
74  
75    // ****************************** Initializer *******************************
76  
77    // ****************************** Constructors ******************************
78  
79    // ****************************** Inner Classes *****************************
80  
81    // ********************************* Methods ********************************
82  
83    // --- init -----------------------------------------------------------------
84  
85    // --- get&set --------------------------------------------------------------
86  
87    // --- business -------------------------------------------------------------
88  
89    /**
90     * {@inheritDoc}
91     */
92    public void execute() throws MojoExecutionException, MojoFailureException
93    {
94      super.execute();
95  
96      if (propertiesOutputFile.exists())
97      {
98        final Properties buildMetaDataProperties = new Properties();
99        final BuildPropertiesFileHelper helper =
100           new BuildPropertiesFileHelper(getLog(), propertiesOutputFile);
101       helper.readBuildPropertiesFile(buildMetaDataProperties);
102 
103       provideBuildPointInfo(buildMetaDataProperties, helper);
104       provideBuildMetaData(buildMetaDataProperties, null, providers, true);
105 
106       helper.writePropertiesFile(buildMetaDataProperties);
107       updateMavenEnvironment(buildMetaDataProperties, helper);
108     }
109     else
110     {
111       getLog()
112           .info(
113               "Skipping build point '" + name + "' since no "
114                   + propertiesOutputFile.getName()
115                   + " with build meta data found.");
116     }
117   }
118 
119   private void provideBuildPointInfo(final Properties buildMetaDataProperties,
120       final BuildPropertiesFileHelper helper)
121   {
122     final Date start = session.getStartTime();
123     final Date end = new Date();
124     final long duration = end.getTime() - start.getTime();
125     final String durationPropertyName = createDurationPropertyName();
126     final String durationString = String.valueOf(duration);
127     buildMetaDataProperties.setProperty(durationPropertyName, durationString);
128     setTimeDifference(helper, buildMetaDataProperties, end, durationString,
129         durationPropertyName);
130   }
131 
132   private void setTimeDifference(final BuildPropertiesFileHelper helper, // NOPMD
133       final Properties buildMetaDataProperties, final Date currentEnd,
134       final String durationString, final String durationPropertyName)
135   {
136     final Properties projectProperties = helper.getProjectProperties(project);
137     final String previousDurationEnd =
138         projectProperties.getProperty(TMP_BUILD_END);
139     final String diffPropertyName = durationPropertyName + ".diff";
140     if (StringUtils.isNotBlank(previousDurationEnd))
141     {
142       final long previousTimestamp = Long.parseLong(previousDurationEnd);
143       final long difference = currentEnd.getTime() - previousTimestamp;
144       buildMetaDataProperties.setProperty(diffPropertyName,
145           String.valueOf(difference));
146     }
147     else
148     {
149       buildMetaDataProperties.setProperty(diffPropertyName, durationString);
150     }
151 
152     projectProperties.setProperty(TMP_BUILD_END,
153         String.valueOf(currentEnd.getTime()));
154   }
155 
156   private String createDurationPropertyName()
157   {
158     if (StringUtils.isNotBlank(name))
159     {
160       return Constant.PROP_NAME_BUILD_DURATION + '.' + name;
161     }
162     return Constant.PROP_NAME_BUILD_DURATION;
163   }
164 
165   // --- object basics --------------------------------------------------------
166 
167 }