001: package org.apache.maven.plugin;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.maven.artifact.repository.ArtifactRepository;
023: import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
024: import org.apache.maven.artifact.repository.metadata.Metadata;
025: import org.apache.maven.artifact.repository.metadata.Plugin;
026: import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
027: import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
028: import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
029: import org.codehaus.plexus.logging.AbstractLogEnabled;
030:
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: /**
039: * Manage plugin prefix to artifact ID mapping associations.
040: *
041: * @author <a href="mailto:brett@apache.org">Brett Porter</a>
042: * @version $Id: DefaultPluginMappingManager.java 495147 2007-01-11 07:47:53Z
043: * jvanzyl $
044: */
045: public class DefaultPluginMappingManager extends AbstractLogEnabled
046: implements PluginMappingManager {
047: protected RepositoryMetadataManager repositoryMetadataManager;
048:
049: private Map pluginDefinitionsByPrefix = new HashMap();
050:
051: public org.apache.maven.model.Plugin getByPrefix(
052: String pluginPrefix, List groupIds,
053: List pluginRepositories, ArtifactRepository localRepository) {
054: // if not found, try from the remote repository
055: if (!pluginDefinitionsByPrefix.containsKey(pluginPrefix)) {
056: getLogger().info(
057: "Searching repository for plugin with prefix: \'"
058: + pluginPrefix + "\'.");
059:
060: loadPluginMappings(groupIds, pluginRepositories,
061: localRepository);
062: }
063:
064: return (org.apache.maven.model.Plugin) pluginDefinitionsByPrefix
065: .get(pluginPrefix);
066: }
067:
068: private void loadPluginMappings(List groupIds,
069: List pluginRepositories, ArtifactRepository localRepository) {
070: List pluginGroupIds = new ArrayList(groupIds);
071:
072: // TODO: use constant
073: if (!pluginGroupIds.contains("org.apache.maven.plugins")) {
074: pluginGroupIds.add("org.apache.maven.plugins");
075: }
076: if (!pluginGroupIds.contains("org.codehaus.mojo")) {
077: pluginGroupIds.add("org.codehaus.mojo");
078: }
079:
080: for (Iterator it = pluginGroupIds.iterator(); it.hasNext();) {
081: String groupId = (String) it.next();
082: getLogger().debug(
083: "Loading plugin prefixes from group: " + groupId);
084: try {
085: loadPluginMappings(groupId, pluginRepositories,
086: localRepository);
087: } catch (RepositoryMetadataResolutionException e) {
088: getLogger().warn(
089: "Cannot resolve plugin-mapping metadata for groupId: "
090: + groupId + " - IGNORING.");
091:
092: getLogger().debug(
093: "Error resolving plugin-mapping metadata for groupId: "
094: + groupId + ".", e);
095: }
096: }
097: }
098:
099: private void loadPluginMappings(String groupId,
100: List pluginRepositories, ArtifactRepository localRepository)
101: throws RepositoryMetadataResolutionException {
102: RepositoryMetadata metadata = new GroupRepositoryMetadata(
103: groupId);
104:
105: repositoryMetadataManager.resolve(metadata, pluginRepositories,
106: localRepository);
107:
108: Metadata repoMetadata = metadata.getMetadata();
109: if (repoMetadata != null) {
110: for (Iterator pluginIterator = repoMetadata.getPlugins()
111: .iterator(); pluginIterator.hasNext();) {
112: Plugin mapping = (Plugin) pluginIterator.next();
113:
114: String prefix = mapping.getPrefix();
115:
116: //if the prefix has already been found, don't add it again.
117: //this is to preserve the correct ordering of prefix searching (MNG-2926)
118: if (!pluginDefinitionsByPrefix.containsKey(prefix)) {
119: String artifactId = mapping.getArtifactId();
120:
121: org.apache.maven.model.Plugin plugin = new org.apache.maven.model.Plugin();
122:
123: plugin.setGroupId(metadata.getGroupId());
124:
125: plugin.setArtifactId(artifactId);
126:
127: pluginDefinitionsByPrefix.put(prefix, plugin);
128: }
129: }
130: }
131: }
132: }
|