View Javadoc

1   package org.apache.maven.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
24  import org.apache.maven.artifact.repository.metadata.Metadata;
25  import org.apache.maven.artifact.repository.metadata.Plugin;
26  import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
27  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
28  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
29  import org.codehaus.plexus.logging.AbstractLogEnabled;
30  
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * Manage plugin prefix to artifact ID mapping associations.
40   * 
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   * @version $Id: DefaultPluginMappingManager.java 495147 2007-01-11 07:47:53Z
43   *          jvanzyl $
44   */
45  public class DefaultPluginMappingManager
46      extends AbstractLogEnabled
47      implements PluginMappingManager
48  {
49      protected RepositoryMetadataManager repositoryMetadataManager;
50  
51      private Map pluginDefinitionsByPrefix = new HashMap();
52  
53      public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories,
54                                                        ArtifactRepository localRepository )
55      {
56          // if not found, try from the remote repository
57          if ( !pluginDefinitionsByPrefix.containsKey( pluginPrefix ) )
58          {
59              getLogger().info( "Searching repository for plugin with prefix: \'" + pluginPrefix + "\'." );
60  
61              loadPluginMappings( groupIds, pluginRepositories, localRepository );
62          }
63  
64          return (org.apache.maven.model.Plugin) pluginDefinitionsByPrefix.get( pluginPrefix );
65      }
66  
67      private void loadPluginMappings( List groupIds, List pluginRepositories, ArtifactRepository localRepository )
68      {
69          List pluginGroupIds = new ArrayList( groupIds );
70  
71          // TODO: use constant
72          if ( !pluginGroupIds.contains( "org.apache.maven.plugins" ) )
73          {
74              pluginGroupIds.add( "org.apache.maven.plugins" );
75          }
76          if ( !pluginGroupIds.contains( "org.codehaus.mojo" ) )
77          {
78              pluginGroupIds.add( "org.codehaus.mojo" );
79          }
80  
81          for ( Iterator it = pluginGroupIds.iterator(); it.hasNext(); )
82          {
83              String groupId = (String) it.next();
84              getLogger().debug( "Loading plugin prefixes from group: " + groupId );
85              try
86              {
87                  loadPluginMappings( groupId, pluginRepositories, localRepository );
88              }
89              catch ( RepositoryMetadataResolutionException e )
90              {
91                  getLogger().warn( "Cannot resolve plugin-mapping metadata for groupId: " + groupId + " - IGNORING." );
92  
93                  getLogger().debug( "Error resolving plugin-mapping metadata for groupId: " + groupId + ".", e );
94              }
95          }
96      }
97  
98      private void loadPluginMappings( String groupId, List pluginRepositories, ArtifactRepository localRepository )
99          throws RepositoryMetadataResolutionException
100     {
101         RepositoryMetadata metadata = new GroupRepositoryMetadata( groupId );
102 
103         repositoryMetadataManager.resolve( metadata, pluginRepositories, localRepository );
104 
105         Metadata repoMetadata = metadata.getMetadata();
106         if ( repoMetadata != null )
107         {
108             for ( Iterator pluginIterator = repoMetadata.getPlugins().iterator(); pluginIterator.hasNext(); )
109             {
110                 Plugin mapping = (Plugin) pluginIterator.next();
111 
112                 String prefix = mapping.getPrefix();
113                 
114                 //if the prefix has already been found, don't add it again.
115                 //this is to preserve the correct ordering of prefix searching (MNG-2926)
116                 if ( !pluginDefinitionsByPrefix.containsKey( prefix ) )
117                 {
118                     String artifactId = mapping.getArtifactId();
119 
120                     org.apache.maven.model.Plugin plugin = new org.apache.maven.model.Plugin();
121 
122                     plugin.setGroupId( metadata.getGroupId() );
123 
124                     plugin.setArtifactId( artifactId );
125 
126                     pluginDefinitionsByPrefix.put( prefix, plugin );
127                 }
128             }
129         }
130     }
131 }