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.maven.plugin.descriptor;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.ArtifactUtils;
36  import org.apache.maven.model.Plugin;
37  import org.apache.maven.plugin.lifecycle.Lifecycle;
38  import org.apache.maven.plugin.lifecycle.LifecycleConfiguration;
39  import org.apache.maven.plugin.lifecycle.io.xpp3.LifecycleMappingsXpp3Reader;
40  import org.codehaus.plexus.classworlds.realm.ClassRealm;
41  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
42  import org.codehaus.plexus.util.ReaderFactory;
43  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
44  
45  /**
46   * @author Jason van Zyl
47   */
48  public class PluginDescriptor extends ComponentSetDescriptor implements Cloneable {
49  
50      private static final String LIFECYCLE_DESCRIPTOR = "META-INF/maven/lifecycle.xml";
51  
52      private String groupId;
53  
54      private String artifactId;
55  
56      private String version;
57  
58      private String goalPrefix;
59  
60      private String source;
61  
62      private boolean inheritedByDefault = true;
63  
64      private List<Artifact> artifacts;
65  
66      private ClassRealm classRealm;
67  
68      // calculated on-demand.
69      private Map<String, Artifact> artifactMap;
70  
71      private Set<Artifact> introducedDependencyArtifacts;
72  
73      private String name;
74  
75      private String description;
76  
77      private String requiredMavenVersion;
78  
79      private Plugin plugin;
80  
81      private Artifact pluginArtifact;
82  
83      private Map<String, Lifecycle> lifecycleMappings;
84  
85      // ----------------------------------------------------------------------
86      //
87      // ----------------------------------------------------------------------
88  
89      @SuppressWarnings({"unchecked", "rawtypes"})
90      public List<MojoDescriptor> getMojos() {
91          return (List) getComponents();
92      }
93  
94      public void addMojo(MojoDescriptor mojoDescriptor) throws DuplicateMojoDescriptorException {
95          MojoDescriptor existing = null;
96          // this relies heavily on the equals() and hashCode() for ComponentDescriptor,
97          // which uses role:roleHint for identity...and roleHint == goalPrefix:goal.
98          // role does not vary for Mojos.
99          List<MojoDescriptor> mojos = getMojos();
100 
101         if (mojos != null && mojos.contains(mojoDescriptor)) {
102             int indexOf = mojos.indexOf(mojoDescriptor);
103 
104             existing = mojos.get(indexOf);
105         }
106 
107         if (existing != null) {
108             throw new DuplicateMojoDescriptorException(
109                     getGoalPrefix(),
110                     mojoDescriptor.getGoal(),
111                     existing.getImplementation(),
112                     mojoDescriptor.getImplementation());
113         } else {
114             addComponentDescriptor(mojoDescriptor);
115         }
116     }
117 
118     public String getGroupId() {
119         return groupId;
120     }
121 
122     public void setGroupId(String groupId) {
123         this.groupId = groupId;
124     }
125 
126     public String getArtifactId() {
127         return artifactId;
128     }
129 
130     public void setArtifactId(String artifactId) {
131         this.artifactId = artifactId;
132     }
133 
134     // ----------------------------------------------------------------------
135     // Dependencies
136     // ----------------------------------------------------------------------
137 
138     public static String constructPluginKey(String groupId, String artifactId, String version) {
139         return groupId + ":" + artifactId + ":" + version;
140     }
141 
142     public String getPluginLookupKey() {
143         return groupId + ":" + artifactId;
144     }
145 
146     public String getId() {
147         return constructPluginKey(groupId, artifactId, version);
148     }
149 
150     public static String getDefaultPluginArtifactId(String id) {
151         return "maven-" + id + "-plugin";
152     }
153 
154     public static String getDefaultPluginGroupId() {
155         return "org.apache.maven.plugins";
156     }
157 
158     /**
159      * Parse maven-...-plugin.
160      *
161      * TODO move to plugin-tools-api as a default only
162      */
163     public static String getGoalPrefixFromArtifactId(String artifactId) {
164         if ("maven-plugin-plugin".equals(artifactId)) {
165             return "plugin";
166         } else {
167             return artifactId.replaceAll("-?maven-?", "").replaceAll("-?plugin-?", "");
168         }
169     }
170 
171     public String getGoalPrefix() {
172         return goalPrefix;
173     }
174 
175     public void setGoalPrefix(String goalPrefix) {
176         this.goalPrefix = goalPrefix;
177     }
178 
179     public void setVersion(String version) {
180         this.version = version;
181     }
182 
183     public String getVersion() {
184         return version;
185     }
186 
187     public void setSource(String source) {
188         this.source = source;
189     }
190 
191     public String getSource() {
192         return source;
193     }
194 
195     public boolean isInheritedByDefault() {
196         return inheritedByDefault;
197     }
198 
199     public void setInheritedByDefault(boolean inheritedByDefault) {
200         this.inheritedByDefault = inheritedByDefault;
201     }
202 
203     /**
204      * Gets the artifacts that make up the plugin's class realm, excluding artifacts shadowed by the Maven core realm
205      * like {@code maven-project}.
206      *
207      * @return The plugin artifacts, never {@code null}.
208      */
209     public List<Artifact> getArtifacts() {
210         return artifacts;
211     }
212 
213     public void setArtifacts(List<Artifact> artifacts) {
214         this.artifacts = artifacts;
215 
216         // clear the calculated artifactMap
217         artifactMap = null;
218     }
219 
220     /**
221      * The map of artifacts accessible by the versionlessKey, i.e. groupId:artifactId
222      *
223      * @return a Map of artifacts, never {@code null}
224      * @see #getArtifacts()
225      */
226     public Map<String, Artifact> getArtifactMap() {
227         if (artifactMap == null) {
228             artifactMap = ArtifactUtils.artifactMapByVersionlessId(getArtifacts());
229         }
230 
231         return artifactMap;
232     }
233 
234     public boolean equals(Object object) {
235         if (this == object) {
236             return true;
237         }
238 
239         return object instanceof PluginDescriptor && getId().equals(((PluginDescriptor) object).getId());
240     }
241 
242     public int hashCode() {
243         return 10 + getId().hashCode();
244     }
245 
246     public MojoDescriptor getMojo(String goal) {
247         if (getMojos() == null) {
248             return null; // no mojo in this POM
249         }
250 
251         // TODO could we use a map? Maybe if the parent did that for components too, as this is too vulnerable to
252         // changes above not being propagated to the map
253         for (MojoDescriptor desc : getMojos()) {
254             if (goal.equals(desc.getGoal())) {
255                 return desc;
256             }
257         }
258         return null;
259     }
260 
261     public void setClassRealm(ClassRealm classRealm) {
262         this.classRealm = classRealm;
263     }
264 
265     public ClassRealm getClassRealm() {
266         return classRealm;
267     }
268 
269     public void setIntroducedDependencyArtifacts(Set<Artifact> introducedDependencyArtifacts) {
270         this.introducedDependencyArtifacts = introducedDependencyArtifacts;
271     }
272 
273     public Set<Artifact> getIntroducedDependencyArtifacts() {
274         return (introducedDependencyArtifacts != null)
275                 ? introducedDependencyArtifacts
276                 : Collections.<Artifact>emptySet();
277     }
278 
279     public void setName(String name) {
280         this.name = name;
281     }
282 
283     public String getName() {
284         return name;
285     }
286 
287     public void setDescription(String description) {
288         this.description = description;
289     }
290 
291     public String getDescription() {
292         return description;
293     }
294 
295     public void setRequiredMavenVersion(String requiredMavenVersion) {
296         this.requiredMavenVersion = requiredMavenVersion;
297     }
298 
299     public String getRequiredMavenVersion() {
300         return requiredMavenVersion;
301     }
302 
303     public void setPlugin(Plugin plugin) {
304         this.plugin = plugin;
305     }
306 
307     public Plugin getPlugin() {
308         return plugin;
309     }
310 
311     public Artifact getPluginArtifact() {
312         return pluginArtifact;
313     }
314 
315     public void setPluginArtifact(Artifact pluginArtifact) {
316         this.pluginArtifact = pluginArtifact;
317     }
318 
319     public Lifecycle getLifecycleMapping(String lifecycleId) throws IOException, XmlPullParserException {
320         if (lifecycleMappings == null) {
321             LifecycleConfiguration lifecycleConfiguration;
322 
323             try (Reader reader = ReaderFactory.newXmlReader(getDescriptorStream(LIFECYCLE_DESCRIPTOR))) {
324                 lifecycleConfiguration = new LifecycleMappingsXpp3Reader().read(reader);
325             }
326 
327             lifecycleMappings = new HashMap<>();
328 
329             for (Lifecycle lifecycle : lifecycleConfiguration.getLifecycles()) {
330                 lifecycleMappings.put(lifecycle.getId(), lifecycle);
331             }
332         }
333 
334         return lifecycleMappings.get(lifecycleId);
335     }
336 
337     private InputStream getDescriptorStream(String descriptor) throws IOException {
338         File pluginFile = (pluginArtifact != null) ? pluginArtifact.getFile() : null;
339         if (pluginFile == null) {
340             throw new IllegalStateException("plugin main artifact has not been resolved for " + getId());
341         }
342 
343         if (pluginFile.isFile()) {
344             try {
345                 return new URL("jar:" + pluginFile.toURI() + "!/" + descriptor).openStream();
346             } catch (MalformedURLException e) {
347                 throw new IllegalStateException(e);
348             }
349         } else {
350             return new FileInputStream(new File(pluginFile, descriptor));
351         }
352     }
353 
354     /**
355      * Creates a shallow copy of this plugin descriptor.
356      */
357     @Override
358     public PluginDescriptor clone() {
359         try {
360             return (PluginDescriptor) super.clone();
361         } catch (CloneNotSupportedException e) {
362             throw new UnsupportedOperationException(e);
363         }
364     }
365 }