001: package org.apache.maven.lifecycle.plan.testutils;
002:
003: import org.apache.maven.lifecycle.MojoBindingUtils;
004: import org.apache.maven.lifecycle.model.MojoBinding;
005: import org.apache.maven.model.Plugin;
006: import org.apache.maven.model.ReportPlugin;
007: import org.apache.maven.plugin.descriptor.MojoDescriptor;
008: import org.apache.maven.plugin.descriptor.PluginDescriptor;
009: import org.apache.maven.plugin.loader.PluginLoader;
010: import org.apache.maven.plugin.loader.PluginLoaderException;
011: import org.apache.maven.project.MavenProject;
012: import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
013:
014: import java.util.HashMap;
015: import java.util.Map;
016:
017: public class TestPluginLoader implements PluginLoader {
018:
019: private static final Map DEFAULT_PLUGIN_DESCRIPTORS;
020:
021: private static final Map DEFAULT_PLUGIN_PREFIXES;
022:
023: static {
024: Map descriptors = new HashMap();
025: Map prefixes = new HashMap();
026:
027: {
028: PluginDescriptor pd = createPluginDescriptor(
029: "maven-resources-plugin", "resources",
030: "org.apache.maven.plugins", "1");
031:
032: createMojoDescriptor(pd, "resources");
033: createMojoDescriptor(pd, "testResources");
034:
035: descriptors.put(pd.getId(), pd);
036: prefixes.put(pd.getGoalPrefix(), pd);
037: }
038:
039: {
040: PluginDescriptor pd = createPluginDescriptor(
041: "maven-compiler-plugin", "compiler",
042: "org.apache.maven.plugins", "1");
043:
044: createMojoDescriptor(pd, "compile");
045: createMojoDescriptor(pd, "testCompile");
046:
047: descriptors.put(pd.getId(), pd);
048: prefixes.put(pd.getGoalPrefix(), pd);
049: }
050:
051: {
052: PluginDescriptor pd = createPluginDescriptor(
053: "maven-surefire-plugin", "surefire",
054: "org.apache.maven.plugins", "1");
055:
056: createMojoDescriptor(pd, "test");
057:
058: descriptors.put(pd.getId(), pd);
059: prefixes.put(pd.getGoalPrefix(), pd);
060: }
061:
062: {
063: PluginDescriptor pd = createPluginDescriptor(
064: "maven-jar-plugin", "jar",
065: "org.apache.maven.plugins", "1");
066:
067: createMojoDescriptor(pd, "jar");
068:
069: descriptors.put(pd.getId(), pd);
070: prefixes.put(pd.getGoalPrefix(), pd);
071: }
072:
073: DEFAULT_PLUGIN_DESCRIPTORS = descriptors;
074: DEFAULT_PLUGIN_PREFIXES = prefixes;
075: }
076:
077: private Map pluginDescriptors = new HashMap(
078: DEFAULT_PLUGIN_DESCRIPTORS);
079:
080: private Map pluginPrefixes = new HashMap(DEFAULT_PLUGIN_PREFIXES);
081:
082: private Map components = new HashMap();
083:
084: public static MojoDescriptor createMojoDescriptor(
085: PluginDescriptor pd, String goal) {
086: MojoDescriptor md = new MojoDescriptor();
087: md.setPluginDescriptor(pd);
088: md.setGoal(goal);
089: pd.addComponentDescriptor(md);
090:
091: return md;
092: }
093:
094: public static PluginDescriptor createPluginDescriptor(
095: String artifactId, String goalPrefix, String groupId,
096: String version) {
097: PluginDescriptor pd = new PluginDescriptor();
098: pd.setGroupId(groupId);
099: pd.setArtifactId(artifactId);
100: pd.setGoalPrefix(goalPrefix);
101: pd.setVersion(version);
102:
103: return pd;
104: }
105:
106: public PluginDescriptor findPluginForPrefix(String prefix,
107: MavenProject project) throws PluginLoaderException {
108: // System.out.println( "Find plugin for prefix: " + prefix + " in project: " + project.getId() );
109:
110: return (PluginDescriptor) pluginPrefixes.get(prefix);
111: }
112:
113: public PluginDescriptor loadPlugin(Plugin plugin,
114: MavenProject project) throws PluginLoaderException {
115: // System.out.println( "Load plugin from model definition: " + plugin.getKey() + " in project: " + project.getId() );
116:
117: return (PluginDescriptor) pluginDescriptors
118: .get(plugin.getKey());
119: }
120:
121: public PluginDescriptor loadPlugin(MojoBinding mojoBinding,
122: MavenProject project) throws PluginLoaderException {
123: // System.out.println( "Load plugin for mojo binding: " + MojoBindingUtils.toString( mojoBinding )
124: // + " in project: " + project.getId() );
125:
126: return (PluginDescriptor) pluginDescriptors
127: .get(MojoBindingUtils.createPluginKey(mojoBinding));
128: }
129:
130: public Object loadPluginComponent(String role, String roleHint,
131: Plugin plugin, MavenProject project)
132: throws ComponentLookupException, PluginLoaderException {
133: // System.out.println( "Load plugin component: " + role + "/" + roleHint + " from plugin: " + plugin.getKey()
134: // + " in project: " + project.getId() );
135:
136: String key = createKey(role, roleHint, plugin.getGroupId(),
137: plugin.getArtifactId());
138: return components.get(key);
139: }
140:
141: private String createKey(String role, String roleHint,
142: String groupId, String artifactId) {
143: return groupId + ":" + artifactId + ":" + role + ":" + roleHint;
144: }
145:
146: public PluginDescriptor loadReportPlugin(ReportPlugin plugin,
147: MavenProject project) throws PluginLoaderException {
148: System.out.println("Load report plugin from model definition: "
149: + plugin.getKey() + " in project: " + project.getId());
150:
151: return (PluginDescriptor) pluginDescriptors
152: .get(plugin.getKey());
153: }
154:
155: public PluginDescriptor loadReportPlugin(MojoBinding mojoBinding,
156: MavenProject project) throws PluginLoaderException {
157: System.out.println("Load report plugin for mojo binding: "
158: + MojoBindingUtils.toString(mojoBinding)
159: + " in project: " + project.getId());
160:
161: return (PluginDescriptor) pluginDescriptors
162: .get(MojoBindingUtils.createPluginKey(mojoBinding));
163: }
164:
165: public void addPluginDescriptor(PluginDescriptor pd) {
166: pluginDescriptors.put(pd.getId(), pd);
167: pluginPrefixes.put(pd.getGoalPrefix(), pd);
168: }
169:
170: public void addPluginComponent(String groupId, String artifactId,
171: String role, String roleHint, Object component) {
172: components.put(createKey(role, roleHint, groupId, artifactId),
173: component);
174: }
175:
176: }
|