1 package org.apache.maven.shared.io.location;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.factory.ArtifactFactory;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
28 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
29 import org.apache.maven.artifact.resolver.ArtifactResolver;
30 import org.apache.maven.shared.io.logging.MessageHolder;
31
32 public class ArtifactLocatorStrategy
33 implements LocatorStrategy
34 {
35 private final ArtifactFactory factory;
36
37 private final ArtifactResolver resolver;
38
39 private String defaultArtifactType = "jar";
40
41 private final ArtifactRepository localRepository;
42
43 private final List remoteRepositories;
44
45 private String defaultClassifier = null;
46
47 public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
48 ArtifactRepository localRepository, List remoteRepositories )
49 {
50 this.factory = factory;
51 this.resolver = resolver;
52 this.localRepository = localRepository;
53 this.remoteRepositories = remoteRepositories;
54 }
55
56 public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
57 ArtifactRepository localRepository, List remoteRepositories,
58 String defaultArtifactType )
59 {
60 this.factory = factory;
61 this.resolver = resolver;
62 this.localRepository = localRepository;
63 this.remoteRepositories = remoteRepositories;
64 this.defaultArtifactType = defaultArtifactType;
65 }
66
67 public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
68 ArtifactRepository localRepository, List remoteRepositories,
69 String defaultArtifactType, String defaultClassifier )
70 {
71 this.factory = factory;
72 this.resolver = resolver;
73 this.localRepository = localRepository;
74 this.remoteRepositories = remoteRepositories;
75 this.defaultArtifactType = defaultArtifactType;
76 this.defaultClassifier = defaultClassifier;
77 }
78
79
80
81
82
83
84 public Location resolve( String locationSpecification, MessageHolder messageHolder )
85 {
86 String[] parts = locationSpecification.split( ":" );
87
88 Location location = null;
89
90 if ( parts.length > 2 )
91 {
92 String groupId = parts[0];
93 String artifactId = parts[1];
94 String version = parts[2];
95
96 String type = defaultArtifactType;
97 if ( parts.length > 3 )
98 {
99 if ( parts[3].trim().length() > 0 )
100 {
101 type = parts[3];
102 }
103 }
104
105 String classifier = defaultClassifier;
106 if ( parts.length > 4 )
107 {
108 classifier = parts[4];
109 }
110
111 if ( parts.length > 5 )
112 {
113 messageHolder.newMessage().append( "Location specification has unused tokens: \'" );
114
115 for ( int i = 5; i < parts.length; i++ )
116 {
117 messageHolder.append( ":" + parts[i] );
118 }
119 }
120
121 Artifact artifact;
122 if ( classifier == null )
123 {
124 artifact = factory.createArtifact( groupId, artifactId, version, null, type );
125 }
126 else
127 {
128 artifact = factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
129 }
130
131 try
132 {
133 resolver.resolve( artifact, remoteRepositories, localRepository );
134
135 if ( artifact.getFile() != null )
136 {
137 location = new ArtifactLocation( artifact, locationSpecification );
138 }
139 else
140 {
141 messageHolder.addMessage( "Supposedly resolved artifact: " + artifact.getId()
142 + " does not have an associated file." );
143 }
144 }
145 catch ( ArtifactResolutionException e )
146 {
147 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
148 + locationSpecification, e );
149 }
150 catch ( ArtifactNotFoundException e )
151 {
152 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
153 + locationSpecification, e );
154 }
155 }
156 else
157 {
158 messageHolder.addMessage( "Invalid artifact specification: \'" + locationSpecification
159 + "\'. Must contain at least three fields, separated by ':'." );
160 }
161
162 return location;
163 }
164
165 }