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