View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.ide;
20  
21  import java.io.File;
22  
23  import org.apache.maven.project.MavenProject;
24  
25  /**
26   * @author Fabrizio Giustina
27   * @version $Id: IdeDependency.java 616964 2008-01-31 00:04:15Z aheritier $
28   */
29  public class IdeDependency
30      implements Comparable
31  {
32      /**
33       * Is this dependency available in the reactor?
34       */
35      private boolean referencedProject;
36  
37      /**
38       * Is this a test dependency?
39       */
40      private boolean testDependency;
41  
42      /**
43       * Is this a system scope dependency?
44       */
45      private boolean systemScoped;
46  
47      /**
48       * Is this a provided dependency?
49       */
50      private boolean provided;
51  
52      /**
53       * Is this dependency added to classpath?
54       */
55      private boolean addedToClasspath;
56  
57      /**
58       * Resolved artifact file.
59       */
60      private File file;
61  
62      /**
63       * Resolved javadoc file.
64       */
65      private File javadocAttachment;
66  
67      /**
68       * Resolved source file.
69       */
70      private File sourceAttachment;
71  
72      /**
73       * Group id.
74       */
75      private String groupId;
76  
77      /**
78       * Artifact id.
79       */
80      private String artifactId;
81  
82      /**
83       * Artifact version.
84       */
85      private String version;
86  
87      /**
88       * Artifact classifier
89       */
90      private String classifier;
91  
92      /**
93       * Artifact type.
94       */
95      private String type;
96  
97      /**
98       * Does this artifact contains a OSGI Manifest?
99       */
100     private boolean osgiBundle;
101 
102     /**
103      * How is this dependency called when it is an eclipse project.
104      */
105     private String eclipseProjectName;
106 
107     /**
108      * Creates an uninitialized instance
109      */
110     public IdeDependency()
111     {
112     }
113 
114     /**
115      * @param groupId Group id
116      * @param artifactId Artifact id
117      * @param version Artifact version
118      * @param classifier Artifact classifier
119      * @param referencedProject Is this dependency available in the reactor?
120      * @param testDependency Is this a test dependency?
121      * @param systemScoped Is this a system scope dependency?
122      * @param provided Is this a provided dependency?
123      * @param addedToClasspath Is this dependency added to classpath?
124      * @param file Resolved artifact file
125      * @param type Artifact type
126      * @param osgiBundle Does this artifact contains a OSGI Manifest?
127      * @param osgiSymbolicName Bundle-SymbolicName from the Manifest (if available)
128      * @param dependencyDepth Depth of this dependency in the transitive dependency trail.
129      * @param eclipseProjectName The name of the project in eclipse
130      */
131     public IdeDependency( String groupId, String artifactId, String version, String classifier,
132                           boolean referencedProject, boolean testDependency, boolean systemScoped, boolean provided,
133                           boolean addedToClasspath, File file, String type, boolean osgiBundle,
134                           String osgiSymbolicName, int dependencyDepth, String eclipseProjectName )
135     {
136         // group:artifact:version
137         this.groupId = groupId;
138         this.artifactId = artifactId;
139         this.version = version;
140         this.classifier = classifier;
141 
142         // flags
143         this.referencedProject = referencedProject;
144         this.testDependency = testDependency;
145         this.systemScoped = systemScoped;
146         this.provided = provided;
147         this.addedToClasspath = addedToClasspath;
148 
149         // needed for OSGI support
150         this.osgiBundle = osgiBundle;
151         // file and type
152         this.file = file;
153         this.type = type;
154         this.eclipseProjectName = eclipseProjectName;
155     }
156 
157     /**
158      * Getter for <code>javadocAttachment</code>.
159      * 
160      * @return Returns the javadocAttachment.
161      */
162     public File getJavadocAttachment()
163     {
164         return javadocAttachment;
165     }
166 
167     /**
168      * Setter for <code>javadocAttachment</code>.
169      * 
170      * @param javadocAttachment The javadocAttachment to set.
171      */
172     public void setJavadocAttachment( File javadocAttachment )
173     {
174         this.javadocAttachment = javadocAttachment;
175     }
176 
177     /**
178      * Getter for <code>artifactId</code>.
179      * 
180      * @return Returns the artifactId.
181      */
182     public String getArtifactId()
183     {
184         return artifactId;
185     }
186 
187     /**
188      * Setter for <code>artifactId</code>.
189      * 
190      * @param artifactId The artifactId to set.
191      */
192     public void setArtifactId( String artifactId )
193     {
194         this.artifactId = artifactId;
195     }
196 
197     /**
198      * Getter for <code>groupId</code>.
199      * 
200      * @return Returns the groupId.
201      */
202     public String getGroupId()
203     {
204         return groupId;
205     }
206 
207     /**
208      * Setter for <code>groupId</code>.
209      * 
210      * @param groupId The groupId to set.
211      */
212     public void setGroupId( String groupId )
213     {
214         this.groupId = groupId;
215     }
216 
217     /**
218      * Getter for <code>version</code>.
219      * 
220      * @return Returns the version.
221      */
222     public String getVersion()
223     {
224         return version;
225     }
226 
227     /**
228      * Setter for <code>version</code>.
229      * 
230      * @param version The version to set.
231      */
232     public void setVersion( String version )
233     {
234         this.version = version;
235     }
236 
237     /**
238      * Getter for <code>classifier</code>.
239      * 
240      * @return Returns the classifier.
241      */
242     public String getClassifier()
243     {
244         return classifier;
245     }
246 
247     /**
248      * Setter for <code>groupId</code>.
249      * 
250      * @param groupId The groupId to set.
251      */
252     public void setClassifier( String classifier )
253     {
254         this.classifier = classifier;
255     }
256 
257     /**
258      * Getter for <code>referencedProject</code>.
259      * 
260      * @return Returns the referencedProject.
261      */
262     public boolean isReferencedProject()
263     {
264         return referencedProject;
265     }
266 
267     /**
268      * Getter for <code>osgiBundle</code>.
269      * 
270      * @return Returns the osgiBundle.
271      */
272     public boolean isOsgiBundle()
273     {
274         return osgiBundle;
275     }
276 
277     /**
278      * Setter for <code>referencedProject</code>.
279      * 
280      * @param referencedProject The referencedProject to set.
281      */
282     public void setReferencedProject( boolean referencedProject )
283     {
284         this.referencedProject = referencedProject;
285     }
286 
287     /**
288      * Getter for <code>sourceAttachment</code>.
289      * 
290      * @return Returns the sourceAttachment.
291      */
292     public File getSourceAttachment()
293     {
294         return sourceAttachment;
295     }
296 
297     /**
298      * Setter for <code>sourceAttachment</code>.
299      * 
300      * @param sourceAttachment The sourceAttachment to set.
301      */
302     public void setSourceAttachment( File sourceAttachment )
303     {
304         this.sourceAttachment = sourceAttachment;
305     }
306 
307     /**
308      * Getter for <code>systemScoped</code>.
309      * 
310      * @return Returns the systemScoped.
311      */
312     public boolean isSystemScoped()
313     {
314         return systemScoped;
315     }
316 
317     /**
318      * Setter for <code>systemScoped</code>.
319      * 
320      * @param systemScoped The systemScoped to set.
321      */
322     public void setSystemScoped( boolean systemScoped )
323     {
324         this.systemScoped = systemScoped;
325     }
326 
327     /**
328      * Getter for <code>testDependency</code>.
329      * 
330      * @return Returns the testDependency.
331      */
332     public boolean isTestDependency()
333     {
334         return testDependency;
335     }
336 
337     /**
338      * Setter for <code>testDependency</code>.
339      * 
340      * @param testDependency The testDependency to set.
341      */
342     public void setTestDependency( boolean testDependency )
343     {
344         this.testDependency = testDependency;
345     }
346 
347     /**
348      * Getter for <code>file</code>.
349      * 
350      * @return Returns the file.
351      */
352     public File getFile()
353     {
354         return file;
355     }
356 
357     /**
358      * Setter for <code>file</code>.
359      * 
360      * @param file The file to set.
361      */
362     public void setFile( File file )
363     {
364         this.file = file;
365     }
366 
367     /**
368      * Getter for <code>artifactId</code>.
369      * 
370      * @return Returns the artifactId.
371      */
372     public String getId()
373     {
374         return groupId + ':' + artifactId + ':' + version;
375     }
376 
377     /**
378      * Getter for <code>type</code>.
379      * 
380      * @return Returns the type.
381      */
382     public String getType()
383     {
384         return type;
385     }
386 
387     /**
388      * Setter for <code>type</code>.
389      * 
390      * @param type The type to set.
391      */
392     public void setType( String type )
393     {
394         this.type = type;
395     }
396 
397     /**
398      * Getter for <code>addedToClasspath</code>.
399      * 
400      * @return Returns the addedToClasspath.
401      */
402     public boolean isAddedToClasspath()
403     {
404         return addedToClasspath;
405     }
406 
407     /**
408      * Setter for <code>addedToClasspath</code>.
409      * 
410      * @param addedToClasspath The addedToClasspath to set.
411      */
412     public void setAddedToClasspath( boolean addedToClasspath )
413     {
414         this.addedToClasspath = addedToClasspath;
415     }
416 
417     /**
418      * Getter for <code>provided</code>.
419      * 
420      * @return Returns the provided.
421      */
422     public boolean isProvided()
423     {
424         return provided;
425     }
426 
427     /**
428      * Setter for <code>provided</code>.
429      * 
430      * @param provided The provided to set.
431      */
432     public void setProvided( boolean provided )
433     {
434         this.provided = provided;
435     }
436 
437     /**
438      * Getter for <code>eclipseProjectName</code>.
439      * 
440      * @return Returns the eclipseProjectName.
441      */
442     public String getEclipseProjectName()
443     {
444         return eclipseProjectName;
445     }
446 
447     /**
448      * Setter for <code>eclipseProjectName</code>.
449      * 
450      * @param eclipseProjectName The eclipseProjectName to set.
451      */
452     public void setEclipseProjectName( String eclipseProjectName )
453     {
454         this.eclipseProjectName = eclipseProjectName;
455     }
456 
457     /**
458      * @see java.lang.Object#toString()
459      */
460     public String toString()
461     {
462         return getId();
463     }
464 
465     /**
466      * @see java.lang.Comparable#compareTo(java.lang.Object) Compare using groupId+artifactId+type+classifier Strings
467      */
468     public int compareTo( Object o )
469     {
470         IdeDependency dep = (IdeDependency) o;
471         // in case of system scoped dependencies the files must be compared.
472         if ( isSystemScoped() && dep.isSystemScoped() && getFile().equals( dep.getFile() ) )
473         {
474             return 0;
475         }
476         int equals = this.getGroupId().compareTo( dep.getGroupId() );
477         if ( equals != 0 )
478         {
479             return equals;
480         }
481         equals = this.getArtifactId().compareTo( dep.getArtifactId() );
482         if ( equals != 0 )
483         {
484             return equals;
485         }
486         equals = this.getType().compareTo( dep.getType() );
487         if ( equals != 0 )
488         {
489             return equals;
490         }
491         if ( this.getClassifier() != null && dep.getClassifier() != null )
492         {
493             equals = this.getClassifier().compareTo( dep.getClassifier() );
494         }
495         else if ( this.getClassifier() != null && dep.getClassifier() == null )
496         {
497             return 1;
498         }
499         else if ( this.getClassifier() == null && dep.getClassifier() != null )
500         {
501             return -1;
502         }
503         if ( equals != 0 )
504         {
505             return equals;
506         }
507         return 0;
508     }
509 
510     /**
511      * Is this dependency System scoped outside the eclipse project. This is NOT complete because in reality the check
512      * should mean that any module in the reactor contains the system scope locally!
513      * 
514      * @return Returns this dependency is systemScoped outside the project.
515      */
516     public boolean isSystemScopedOutsideProject( MavenProject project )
517     {
518         File modulesTop = project.getBasedir();
519         while ( new File( modulesTop.getParentFile(), "pom.xml" ).exists() )
520         {
521             modulesTop = modulesTop.getParentFile();
522         }
523         return isSystemScoped() && !getFile().getAbsolutePath().startsWith( modulesTop.getAbsolutePath() );
524     }
525 
526     /**
527      * {@inheritDoc}
528      */
529     public boolean equals( Object obj )
530     {
531         return compareTo( obj ) == 0;
532     }
533 
534     /**
535      * {@inheritDoc}
536      */
537     public int hashCode()
538     {
539         if ( isSystemScoped() )
540         {
541             return getFile().hashCode();
542         }
543         else
544         {
545             int hashCode = this.getGroupId().hashCode() ^ this.getArtifactId().hashCode() ^ this.getType().hashCode();
546             if ( this.getClassifier() == null )
547             {
548                 return hashCode;
549             }
550             else
551             {
552                 return hashCode ^ this.getClassifier().hashCode();
553             }
554 
555         }
556     }
557 }