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.repository.internal;
20  
21  import java.io.Reader;
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  import org.apache.maven.repository.internal.PluginsMetadata.PluginInfo;
33  import org.codehaus.plexus.util.ReaderFactory;
34  import org.codehaus.plexus.util.xml.Xpp3Dom;
35  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
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) {
77                  it.remove();
78                  PluginsMetadata pluginMetadata = (PluginsMetadata) metadata;
79                  processedPlugins.put(pluginMetadata.getGroupId(), pluginMetadata);
80              }
81          }
82      }
83  
84      @Override
85      public Collection<? extends Metadata> prepare(Collection<? extends Artifact> artifacts) {
86          return Collections.emptyList();
87      }
88  
89      @Override
90      public Artifact transformArtifact(Artifact artifact) {
91          return artifact;
92      }
93  
94      @Override
95      public Collection<? extends Metadata> finish(Collection<? extends Artifact> artifacts) {
96          LinkedHashMap<String, PluginsMetadata> plugins = new LinkedHashMap<>();
97          for (Artifact artifact : artifacts) {
98              PluginInfo pluginInfo = extractPluginInfo(artifact);
99              if (pluginInfo != null) {
100                 String key = pluginInfo.groupId;
101                 if (processedPlugins.get(key) == null) {
102                     PluginsMetadata pluginMetadata = plugins.get(key);
103                     if (pluginMetadata == null) {
104                         pluginMetadata = new PluginsMetadata(pluginInfo, timestamp);
105                         plugins.put(key, pluginMetadata);
106                     }
107                 }
108             }
109         }
110         return plugins.values();
111     }
112 
113     private PluginInfo extractPluginInfo(Artifact artifact) {
114         // sanity: jar, no classifier and file exists
115         if (artifact != null
116                 && "jar".equals(artifact.getExtension())
117                 && "".equals(artifact.getClassifier())
118                 && artifact.getFile() != null) {
119             Path artifactPath = artifact.getFile().toPath();
120             if (Files.isRegularFile(artifactPath)) {
121                 try (JarFile artifactJar = new JarFile(artifactPath.toFile(), false)) {
122                     ZipEntry pluginDescriptorEntry = artifactJar.getEntry(PLUGIN_DESCRIPTOR_LOCATION);
123 
124                     if (pluginDescriptorEntry != null) {
125                         try (Reader reader =
126                                 ReaderFactory.newXmlReader(artifactJar.getInputStream(pluginDescriptorEntry))) {
127                             // Note: using DOM instead of use of
128                             // org.apache.maven.plugin.descriptor.PluginDescriptor
129                             // as it would pull in dependency on:
130                             // - maven-plugin-api (for model)
131                             // - Plexus Container (for model supporting classes and exceptions)
132                             Xpp3Dom root = Xpp3DomBuilder.build(reader);
133                             String groupId = root.getChild("groupId").getValue();
134                             String artifactId = root.getChild("artifactId").getValue();
135                             String goalPrefix = root.getChild("goalPrefix").getValue();
136                             String name = root.getChild("name").getValue();
137                             return new PluginInfo(groupId, artifactId, goalPrefix, name);
138                         }
139                     }
140                 } catch (Exception e) {
141                     // here we can have: IO. ZIP or Plexus Conf Ex: but we should not interfere with user intent
142                 }
143             }
144         }
145         return null;
146     }
147 }