1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.impl.resolver;
20
21 import java.io.InputStream;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.time.Instant;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.jar.JarFile;
32 import java.util.zip.ZipEntry;
33
34 import org.apache.maven.api.Constants;
35 import org.apache.maven.api.MonotonicClock;
36 import org.apache.maven.api.xml.XmlNode;
37 import org.apache.maven.impl.resolver.PluginsMetadata.PluginInfo;
38 import org.apache.maven.internal.xml.XmlNodeStaxBuilder;
39 import org.eclipse.aether.RepositorySystemSession;
40 import org.eclipse.aether.artifact.Artifact;
41 import org.eclipse.aether.deployment.DeployRequest;
42 import org.eclipse.aether.impl.MetadataGenerator;
43 import org.eclipse.aether.installation.InstallRequest;
44 import org.eclipse.aether.metadata.Metadata;
45 import org.eclipse.aether.util.ConfigUtils;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52
53
54 class PluginsMetadataGenerator implements MetadataGenerator {
55 private static final String PLUGIN_DESCRIPTOR_LOCATION = "META-INF/maven/plugin.xml";
56
57 private final Logger logger = LoggerFactory.getLogger(getClass());
58
59 private final Map<Object, PluginsMetadata> processedPlugins;
60
61 private final Instant timestamp;
62
63 PluginsMetadataGenerator(RepositorySystemSession session, InstallRequest request) {
64 this(session, request.getMetadata());
65 }
66
67 PluginsMetadataGenerator(RepositorySystemSession session, DeployRequest request) {
68 this(session, request.getMetadata());
69 }
70
71 private PluginsMetadataGenerator(RepositorySystemSession session, Collection<? extends Metadata> metadatas) {
72 this.processedPlugins = new LinkedHashMap<>();
73 this.timestamp = (Instant) ConfigUtils.getObject(session, MonotonicClock.now(), Constants.MAVEN_START_INSTANT);
74
75
76
77
78
79
80
81 for (Iterator<? extends Metadata> it = metadatas.iterator(); it.hasNext(); ) {
82 Metadata metadata = it.next();
83 if (metadata instanceof PluginsMetadata pluginMetadata) {
84 it.remove();
85 processedPlugins.put(pluginMetadata.getGroupId(), pluginMetadata);
86 }
87 }
88 }
89
90 @Override
91 public Collection<? extends Metadata> prepare(Collection<? extends Artifact> artifacts) {
92 return Collections.emptyList();
93 }
94
95 @Override
96 public Artifact transformArtifact(Artifact artifact) {
97 return artifact;
98 }
99
100 @Override
101 public Collection<? extends Metadata> finish(Collection<? extends Artifact> artifacts) {
102 LinkedHashMap<String, PluginsMetadata> plugins = new LinkedHashMap<>();
103 for (Artifact artifact : artifacts) {
104 PluginInfo pluginInfo = extractPluginInfo(artifact);
105 if (pluginInfo != null) {
106 String key = pluginInfo.groupId;
107 if (processedPlugins.get(key) == null) {
108 PluginsMetadata pluginMetadata = plugins.get(key);
109 if (pluginMetadata == null) {
110 pluginMetadata = new PluginsMetadata(pluginInfo, timestamp);
111 plugins.put(key, pluginMetadata);
112 }
113 }
114 }
115 }
116 return plugins.values();
117 }
118
119 private PluginInfo extractPluginInfo(Artifact artifact) {
120
121 if (artifact != null
122 && "jar".equals(artifact.getExtension())
123 && "".equals(artifact.getClassifier())
124 && artifact.getPath() != null) {
125 Path artifactPath = artifact.getPath();
126 if (Files.isRegularFile(artifactPath)) {
127 try (JarFile artifactJar = new JarFile(artifactPath.toFile(), false)) {
128 ZipEntry pluginDescriptorEntry = artifactJar.getEntry(PLUGIN_DESCRIPTOR_LOCATION);
129
130 if (pluginDescriptorEntry != null) {
131 try (InputStream is = artifactJar.getInputStream(pluginDescriptorEntry)) {
132
133
134
135
136
137 XmlNode root = XmlNodeStaxBuilder.build(is, null);
138 String groupId = mayGetChild(root, "groupId");
139 String artifactId = mayGetChild(root, "artifactId");
140 String goalPrefix = mayGetChild(root, "goalPrefix");
141 String name = mayGetChild(root, "name");
142
143 if (Objects.equals(artifact.getGroupId(), groupId)
144 && Objects.equals(artifact.getArtifactId(), artifactId)) {
145
146 return new PluginInfo(groupId, artifactId, goalPrefix, name);
147 } else {
148 logger.warn(
149 "Artifact {}:{}"
150 + " JAR (about to be installed/deployed) contains Maven Plugin metadata for"
151 + " conflicting coordinates: {}:{}."
152 + " Your JAR contains rogue Maven Plugin metadata."
153 + " Possible causes may be: shaded into this JAR some Maven Plugin or some rogue resource.",
154 artifact.getGroupId(),
155 artifact.getArtifactId(),
156 groupId,
157 artifactId);
158 }
159 }
160 }
161 } catch (Exception e) {
162
163 }
164 }
165 }
166 return null;
167 }
168
169 private static String mayGetChild(XmlNode node, String child) {
170 XmlNode c = node.getChild(child);
171 if (c != null) {
172 return c.getValue();
173 }
174 return null;
175 }
176 }