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.Locale;
21  import java.util.ResourceBundle;
22  
23  import org.apache.maven.doxia.sink.Sink;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.reporting.MavenReportException;
26  
27  import de.smartics.maven.plugin.buildmetadata.common.Property;
28  
29  /**
30   * Generates a report about the meta data provided to the build.
31   *
32   * @goal buildmetadata-report
33   * @phase site
34   * @description Generates a report on the build meta data.
35   * @requiresProject
36   * @threadSafe
37   * @since 1.0
38   */
39  public final class BuildReportMojo extends AbstractReportMojo
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The name of the properties file to write. Per default this value is
49     * overridden by packaging dependent locations. Please refer to <a
50     * href="#activatePropertyOutputFileMapping"
51     * >activatePropertyOutputFileMapping</a> for details.
52     *
53     * @parameter default-value=
54     *            "${project.build.outputDirectory}/META-INF/build.properties"
55     * @since 1.0
56     */
57    private File propertiesOutputFile;
58  
59    /**
60     * Used to activate the default mapping that writes the build properties of
61     * deployable units to
62     * <code>${project.build.directory}/${project.build.finalName}/META-INF/build.properties</code>
63     * and for standard JAR files to
64     * <code>${project.build.outputDirectory}/META-INF/build.properties</code>.
65     *
66     * @parameter default-value=true
67     * @since 1.1
68     */
69    private boolean activatePropertyOutputFileMapping;
70  
71    /**
72     * Maps a packaging to a location for the build meta data properties file.
73     * <p>
74     * This mapping is especially useful for multi projects.
75     * </p>
76     *
77     * @parameter
78     * @since 1.1
79     */
80    protected List<FileMapping> propertyOutputFileMapping; // NOPMD
81  
82    /**
83     * The list of a system properties or environment variables to be selected by
84     * the user to include into the build meta data properties.
85     * <p>
86     * The name is the name of the property, the section is relevant for placing
87     * the property in one of the following sections:
88     * </p>
89     * <ul>
90     * <li><code>build.scm</code></li>
91     * <li><code>build.dateAndVersion</code></li>
92     * <li><code>build.runtime</code></li>
93     * <li><code>build.java</code></li>
94     * <li><code>build.maven</code></li>
95     * <li><code>project</code></li>
96     * <li><code>build.misc</code></li>
97     * </ul>
98     * <p>
99     * If no valid section is given, the property is silently rendered in the
100    * <code>build.misc</code> section.
101    * </p>
102    *
103    * @parameter
104    * @since 1.0
105    */
106   protected List<Property> properties; // NOPMD
107 
108   /**
109    * Flag to check if the mojo has already been initialized.
110    */
111   private boolean initialized;
112 
113   // ****************************** Initializer *******************************
114 
115   // ****************************** Constructors ******************************
116 
117   // ****************************** Inner Classes *****************************
118 
119   // ********************************* Methods ********************************
120 
121   // --- init -----------------------------------------------------------------
122 
123   // --- get&set --------------------------------------------------------------
124 
125   /**
126    * {@inheritDoc}
127    *
128    * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
129    */
130   public String getName(final Locale locale)
131   {
132     return getBundle(locale).getString("report.name");
133   }
134 
135   /**
136    * {@inheritDoc}
137    *
138    * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
139    */
140   public String getDescription(final Locale locale)
141   {
142     return getBundle(locale).getString("report.description");
143   }
144 
145   /**
146    * {@inheritDoc}
147    *
148    * @see org.apache.maven.reporting.MavenReport#getOutputName()
149    */
150   public String getOutputName()
151   {
152     return "build-report";
153   }
154 
155   // --- business -------------------------------------------------------------
156 
157   /**
158    * {@inheritDoc}
159    */
160   @Override
161   public void execute() throws MojoExecutionException
162   {
163     init();
164     super.execute();
165   }
166 
167   /**
168    * Initializes the Mojo.
169    */
170   protected void init()
171   {
172     if (!initialized)
173     {
174       final PropertyOutputFileMapper mapper =
175           new PropertyOutputFileMapper(project, propertyOutputFileMapping);
176       this.propertyOutputFileMapping = mapper.initPropertyOutputFileMapping();
177       this.propertiesOutputFile =
178           mapper.getPropertiesOutputFile(activatePropertyOutputFileMapping,
179               propertiesOutputFile);
180       this.initialized = true;
181     }
182   }
183 
184   /**
185    * {@inheritDoc}
186    *
187    * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
188    */
189   @Override
190   protected void executeReport(final Locale locale) throws MavenReportException
191   {
192     super.executeReport(locale);
193 
194     final Sink sink = getSink();
195     final ResourceBundle messages = getBundle(locale);
196     final BuildReportRenderer renderer =
197         new BuildReportRenderer(messages, sink, propertiesOutputFile,
198             properties);
199     renderer.renderReport();
200   }
201 
202   /**
203    * {@inheritDoc}
204    * <p>
205    * Returns <code>false</code> if the properties file that contains the build
206    * information cannot be read.
207    * </p>
208    *
209    * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
210    */
211   @Override
212   public boolean canGenerateReport()
213   {
214     init();
215     return super.canGenerateReport() && propertiesOutputFile.canRead();
216   }
217 
218   // --- object basics --------------------------------------------------------
219 
220 }