Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2014 smartics, Kronseder & Reiner GmbH
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.redhat.rcm.maven.plugin.buildmetadata.data;
 
 import java.util.List;
 import java.util.Set;
Extracts information from the Maven project, session, and runtime information.

Author(s):
Robert Reiner
Version:
$Revision:591 $
 
 public final class MavenMetaDataProvider extends AbstractMetaDataProvider
 { // NOPMD
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
   // --- members --------------------------------------------------------------
 
  
The selection of properties to be added or hidden.
 
   private final MavenMetaDataSelection selection;
 
   // ****************************** Initializer *******************************
 
   // ****************************** Constructors ******************************
 
  
Constructor.

Parameters:
project the Maven project.
session the Maven session instance.
runtime the runtime information of the Maven instance being executed for the build.
selection the selection of properties to be added or hidden.
See also:
com.redhat.rcm.maven.plugin.buildmetadata.data.AbstractMetaDataProvider.AbstractMetaDataProvider.()
 
   public MavenMetaDataProvider(final MavenProject project,
       final MavenSession sessionfinal RuntimeInformation runtime,
       final MavenMetaDataSelection selection)
   {
     this. = project;
     this. = session;
     this. = runtime;
     this. = selection;
   }
 
   // ****************************** Inner Classes *****************************
 
   // ********************************* Methods ********************************
 
   // --- init -----------------------------------------------------------------
 
   // --- get&set --------------------------------------------------------------
 
   // --- business -------------------------------------------------------------
 
  
Adds the information of the Maven runtime as build properties.

Parameters:
buildMetaDataProperties the build meta data properties.
 
   @Override
 public void provideBuildMetaData(final Properties buildMetaDataProperties)
   {
     if ( != null)
     {
       provideRuntimeInfo(buildMetaDataProperties);
     }
    if ( != null)
    {
      provideSessionInfo(buildMetaDataProperties);
    }
    if ( != null)
    {
      provideProjectInfo(buildMetaDataProperties);
    }
    final List<PropertyselectedProperties =
    if (selectedProperties != null && !selectedProperties.isEmpty())
    {
      provideSelectedProperties(buildMetaDataPropertiesselectedProperties);
    }
  }
  private void provideRuntimeInfo(final Properties buildMetaDataProperties)
  {
    final ArtifactVersion mavenVersion = .getApplicationVersion();
    final String mavenVersionString = mavenVersion.toString();
    buildMetaDataProperties.setProperty(.,
        mavenVersionString);
    {
      providePropertyIfExists(buildMetaDataProperties"java.runtime.name",
      providePropertyIfExists(buildMetaDataProperties"java.runtime.version",
      providePropertyIfExists(buildMetaDataProperties"java.vendor",
          .);
      providePropertyIfExists(buildMetaDataProperties"java.vm.name",
          .);
      providePropertyIfExists(buildMetaDataProperties,
          "sun.management.compiler".);
    }
    if (.isAddOsInfo())
    {
      providePropertyIfExists(buildMetaDataProperties"os.name",
          .);
      providePropertyIfExists(buildMetaDataProperties"os.arch",
          .);
      providePropertyIfExists(buildMetaDataProperties"os.version",
          .);
    }
  }
  private void providePropertyIfExists(
      final Properties buildMetaDataProperties,
      final String systemPropertyNamefinal String propName)
  {
    final String value = System.getProperty(systemPropertyName);
    if (StringUtils.isNotBlank(value))
    {
      buildMetaDataProperties.setProperty(propNamevalue);
    }
  }
  private void provideSelectedProperties(
      final Properties buildMetaDataProperties,
      final List<PropertyselectedProperties)
  {
    final MavenPropertyHelper helper = new MavenPropertyHelper();
    for (final Property property : selectedProperties)
    {
      final String name = property.getName();
      final String fixedValue = property.getValue();
      if (fixedValue != null)
      {
        setProperty(buildMetaDataPropertiespropertyfixedValue);
      }
      else
      {
        final String mavenPropertyValue = helper.getProperty(name);
        if (mavenPropertyValue != null)
        {
          setProperty(buildMetaDataPropertiespropertymavenPropertyValue);
        }
        else
        {
          final String value = System.getProperty(name);
          if (StringUtils.isNotBlank(value))
          {
            setProperty(buildMetaDataPropertiespropertyvalue);
          }
          else
          {
            final String envValue = System.getenv(name);
            // CHECKSTYLE:OFF
            if (StringUtils.isNotBlank(envValue))
            {
              setProperty(buildMetaDataPropertiespropertyenvValue);
            }
            // CHECKSTYLE:ON
          }
        }
      }
    }
  }
  private static void setProperty(final Properties buildMetaDataProperties,
      final Property propertyfinal String value)
  {
    final String name = property.getMappedName();
    buildMetaDataProperties.setProperty(namevalue);
  }
  private void provideSessionInfo(final Properties buildMetaDataProperties)
  {
    final Properties executionProperties =
        provideExecutionProperties(buildMetaDataProperties);
    {
      provideGoals(buildMetaDataProperties);
      provideSpecialEnvVars(buildMetaDataPropertiesexecutionProperties);
    }
  }
  private void provideProjectInfo(final Properties buildMetaDataProperties)
  {
    {
      provideActiveProfiles(buildMetaDataProperties);
      provideExecutedProjectInfo(buildMetaDataProperties);
      final String filters = MojoUtils.toPrettyString(.getFilters());
      if (StringUtils.isNotBlank(filters))
      {
        buildMetaDataProperties.setProperty(.,
            filters);
      }
    }
    {
      final String projectUrl = .getUrl();
      if (StringUtils.isNotBlank(projectUrl))
      {
        buildMetaDataProperties.setProperty(
            .projectUrl);
      }
      final Properties projectProperties = .getProperties();
      if (projectProperties != null)
      {
        providePropertiesProperty(buildMetaDataPropertiesprojectProperties,
            .);
        providePropertiesProperty(buildMetaDataPropertiesprojectProperties,
            .);
        providePropertiesProperty(buildMetaDataPropertiesprojectProperties,
            .);
        providePropertiesProperty(buildMetaDataPropertiesprojectProperties,
            .);
      }
    }
  }
  private static void providePropertiesProperty(
      final Properties buildMetaDataProperties,
      final Properties projectPropertiesfinal String name)
  {
    final String value = projectProperties.getProperty(name);
    if (StringUtils.isNotBlank(value))
    {
      buildMetaDataProperties.setProperty(namevalue);
    }
  }
  private void provideExecutedProjectInfo(
      final Properties buildMetaDataProperties)
  {
    buildMetaDataProperties.setProperty(
        String.valueOf(.isExecutionRoot()));
    final MavenProject executionProject = .getExecutionProject();
    if (executionProject != null)
    {
      buildMetaDataProperties.setProperty(
          .executionProject.getId());
    }
  }
  private void provideActiveProfiles(final Properties buildMetaDataProperties)
  {
    final List<Profileprofiles = getActiveProfiles();
    if (profiles != null && !profiles.isEmpty())
    {
      final List<StringprofileIds = new ArrayList<String>(profiles.size());
      for (final Profile profile : profiles)
      {
        final String profileId = profile.getId();
        if (!profileIds.contains(profileId))
        {
          final String key =
              . + '.' + profileId;
          final String value = profile.getSource();
          buildMetaDataProperties.setProperty(keyvalue);
          profileIds.add(profileId);
        }
      }
      buildMetaDataProperties.setProperty(
          MojoUtils.toPrettyString(profileIds));
    }
  }

  
  @SuppressWarnings("unchecked")
  {
    return .getActiveProfiles();
  }
  private void provideGoals(final Properties buildMetaDataProperties)
  {
    final String goals = MojoUtils.toPrettyString(.getGoals());
    buildMetaDataProperties.setProperty(.goals);
  }
      final Properties buildMetaDataProperties)
  {
    final Properties executionProperties = .getExecutionProperties();
    if (.isAddEnvInfo())
    {
      final Set<ObjectsortedKeys = new TreeSet<Object>();
      sortedKeys.addAll(executionProperties.keySet());
      for (final Object originalKey : sortedKeys)
      {
        final String key =
            . + '.' + originalKey;
        final String value =
            executionProperties.getProperty((StringoriginalKey);
        buildMetaDataProperties.setProperty(keyvalue);
      }
    }
    return executionProperties;
  }
  private void provideSpecialEnvVars(final Properties buildMetaDataProperties,
      final Properties executionProperties)
  {
    {
      final String commandLine = calcCommandLine(executionProperties);
      if (!StringUtils.isEmpty(commandLine))
      {
          buildMetaDataProperties.setProperty(.commandLine);
      }
    }
    {
      final String mavenOpts =
          executionProperties.getProperty("env.MAVEN_OPTS");
      if (!StringUtils.isEmpty(mavenOpts))
      {
        buildMetaDataProperties.setProperty(.,
            mavenOpts);
      }
    }
    {
      final String javaOpts = executionProperties.getProperty("env.JAVA_OPTS");
      if (!StringUtils.isEmpty(javaOpts))
      {
        buildMetaDataProperties.setProperty(.,
            javaOpts);
      }
    }
  }
  private String calcCommandLine(final Properties executionProperties)
  {
      String commandLine = executionProperties.getProperty("sun.java.command");
      if (commandLine != null)
      {
          commandLine = commandLine.replace("org.codehaus.plexus.classworlds.launcher.Launcher """);
      }
      return commandLine;
  }
New to GrepCode? Check out our FAQ X