View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.felix.bundleplugin;
20  
21  
22  import org.apache.maven.archiver.MavenArchiveConfiguration;
23  import org.apache.maven.project.MavenProject;
24  import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
25  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
26  import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
27  import org.codehaus.plexus.component.configurator.expression.DefaultExpressionEvaluator;
28  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
29  import org.codehaus.plexus.configuration.PlexusConfiguration;
30  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
31  import org.codehaus.plexus.util.xml.Xpp3Dom;
32  
33  
34  /**
35   * Provide access to the archive configuration from the jar plugin
36   * 
37   * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
38   */
39  public final class JarPluginConfiguration
40  {
41      public static MavenArchiveConfiguration getArchiveConfiguration( MavenProject project )
42      {
43          MavenArchiveConfiguration archiveConfig = new MavenArchiveConfiguration();
44  
45          try
46          {
47              ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
48              ClassLoader loader = JarPluginConfiguration.class.getClassLoader();
49              ExpressionEvaluator evaluator = new DefaultExpressionEvaluator();
50              ConverterLookup converters = new DefaultConverterLookup();
51  
52              PlexusConfiguration settings = null;
53  
54              try
55              {
56                  // first look for bundle specific archive settings
57                  settings = getPluginConfiguration( project, "org.apache.felix", "maven-bundle-plugin" );
58                  settings = settings.getChild( "archive" );
59              }
60              catch ( Exception e )
61              {
62              }
63  
64              // if it's empty fall back to the jar archive settings
65              if ( null == settings || settings.getChildCount() == 0 )
66              {
67                  settings = getCorePluginConfiguration( project, "jar" );
68                  settings = settings.getChild( "archive" );
69              }
70  
71              converter.processConfiguration( converters, archiveConfig, loader, settings, evaluator, null );
72          }
73          catch ( Exception e )
74          {
75              // ignore and return empty configuration...
76          }
77  
78          return archiveConfig;
79      }
80  
81  
82      private static PlexusConfiguration getCorePluginConfiguration( MavenProject project, String pluginName )
83      {
84          return getPluginConfiguration( project, "org.apache.maven.plugins", "maven-" + pluginName + "-plugin" );
85      }
86  
87  
88      private static PlexusConfiguration getPluginConfiguration( MavenProject project, String groupId, String artifactId )
89      {
90          Xpp3Dom pluginConfig = project.getGoalConfiguration( groupId, artifactId, null, null );
91  
92          return new XmlPlexusConfiguration( pluginConfig );
93      }
94  }