1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
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
70
71
72
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
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
128
129
130
131
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
142 }
143 }
144 }
145 return null;
146 }
147 }