1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index.locator;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.file.Files;
25
26 import org.apache.maven.index.artifact.ArtifactPackagingMapper;
27 import org.apache.maven.index.artifact.Gav;
28 import org.apache.maven.index.artifact.GavCalculator;
29 import org.apache.maven.model.Model;
30 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
31 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40 public class ArtifactLocator implements GavHelpedLocator {
41
42 private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactLocator.class);
43
44 private final ArtifactPackagingMapper mapper;
45
46 public ArtifactLocator(ArtifactPackagingMapper mapper) {
47 this.mapper = mapper;
48 }
49
50 public File locate(File source, GavCalculator gavCalculator, Gav gav) {
51
52 if (source == null
53 || !source.exists()
54 || gav == null
55 || gav.getArtifactId() == null
56 || gav.getVersion() == null) {
57 return null;
58 }
59
60 try (InputStream inputStream = Files.newInputStream(source.toPath())) {
61
62 final Model model = new MavenXpp3Reader().read(inputStream, false);
63
64 if (model == null) {
65 return null;
66 }
67
68
69 String artifactName = gav.getArtifactId() + "-" + gav.getVersion() + "."
70 + mapper.getExtensionForPackaging(model.getPackaging());
71
72 File artifact = new File(source.getParent(), artifactName);
73
74 if (!artifact.exists()) {
75 return null;
76 }
77
78 return artifact;
79 } catch (XmlPullParserException | IOException e) {
80 LOGGER.warn("skip error reading pom from file:" + source, e);
81 return null;
82 }
83 }
84 }