1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.jar.identification.exposers;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.List;
28 import java.util.jar.JarEntry;
29
30 import org.apache.maven.model.Model;
31 import org.apache.maven.model.Organization;
32 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
33 import org.apache.maven.shared.jar.JarAnalyzer;
34 import org.apache.maven.shared.jar.identification.JarIdentification;
35 import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
36 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43 @Singleton
44 @Named("embeddedMavenModel")
45 public class EmbeddedMavenModelExposer implements JarIdentificationExposer {
46 private final Logger logger = LoggerFactory.getLogger(getClass());
47
48 @Override
49 public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) {
50 List<JarEntry> entries = jarAnalyzer.getMavenPomEntries();
51 if (entries.isEmpty()) {
52 return;
53 }
54
55 if (entries.size() > 1) {
56 logger.warn("More than one Maven model entry was found in the JAR, using only the first of: " + entries);
57 }
58
59 JarEntry pom = entries.get(0);
60 MavenXpp3Reader pomreader = new MavenXpp3Reader();
61 try (InputStream is = jarAnalyzer.getEntryInputStream(pom);
62 InputStreamReader isreader = new InputStreamReader(is)) {
63 Model model = pomreader.read(isreader);
64
65 if (model.getParent() != null) {
66
67 if (model.getGroupId() == null) {
68 identification.addAndSetGroupId(model.getParent().getGroupId());
69 }
70 if (model.getVersion() == null) {
71 identification.addAndSetVersion(model.getParent().getVersion());
72 }
73 }
74
75 identification.addAndSetGroupId(model.getGroupId());
76 identification.addAndSetArtifactId(model.getArtifactId());
77 identification.addAndSetVersion(model.getVersion());
78 identification.addAndSetName(model.getName());
79
80
81 if (model.getName() == null) {
82 identification.addAndSetName(model.getArtifactId());
83 }
84
85 Organization org = model.getOrganization();
86 if (org != null) {
87 identification.addAndSetVendor(org.getName());
88 }
89 } catch (IOException e) {
90 logger.error("Unable to read model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e);
91 } catch (XmlPullParserException e) {
92 logger.error("Unable to parse model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e);
93 }
94 }
95 }