1 package org.apache.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
42
43
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
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
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
115
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 }