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.data;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Properties;
21  import java.util.Set;
22  import java.util.TreeSet;
23  
24  import org.apache.maven.artifact.versioning.ArtifactVersion;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.execution.RuntimeInformation;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import de.smartics.maven.plugin.buildmetadata.common.Constant;
32  import de.smartics.maven.plugin.buildmetadata.common.MojoUtils;
33  import de.smartics.maven.plugin.buildmetadata.common.Property;
34  import de.smartics.maven.plugin.buildmetadata.maven.MavenPropertyHelper;
35  
36  /**
37   * Extracts information from the Maven project, session, and runtime
38   * information.
39   *
40   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
41   * @version $Revision:591 $
42   */
43  public final class MavenMetaDataProvider extends AbstractMetaDataProvider
44  { // NOPMD
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The selection of properties to be added or hidden.
53     */
54    private final MavenMetaDataSelection selection;
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    /**
61     * Constructor.
62     *
63     * @param project the Maven project.
64     * @param session the Maven session instance.
65     * @param runtime the runtime information of the Maven instance being executed
66     *          for the build.
67     * @param selection the selection of properties to be added or hidden.
68     * @see de.smartics.maven.plugin.buildmetadata.data.AbstractMetaDataProvider#AbstractMetaDataProvider()
69     */
70    public MavenMetaDataProvider(final MavenProject project,
71        final MavenSession session, final RuntimeInformation runtime,
72        final MavenMetaDataSelection selection)
73    {
74      this.project = project;
75      this.session = session;
76      this.runtime = runtime;
77      this.selection = selection;
78    }
79  
80    // ****************************** Inner Classes *****************************
81  
82    // ********************************* Methods ********************************
83  
84    // --- init -----------------------------------------------------------------
85  
86    // --- get&set --------------------------------------------------------------
87  
88    // --- business -------------------------------------------------------------
89  
90    /**
91     * Adds the information of the Maven runtime as build properties.
92     *
93     * @param buildMetaDataProperties the build meta data properties.
94     */
95    public void provideBuildMetaData(final Properties buildMetaDataProperties)
96    {
97      if (runtime != null)
98      {
99        provideRuntimeInfo(buildMetaDataProperties);
100     }
101 
102     if (session != null)
103     {
104       provideSessionInfo(buildMetaDataProperties);
105     }
106 
107     if (project != null)
108     {
109       provideProjectInfo(buildMetaDataProperties);
110     }
111 
112     final List<Property> selectedProperties =
113         selection.getSelectedSystemProperties();
114     if (selectedProperties != null && !selectedProperties.isEmpty())
115     {
116       provideSelectedProperties(buildMetaDataProperties, selectedProperties);
117     }
118   }
119 
120   private void provideRuntimeInfo(final Properties buildMetaDataProperties)
121   {
122     final ArtifactVersion mavenVersion = runtime.getApplicationVersion();
123     final String mavenVersionString = mavenVersion.toString();
124     buildMetaDataProperties.setProperty(Constant.PROP_NAME_MAVEN_VERSION,
125         mavenVersionString);
126 
127     if (selection.isAddJavaRuntimeInfo())
128     {
129       providePropertyIfExists(buildMetaDataProperties, "java.runtime.name",
130           Constant.PROP_NAME_JAVA_RUNTIME_NAME);
131       providePropertyIfExists(buildMetaDataProperties, "java.runtime.version",
132           Constant.PROP_NAME_JAVA_RUNTIME_VERSION);
133       providePropertyIfExists(buildMetaDataProperties, "java.vendor",
134           Constant.PROP_NAME_JAVA_VENDOR);
135       providePropertyIfExists(buildMetaDataProperties, "java.vm.name",
136           Constant.PROP_NAME_JAVA_VM);
137       providePropertyIfExists(buildMetaDataProperties,
138           "sun.management.compiler", Constant.PROP_NAME_JAVA_COMPILER);
139     }
140 
141     if (selection.isAddOsInfo())
142     {
143       providePropertyIfExists(buildMetaDataProperties, "os.name",
144           Constant.PROP_NAME_OS_NAME);
145       providePropertyIfExists(buildMetaDataProperties, "os.arch",
146           Constant.PROP_NAME_OS_ARCH);
147       providePropertyIfExists(buildMetaDataProperties, "os.version",
148           Constant.PROP_NAME_OS_VERSION);
149     }
150   }
151 
152   private void providePropertyIfExists(
153       final Properties buildMetaDataProperties,
154       final String systemPropertyName, final String propName)
155   {
156     final String value = System.getProperty(systemPropertyName);
157     if (StringUtils.isNotBlank(value))
158     {
159       buildMetaDataProperties.setProperty(propName, value);
160     }
161   }
162 
163   private void provideSelectedProperties(
164       final Properties buildMetaDataProperties,
165       final List<Property> selectedProperties)
166   {
167     final MavenPropertyHelper helper = new MavenPropertyHelper(project);
168     for (final Property property : selectedProperties)
169     {
170       final String name = property.getName();
171 
172       final String fixedValue = property.getValue();
173       if (fixedValue != null)
174       {
175         setProperty(buildMetaDataProperties, property, fixedValue);
176       }
177       else
178       {
179         final String mavenPropertyValue = helper.getProperty(name);
180         if (mavenPropertyValue != null)
181         {
182           setProperty(buildMetaDataProperties, property, mavenPropertyValue);
183         }
184         else
185         {
186           final String value = System.getProperty(name);
187           if (StringUtils.isNotBlank(value))
188           {
189             setProperty(buildMetaDataProperties, property, value);
190           }
191           else
192           {
193             final String envValue = System.getenv(name);
194             // CHECKSTYLE:OFF
195             if (StringUtils.isNotBlank(envValue))
196             {
197               setProperty(buildMetaDataProperties, property, envValue);
198             }
199             // CHECKSTYLE:ON
200           }
201         }
202       }
203     }
204   }
205 
206   private static void setProperty(final Properties buildMetaDataProperties,
207       final Property property, final String value)
208   {
209     final String name = property.getMappedName();
210     buildMetaDataProperties.setProperty(name, value);
211   }
212 
213   private void provideSessionInfo(final Properties buildMetaDataProperties)
214   {
215     final Properties executionProperties =
216         provideExecutionProperties(buildMetaDataProperties);
217     if (selection.isAddMavenExecutionInfo())
218     {
219       provideGoals(buildMetaDataProperties);
220       provideSpecialEnvVars(buildMetaDataProperties, executionProperties);
221     }
222   }
223 
224   private void provideProjectInfo(final Properties buildMetaDataProperties)
225   {
226     if (selection.isAddMavenExecutionInfo())
227     {
228       provideActiveProfiles(buildMetaDataProperties);
229       provideExecutedProjectInfo(buildMetaDataProperties);
230       final String filters = MojoUtils.toPrettyString(project.getFilters());
231       if (StringUtils.isNotBlank(filters))
232       {
233         buildMetaDataProperties.setProperty(Constant.PROP_NAME_MAVEN_FILTERS,
234             filters);
235       }
236     }
237 
238     if (selection.isAddProjectInfo())
239     {
240       final String projectUrl = project.getUrl();
241       if (StringUtils.isNotBlank(projectUrl))
242       {
243         buildMetaDataProperties.setProperty(
244             Constant.PROP_NAME_PROJECT_HOMEPAGE, projectUrl);
245       }
246 
247       final Properties projectProperties = project.getProperties();
248       if (projectProperties != null)
249       {
250         providePropertiesProperty(buildMetaDataProperties, projectProperties,
251             Constant.PROP_NAME_PROJECT_OPS);
252         providePropertiesProperty(buildMetaDataProperties, projectProperties,
253             Constant.PROP_NAME_PROJECT_CATEGORY);
254         providePropertiesProperty(buildMetaDataProperties, projectProperties,
255             Constant.PROP_NAME_PROJECT_SUBCATEGORY);
256         providePropertiesProperty(buildMetaDataProperties, projectProperties,
257             Constant.PROP_NAME_PROJECT_TAGS);
258       }
259     }
260   }
261 
262   private static void providePropertiesProperty(
263       final Properties buildMetaDataProperties,
264       final Properties projectProperties, final String name)
265   {
266     final String value = projectProperties.getProperty(name);
267     if (StringUtils.isNotBlank(value))
268     {
269       buildMetaDataProperties.setProperty(name, value);
270     }
271   }
272 
273   private void provideExecutedProjectInfo(
274       final Properties buildMetaDataProperties)
275   {
276     buildMetaDataProperties.setProperty(
277         Constant.PROP_NAME_MAVEN_IS_EXECUTION_ROOT,
278         String.valueOf(project.isExecutionRoot()));
279     final MavenProject executionProject = project.getExecutionProject();
280     if (executionProject != null)
281     {
282       buildMetaDataProperties.setProperty(
283           Constant.PROP_NAME_MAVEN_EXECUTION_PROJECT, executionProject.getId());
284     }
285   }
286 
287   private void provideActiveProfiles(final Properties buildMetaDataProperties)
288   {
289     final List<Profile> profiles = getActiveProfiles();
290     if (profiles != null && !profiles.isEmpty())
291     {
292       final List<String> profileIds = new ArrayList<String>(profiles.size());
293       for (final Profile profile : profiles)
294       {
295         final String profileId = profile.getId();
296         if (!profileIds.contains(profileId))
297         {
298           final String key =
299               Constant.MAVEN_ACTIVE_PROFILE_PREFIX + '.' + profileId;
300           final String value = profile.getSource();
301           buildMetaDataProperties.setProperty(key, value);
302           profileIds.add(profileId);
303         }
304       }
305       buildMetaDataProperties.setProperty(
306           Constant.PROP_NAME_MAVEN_ACTIVE_PROFILES,
307           MojoUtils.toPrettyString(profileIds));
308     }
309   }
310 
311   /**
312    * Delegates call to
313    * {@link org.apache.maven.project.MavenProject#getActiveProfiles()}.
314    *
315    * @return the result of the call to
316    *         {@link org.apache.maven.project.MavenProject#getActiveProfiles()}.
317    * @see org.apache.maven.project.MavenProject#getActiveProfiles()
318    */
319   @SuppressWarnings("unchecked")
320   private List<Profile> getActiveProfiles()
321   {
322     return (List<Profile>) project.getActiveProfiles();
323   }
324 
325   private void provideGoals(final Properties buildMetaDataProperties)
326   {
327     final String goals = MojoUtils.toPrettyString(session.getGoals());
328     buildMetaDataProperties.setProperty(Constant.PROP_NAME_MAVEN_GOALS, goals);
329   }
330 
331   private Properties provideExecutionProperties(
332       final Properties buildMetaDataProperties)
333   {
334     final Properties executionProperties = session.getExecutionProperties();
335     if (selection.isAddEnvInfo())
336     {
337       final Set<Object> sortedKeys = new TreeSet<Object>();
338       sortedKeys.addAll(executionProperties.keySet());
339       for (final Object originalKey : sortedKeys)
340       {
341         final String key =
342             Constant.MAVEN_EXECUTION_PROPERTIES_PREFIX + '.' + originalKey;
343         final String value =
344             executionProperties.getProperty((String) originalKey);
345         buildMetaDataProperties.setProperty(key, value);
346       }
347     }
348     return executionProperties;
349   }
350 
351   private void provideSpecialEnvVars(final Properties buildMetaDataProperties,
352       final Properties executionProperties)
353   {
354     if (!selection.isHideCommandLineInfo())
355     {
356       final String commandLine =
357           executionProperties.getProperty("env.MAVEN_CMD_LINE_ARGS");
358       if (!StringUtils.isEmpty(commandLine))
359       {
360         buildMetaDataProperties.setProperty(Constant.PROP_NAME_MAVEN_CMDLINE,
361             commandLine);
362       }
363     }
364 
365     if (!selection.isHideMavenOptsInfo())
366     {
367       final String mavenOpts =
368           executionProperties.getProperty("env.MAVEN_OPTS");
369       if (!StringUtils.isEmpty(mavenOpts))
370       {
371         buildMetaDataProperties.setProperty(Constant.PROP_NAME_MAVEN_OPTS,
372             mavenOpts);
373       }
374     }
375 
376     if (!selection.isHideJavaOptsInfo())
377     {
378       final String javaOpts = executionProperties.getProperty("env.JAVA_OPTS");
379       if (!StringUtils.isEmpty(javaOpts))
380       {
381         buildMetaDataProperties.setProperty(Constant.PROP_NAME_JAVA_OPTS,
382             javaOpts);
383       }
384     }
385   }
386 
387   // --- object basics --------------------------------------------------------
388 
389 }