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.internal.impl.resolver;
20  
21  import java.io.InputStream;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Date;
27  import java.util.Iterator;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  import java.util.jar.JarFile;
31  import java.util.zip.ZipEntry;
32  
33  import org.apache.maven.api.xml.XmlNode;
34  import org.apache.maven.internal.impl.resolver.PluginsMetadata.PluginInfo;
35  import org.apache.maven.internal.xml.XmlNodeStaxBuilder;
36  import org.eclipse.aether.RepositorySystemSession;
37  import org.eclipse.aether.artifact.Artifact;
38  import org.eclipse.aether.deployment.DeployRequest;
39  import org.eclipse.aether.impl.MetadataGenerator;
40  import org.eclipse.aether.installation.InstallRequest;
41  import org.eclipse.aether.metadata.Metadata;
42  import org.eclipse.aether.util.ConfigUtils;
43  
44  /**
45   * Maven G level metadata generator.
46   * <p>
47   * Plugin metadata contains G level list of "prefix" to A mapping for plugins present under this G.
48   */
49  class PluginsMetadataGenerator implements MetadataGenerator {
50      private static final String PLUGIN_DESCRIPTOR_LOCATION = "META-INF/maven/plugin.xml";
51  
52      private final Map<Object, PluginsMetadata> processedPlugins;
53  
54      private final Date timestamp;
55  
56      PluginsMetadataGenerator(RepositorySystemSession session, InstallRequest request) {
57          this(session, request.getMetadata());
58      }
59  
60      PluginsMetadataGenerator(RepositorySystemSession session, DeployRequest request) {
61          this(session, request.getMetadata());
62      }
63  
64      private PluginsMetadataGenerator(RepositorySystemSession session, Collection<? extends Metadata> metadatas) {
65          this.processedPlugins = new LinkedHashMap<>();
66          this.timestamp = (Date) ConfigUtils.getObject(session, new Date(), "maven.startTime");
67  
68          /*
69           * NOTE: This should be considered a quirk to support interop with Maven's legacy ArtifactDeployer which
70           * processes one artifact at a time and hence cannot associate the artifacts from the same project to use the
71           * same version index. Allowing the caller to pass in metadata from a previous deployment allows to re-establish
72           * the association between the artifacts of the same project.
73           */
74          for (Iterator<? extends Metadata> it = metadatas.iterator(); it.hasNext(); ) {
75              Metadata metadata = it.next();
76              if (metadata instanceof PluginsMetadata pluginMetadata) {
77                  it.remove();
78                  processedPlugins.put(pluginMetadata.getGroupId(), pluginMetadata);
79              }
80          }
81      }
82  
83      @Override
84      public Collection<? extends Metadata> prepare(Collection<? extends Artifact> artifacts) {
85          return Collections.emptyList();
86      }
87  
88      @Override
89      public Artifact transformArtifact(Artifact artifact) {
90          return artifact;
91      }
92  
93      @Override
94      public Collection<? extends Metadata> finish(Collection<? extends Artifact> artifacts) {
95          LinkedHashMap<String, PluginsMetadata> plugins = new LinkedHashMap<>();
96          for (Artifact artifact : artifacts) {
97              PluginInfo pluginInfo = extractPluginInfo(artifact);
98              if (pluginInfo != null) {
99                  String key = pluginInfo.groupId;
100                 if (processedPlugins.get(key) == null) {
101                     PluginsMetadata pluginMetadata = plugins.get(key);
102                     if (pluginMetadata == null) {
103                         pluginMetadata = new PluginsMetadata(pluginInfo, timestamp);
104                         plugins.put(key, pluginMetadata);
105                     }
106                 }
107             }
108         }
109         return plugins.values();
110     }
111 
112     private PluginInfo extractPluginInfo(Artifact artifact) {
113         // sanity: jar, no classifier and file exists
114         if (artifact != null
115                 && "jar".equals(artifact.getExtension())
116                 && "".equals(artifact.getClassifier())
117                 && artifact.getPath() != null) {
118             Path artifactPath = artifact.getPath();
119             if (Files.isRegularFile(artifactPath)) {
120                 try (JarFile artifactJar = new JarFile(artifactPath.toFile(), false)) {
121                     ZipEntry pluginDescriptorEntry = artifactJar.getEntry(PLUGIN_DESCRIPTOR_LOCATION);
122 
123                     if (pluginDescriptorEntry != null) {
124                         try (InputStream is = artifactJar.getInputStream(pluginDescriptorEntry)) {
125                             // Note: using DOM instead of use of
126                             // org.apache.maven.plugin.descriptor.PluginDescriptor
127                             // as it would pull in dependency on:
128                             // - maven-plugin-api (for model)
129                             // - Plexus Container (for model supporting classes and exceptions)
130                             XmlNode root = XmlNodeStaxBuilder.build(is, null);
131                             String groupId = root.getChild("groupId").getValue();
132                             String artifactId = root.getChild("artifactId").getValue();
133                             String goalPrefix = root.getChild("goalPrefix").getValue();
134                             String name = root.getChild("name").getValue();
135                             return new PluginInfo(groupId, artifactId, goalPrefix, name);
136                         }
137                     }
138                 } catch (Exception e) {
139                     // here we can have: IO. ZIP or Plexus Conf Ex: but we should not interfere with user intent
140                 }
141             }
142         }
143         return null;
144     }
145 }