2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.jetbrains
.idea
.maven
.project
;
19 import com
.intellij
.openapi
.util
.io
.FileUtil
;
20 import com
.intellij
.openapi
.util
.text
.StringUtil
;
21 import com
.intellij
.openapi
.vfs
.JarFileSystem
;
22 import com
.intellij
.openapi
.vfs
.VirtualFileManager
;
23 import org
.apache
.maven
.artifact
.Artifact
;
24 import org
.apache
.maven
.artifact
.handler
.ArtifactHandler
;
25 import org
.jetbrains
.annotations
.NotNull
;
26 import org
.jetbrains
.idea
.maven
.embedder
.CustomArtifact
;
27 import org
.jetbrains
.idea
.maven
.utils
.MavenConstants
;
30 import java
.io
.Serializable
;
31 import java
.text
.MessageFormat
;
32 import java
.util
.ArrayList
;
33 import java
.util
.List
;
35 import static org
.jetbrains
.idea
.maven
.project
.MavenId
.append
;
37 public class MavenArtifact
implements Serializable
{
38 private String myGroupId
;
39 private String myArtifactId
;
40 private String myVersion
;
41 private String myBaseVersion
;
42 private String myType
;
43 private String myClassifier
;
45 private String myScope
;
46 private boolean myOptional
;
48 private String myExtension
;
51 private boolean myResolved
;
52 private boolean myStubbed
;
54 private List
<String
> myTrail
;
56 protected MavenArtifact() {
59 public MavenArtifact(Artifact artifact
, File localRepository
) {
60 myGroupId
= artifact
.getGroupId();
61 myArtifactId
= artifact
.getArtifactId();
62 myVersion
= artifact
.getVersion();
63 myBaseVersion
= artifact
.getBaseVersion();
64 myType
= artifact
.getType();
65 myClassifier
= artifact
.getClassifier();
67 myScope
= artifact
.getScope();
68 myOptional
= artifact
.isOptional();
70 myExtension
= getExtension(artifact
);
72 myFile
= artifact
.getFile();
74 myFile
= new File(localRepository
, getRelativePath());
77 myResolved
= artifact
.isResolved();
78 myStubbed
= artifact
instanceof CustomArtifact
&& ((CustomArtifact
)artifact
).isStub();
80 List
<String
> originalTrail
= artifact
.getDependencyTrail();
81 myTrail
= originalTrail
== null || originalTrail
.isEmpty()
82 ?
new ArrayList
<String
>()
83 : new ArrayList
<String
>(originalTrail
.subList(1, originalTrail
.size()));
86 private String
getExtension(Artifact artifact
) {
87 ArtifactHandler handler
= artifact
.getArtifactHandler();
89 if (handler
!= null) result
= handler
.getExtension();
90 if (result
== null) result
= artifact
.getType();
94 public String
getGroupId() {
98 public String
getArtifactId() {
102 public String
getVersion() {
106 public MavenId
getMavenId() {
107 return new MavenId(myGroupId
, myArtifactId
, myVersion
);
110 public String
getType() {
114 public String
getClassifier() {
118 public String
getScope() {
122 public boolean isOptional() {
126 public boolean isExportable() {
127 if (myOptional
) return false;
128 return Artifact
.SCOPE_COMPILE
.equals(myScope
) || Artifact
.SCOPE_RUNTIME
.equals(myScope
);
131 public String
getExtension() {
135 public boolean isResolved() {
136 return myResolved
&& myFile
.exists() && !myStubbed
;
140 public File
getFile() {
144 public String
getPath() {
145 return FileUtil
.toSystemIndependentName(myFile
.getPath());
148 public String
getRelativePath() {
149 return getRelativePathForExtraArtifact(null);
152 public String
getRelativePathForExtraArtifact(String extraArtifactClassifier
) {
153 StringBuilder result
= new StringBuilder();
154 result
.append(myGroupId
.replace('.', '/'));
156 result
.append(myArtifactId
);
158 result
.append(myVersion
);
160 result
.append(myArtifactId
);
162 result
.append(myVersion
);
164 if (!StringUtil
.isEmptyOrSpaces(extraArtifactClassifier
)) {
165 if (MavenConstants
.TYPE_TEST_JAR
.equals(myType
)) {
166 result
.append("-test");
169 result
.append(extraArtifactClassifier
);
170 result
.append(".jar");
173 if (!StringUtil
.isEmptyOrSpaces(myClassifier
)) {
175 result
.append(myClassifier
);
178 result
.append(myExtension
);
180 return result
.toString();
183 public String
getUrl() {
184 return getUrlForExtraArtifact(null);
187 public String
getUrlForExtraArtifact(String extraArtifactClassifier
) {
188 String path
= getPath();
190 if (!StringUtil
.isEmptyOrSpaces(extraArtifactClassifier
)) {
191 int repoEnd
= path
.lastIndexOf(getRelativePath());
194 // unknown path format: try to add a classified at the end of the filename
195 int dotPos
= path
.lastIndexOf(".");
196 if (dotPos
!= -1) {// sometimes path doesn't contain '.'; but i can't find any reason why.
197 String withoutExtension
= path
.substring(0, dotPos
);
198 path
= MessageFormat
.format("{0}-{1}.jar", withoutExtension
, extraArtifactClassifier
);
201 String repoPath
= path
.substring(0, repoEnd
);
202 path
= repoPath
+ getRelativePathForExtraArtifact(extraArtifactClassifier
);
205 return VirtualFileManager
.constructUrl(JarFileSystem
.PROTOCOL
, path
) + JarFileSystem
.JAR_SEPARATOR
;
208 public List
<String
> getTrail() {
212 public String
getDisplayStringSimple() {
213 StringBuilder builder
= new StringBuilder();
215 append(builder
, myGroupId
);
216 append(builder
, myArtifactId
);
217 append(builder
, myVersion
);
219 return builder
.toString();
222 public String
getDisplayStringWithType() {
223 StringBuilder builder
= new StringBuilder();
225 append(builder
, myGroupId
);
226 append(builder
, myArtifactId
);
227 append(builder
, myType
);
228 append(builder
, myVersion
);
230 return builder
.toString();
233 public String
getDisplayStringWithTypeAndClassifier() {
234 StringBuilder builder
= new StringBuilder();
236 append(builder
, myGroupId
);
237 append(builder
, myArtifactId
);
238 append(builder
, myType
);
239 if (!StringUtil
.isEmptyOrSpaces(myClassifier
)) append(builder
, myClassifier
);
240 append(builder
, myVersion
);
242 return builder
.toString();
245 public String
getDisplayStringFull() {
246 StringBuilder builder
= new StringBuilder();
248 append(builder
, myGroupId
);
249 append(builder
, myArtifactId
);
250 append(builder
, myType
);
251 if (!StringUtil
.isEmptyOrSpaces(myClassifier
)) append(builder
, myClassifier
);
252 append(builder
, myVersion
);
253 if (!StringUtil
.isEmptyOrSpaces(myScope
)) append(builder
, myScope
);
255 return builder
.toString();
258 public String
getDisplayStringForLibraryName() {
259 StringBuilder builder
= new StringBuilder();
261 append(builder
, myGroupId
);
262 append(builder
, myArtifactId
);
264 if (!StringUtil
.isEmptyOrSpaces(myType
) && !MavenConstants
.TYPE_JAR
.equals(myType
)) append(builder
, myType
);
265 if (!StringUtil
.isEmptyOrSpaces(myClassifier
)) append(builder
, myClassifier
);
267 String version
= !StringUtil
.isEmptyOrSpaces(myBaseVersion
) ? myBaseVersion
: myVersion
;
268 if (!StringUtil
.isEmptyOrSpaces(version
)) append(builder
, version
);
270 return builder
.toString();
274 public String
toString() {
275 return getDisplayStringFull();
279 public boolean equals(Object o
) {
280 if (this == o
) return true;
281 if (o
== null || getClass() != o
.getClass()) return false;
283 MavenArtifact that
= (MavenArtifact
)o
;
285 if (myGroupId
!= null ?
!myGroupId
.equals(that
.myGroupId
) : that
.myGroupId
!= null) return false;
286 if (myArtifactId
!= null ?
!myArtifactId
.equals(that
.myArtifactId
) : that
.myArtifactId
!= null) return false;
287 if (myVersion
!= null ?
!myVersion
.equals(that
.myVersion
) : that
.myVersion
!= null) return false;
288 if (myBaseVersion
!= null ?
!myBaseVersion
.equals(that
.myBaseVersion
) : that
.myBaseVersion
!= null) return false;
289 if (myType
!= null ?
!myType
.equals(that
.myType
) : that
.myType
!= null) return false;
290 if (myClassifier
!= null ?
!myClassifier
.equals(that
.myClassifier
) : that
.myClassifier
!= null) return false;
291 if (myScope
!= null ?
!myScope
.equals(that
.myScope
) : that
.myScope
!= null) return false;
292 if (myExtension
!= null ?
!myExtension
.equals(that
.myExtension
) : that
.myExtension
!= null) return false;
293 if (myFile
!= null ?
!myFile
.equals(that
.myFile
) : that
.myFile
!= null) return false;
294 if (myTrail
!= null ?
!myTrail
.equals(that
.myTrail
) : that
.myTrail
!= null) return false;
300 public int hashCode() {
301 int result
= myGroupId
!= null ? myGroupId
.hashCode() : 0;
302 result
= 31 * result
+ (myArtifactId
!= null ? myArtifactId
.hashCode() : 0);
303 result
= 31 * result
+ (myVersion
!= null ? myVersion
.hashCode() : 0);
304 result
= 31 * result
+ (myBaseVersion
!= null ? myBaseVersion
.hashCode() : 0);
305 result
= 31 * result
+ (myType
!= null ? myType
.hashCode() : 0);
306 result
= 31 * result
+ (myClassifier
!= null ? myClassifier
.hashCode() : 0);
307 result
= 31 * result
+ (myScope
!= null ? myScope
.hashCode() : 0);
308 result
= 31 * result
+ (myExtension
!= null ? myExtension
.hashCode() : 0);
309 result
= 31 * result
+ (myFile
!= null ? myFile
.hashCode() : 0);
310 result
= 31 * result
+ (myTrail
!= null ? myTrail
.hashCode() : 0);