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