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.FileInputStream;
24 import java.io.IOException;
25
26 import org.apache.maven.index.ArtifactContext.ModelReader;
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
32
33
34
35
36
37 public class ArtifactLocator
38 implements GavHelpedLocator
39 {
40 private final ArtifactPackagingMapper mapper;
41
42 public ArtifactLocator( ArtifactPackagingMapper mapper )
43 {
44 this.mapper = mapper;
45 }
46
47 public File locate( File source, GavCalculator gavCalculator, Gav gav )
48 {
49
50 if ( source == null || !source.exists() || gav == null || gav.getArtifactId() == null
51 || gav.getVersion() == null )
52 {
53 return null;
54 }
55
56 try
57 {
58
59 Model model = new ModelReader().readModel( new FileInputStream( source ) );
60
61 if ( model == null )
62 {
63 return null;
64 }
65
66
67 String artifactName =
68 gav.getArtifactId() + "-" + gav.getVersion() + "."
69 + mapper.getExtensionForPackaging( model.getPackaging() );
70
71 File artifact = new File( source.getParent(), artifactName );
72
73 if ( !artifact.exists() )
74 {
75 return null;
76 }
77
78 return artifact;
79 }
80 catch ( IOException e )
81 {
82 return null;
83 }
84 }
85 }