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.io.File;
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.apache.maven.execution.MavenSession;
23  import org.apache.maven.execution.RuntimeInformation;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.project.MavenProject;
28  
29  import de.smartics.maven.plugin.buildmetadata.common.Property;
30  import de.smartics.maven.plugin.buildmetadata.common.ScmInfo;
31  import de.smartics.maven.plugin.buildmetadata.data.MetaDataProvider;
32  import de.smartics.maven.plugin.buildmetadata.data.MetaDataProviderBuilder;
33  import de.smartics.maven.plugin.buildmetadata.data.Provider;
34  import de.smartics.maven.plugin.buildmetadata.io.BuildPropertiesFileHelper;
35  
36  /**
37   * Base implementation for all build mojos.
38   *
39   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
40   * @version $Revision: 9143 $
41   */
42  public abstract class AbstractBuildMojo extends AbstractMojo
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    // --- members --------------------------------------------------------------
49  
50    // ... Mojo infrastructure ..................................................
51  
52    /**
53     * The Maven project.
54     *
55     * @parameter expression="${project}"
56     * @required
57     * @readonly
58     * @since 1.0
59     */
60    protected MavenProject project;
61  
62    /**
63     * The Maven session instance.
64     *
65     * @parameter expression="${session}"
66     * @required
67     * @readonly
68     */
69    protected MavenSession session;
70  
71    /**
72     * The runtime information of the Maven instance being executed for the build.
73     *
74     * @component
75     * @since 1.0
76     */
77    protected RuntimeInformation runtime;
78  
79    /**
80     * The name of the properties file to write. Per default this value is
81     * overridden by packaging dependent locations. Please refer to <a
82     * href="#activatePropertyOutputFileMapping"
83     * >activatePropertyOutputFileMapping</a> for details.
84     *
85     * @parameter default-value=
86     *            "${project.build.outputDirectory}/META-INF/build.properties"
87     * @since 1.0
88     */
89    protected File propertiesOutputFile;
90  
91    /**
92     * Used to activate the default mapping that writes the build properties of
93     * deployable units to
94     * <code>${project.build.directory}/${project.build.finalName}/META-INF/build.properties</code>
95     * and for standard JAR files to
96     * <code>${project.build.outputDirectory}/META-INF/build.properties</code>.
97     *
98     * @parameter default-value=true
99     * @since 1.1
100    */
101   private boolean activatePropertyOutputFileMapping;
102 
103   /**
104    * Maps a packaging to a location for the build meta data properties file.
105    * <p>
106    * This mapping is especially useful for multi projects.
107    * </p>
108    *
109    * @parameter
110    * @since 1.1
111    */
112   protected List<FileMapping> propertyOutputFileMapping;
113 
114   /**
115    * The name of the XML report file to write. If you want to include the XML
116    * file in the artifact, use
117    * <code>${project.build.outputDirectory}/META-INF/buildmetadata.xml</code>.
118    *
119    * @parameter default-value= "${project.build.directory}/buildmetadata.xml"
120    * @since 1.0
121    */
122   protected File xmlOutputFile;
123 
124   /**
125    * Flag to choose whether (<code>true</code>) or not (<code>false</code>) the
126    * XML report should be created.
127    *
128    * @parameter default-value= "true"
129    * @since 1.0
130    */
131   protected boolean createXmlReport;
132 
133   /**
134    * The list of meta data providers to launch that contribute to the meta data.
135    *
136    * @parameter
137    */
138   protected List<Provider> providers;
139 
140   /**
141    * The list of a system properties or environment variables to be selected by
142    * the user to include into the build meta data properties.
143    * <p>
144    * The name is the name of the property, the section is relevant for placing
145    * the property in one of the following sections:
146    * </p>
147    * <ul>
148    * <li><code>build.scm</code></li>
149    * <li><code>build.dateAndVersion</code></li>
150    * <li><code>build.runtime</code></li>
151    * <li><code>build.java</code></li>
152    * <li><code>build.maven</code></li>
153    * <li><code>project</code></li>
154    * <li><code>build.misc</code></li>
155    * </ul>
156    * <p>
157    * If no valid section is given, the property is silently rendered in the
158    * <code>build.misc</code> section.
159    * </p>
160    *
161    * @parameter
162    * @since 1.0
163    */
164   protected List<Property> properties;
165 
166   /**
167    * Flag to indicate whether or not the generated properties file should be
168    * added to the projects filters.
169    * <p>
170    * Filters are only added temporarily (read in-memory during the build) and
171    * are not written to the POM.
172    * </p>
173    *
174    * @parameter expression="${buildMetaData.addToFilters}" default-value="true"
175    * @since 1.0
176    */
177   protected boolean addToFilters;
178 
179   // ****************************** Initializer *******************************
180 
181   // ****************************** Constructors ******************************
182 
183   // ****************************** Inner Classes *****************************
184 
185   // ********************************* Methods ********************************
186 
187   // --- init -----------------------------------------------------------------
188 
189   // --- get&set --------------------------------------------------------------
190 
191   /**
192    * Returns the Maven project.
193    *
194    * @return the Maven project.
195    */
196   public final MavenProject getProject()
197   {
198     return project;
199   }
200 
201   /**
202    * Sets the Maven project.
203    *
204    * @param project the Maven project.
205    */
206   public final void setProject(final MavenProject project)
207   {
208     this.project = project;
209   }
210 
211   /**
212    * Sets the Maven session.
213    * <p>
214    * Used for testing.
215    * </p>
216    *
217    * @param session the Maven session.
218    */
219   public final void setSession(final MavenSession session)
220   {
221     this.session = session;
222   }
223 
224   /**
225    * Sets the name of the properties file to write.
226    * <p>
227    * Used for testing.
228    * </p>
229    *
230    * @param propertiesOutputFile the name of the properties file to write.
231    */
232   public final void setPropertiesOutputFile(final File propertiesOutputFile)
233   {
234     this.propertiesOutputFile = propertiesOutputFile;
235   }
236 
237   // --- business -------------------------------------------------------------
238 
239   // CHECKSTYLE:OFF
240   /**
241    * {@inheritDoc}
242    */
243   public void execute() throws MojoExecutionException, MojoFailureException
244   {
245     // CHECKSTYLE:ON
246     final PropertyOutputFileMapper mapper =
247         new PropertyOutputFileMapper(project, propertyOutputFileMapping);
248     this.propertyOutputFileMapping = mapper.initPropertyOutputFileMapping();
249     this.propertiesOutputFile =
250         mapper.getPropertiesOutputFile(activatePropertyOutputFileMapping,
251             propertiesOutputFile);
252   }
253 
254   /**
255    * Adds the information as build properties for each provider.
256    *
257    * @param buildMetaDataProperties the build meta data properties to add to.
258    * @param scmInfo the information for the SCM provided to the build plugin.
259    * @param providers the providers to iterate over.
260    * @param runAtEndOfBuild checks if the provider is configured to be run at
261    *          the end of the build. If a provider matches this value, it is run.
262    * @throws MojoExecutionException on any problem running on the providers.
263    */
264   protected final void provideBuildMetaData(
265       final Properties buildMetaDataProperties, final ScmInfo scmInfo,
266       final List<Provider> providers, final boolean runAtEndOfBuild)
267     throws MojoExecutionException
268   {
269     if (providers != null && !providers.isEmpty())
270     {
271       final MetaDataProviderBuilder builder =
272           new MetaDataProviderBuilder(project, session, runtime, scmInfo);
273       for (final Provider providerConfig : providers)
274       {
275         if (providerConfig.isRunAtEndOfBuild() == runAtEndOfBuild)
276         {
277           final MetaDataProvider provider = builder.build(providerConfig);
278           provider.provideBuildMetaData(buildMetaDataProperties);
279         }
280       }
281     }
282   }
283 
284   /**
285    * Updates the Maven runtime with build properties.
286    *
287    * @param buildMetaDataProperties the properties to add to the Maven project
288    *          properties.
289    * @param helper the project helper to use.
290    */
291   protected final void updateMavenEnvironment(
292       final Properties buildMetaDataProperties,
293       final BuildPropertiesFileHelper helper)
294   {
295     final Properties projectProperties = helper.getProjectProperties(project);
296 
297     // Filters are only added temporarily and are not written to the POM...
298     if (addToFilters)
299     {
300       project.getBuild().addFilter(propertiesOutputFile.getAbsolutePath());
301     }
302     projectProperties.putAll(buildMetaDataProperties);
303   }
304 
305   // --- object basics --------------------------------------------------------
306 
307 }