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