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