View Javadoc
1   package com.simpligility.maven.plugins.android.common;
2   
3   import org.codehaus.plexus.util.xml.Xpp3Dom;
4   import org.apache.commons.lang3.StringUtils;
5   import org.apache.maven.project.MavenProject;
6   import org.apache.maven.model.Plugin;
7   
8   import com.simpligility.maven.plugins.android.PluginInfo;
9   
10  /**
11   * A helper class to access plugin configuration from the pom.
12   *
13   * @author Benoit Billington
14   * @author Manfred Moser
15   */
16  public final class PomConfigurationHelper
17  {
18      public static String getPluginConfigParameter ( MavenProject project, String parameter, String defaultValue )
19      {
20          String value = null;
21          for ( Plugin plugin : project.getBuild().getPlugins() )
22          {
23              if ( plugin.getArtifactId().equals( PluginInfo.getArtifactId() ) )
24              {
25                  Xpp3Dom configuration = getMojoConfiguration( plugin );
26                  if ( configuration != null && configuration.getChild( parameter ) != null )
27                  {
28                    value = configuration.getChild( parameter ).getValue() ;
29                  }
30              }
31          }
32          // if we got nothing, fall back to the default value
33          return ( StringUtils.isEmpty( value ) ) ? defaultValue : value;
34      }
35  
36      public static boolean getPluginConfigParameter ( MavenProject project, String parameter, boolean defaultValue )
37      {
38          String value = getPluginConfigParameter( project, parameter, Boolean.toString( defaultValue ) );
39          return Boolean.valueOf( value );
40      }
41      
42      private static Xpp3Dom getMojoConfiguration( Plugin plugin )
43      {
44          //
45          // We need to look in the configuration element, and then look for configuration elements
46          // within the executions.
47          //
48          Xpp3Dom configuration = ( Xpp3Dom ) plugin.getConfiguration();
49          if ( configuration == null )
50          {
51              if ( !plugin.getExecutions().isEmpty() )
52              {
53                  configuration = ( Xpp3Dom ) plugin.getExecutions().get( 0 ).getConfiguration();
54              }
55          }
56          return configuration;
57      }
58  }