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 691404 2008-09-02 21:57:19Z 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      * The ajdt weave dependency
109      */
110     private boolean ajdtWeaveDependency;
111 
112     /**
113      * The ajdt dependency.
114      */
115     private boolean ajdtDependency;
116 
117     /**
118      * Creates an uninitialized instance
119      */
120     public IdeDependency()
121     {
122     }
123 
124     /**
125      * @param groupId Group id
126      * @param artifactId Artifact id
127      * @param version Artifact version
128      * @param classifier Artifact classifier
129      * @param referencedProject Is this dependency available in the reactor?
130      * @param testDependency Is this a test dependency?
131      * @param systemScoped Is this a system scope dependency?
132      * @param provided Is this a provided dependency?
133      * @param addedToClasspath Is this dependency added to classpath?
134      * @param file Resolved artifact file
135      * @param type Artifact type
136      * @param osgiBundle Does this artifact contains a OSGI Manifest?
137      * @param osgiSymbolicName Bundle-SymbolicName from the Manifest (if available)
138      * @param dependencyDepth Depth of this dependency in the transitive dependency trail.
139      * @param eclipseProjectName The name of the project in eclipse
140      */
141     public IdeDependency( String groupId, String artifactId, String version, String classifier,
142                           boolean referencedProject, boolean testDependency, boolean systemScoped, boolean provided,
143                           boolean addedToClasspath, File file, String type, boolean osgiBundle,
144                           String osgiSymbolicName, int dependencyDepth, String eclipseProjectName )
145     {
146         // group:artifact:version
147         this.groupId = groupId;
148         this.artifactId = artifactId;
149         this.version = version;
150         this.classifier = classifier;
151 
152         // flags
153         this.referencedProject = referencedProject;
154         this.testDependency = testDependency;
155         this.systemScoped = systemScoped;
156         this.provided = provided;
157         this.addedToClasspath = addedToClasspath;
158 
159         // needed for OSGI support
160         this.osgiBundle = osgiBundle;
161         // file and type
162         this.file = file;
163         this.type = type;
164         this.eclipseProjectName = eclipseProjectName;
165     }
166 
167     /**
168      * Getter for <code>javadocAttachment</code>.
169      * 
170      * @return Returns the javadocAttachment.
171      */
172     public File getJavadocAttachment()
173     {
174         return javadocAttachment;
175     }
176 
177     /**
178      * Setter for <code>javadocAttachment</code>.
179      * 
180      * @param javadocAttachment The javadocAttachment to set.
181      */
182     public void setJavadocAttachment( File javadocAttachment )
183     {
184         this.javadocAttachment = javadocAttachment;
185     }
186 
187     /**
188      * Getter for <code>artifactId</code>.
189      * 
190      * @return Returns the artifactId.
191      */
192     public String getArtifactId()
193     {
194         return artifactId;
195     }
196 
197     /**
198      * Setter for <code>artifactId</code>.
199      * 
200      * @param artifactId The artifactId to set.
201      */
202     public void setArtifactId( String artifactId )
203     {
204         this.artifactId = artifactId;
205     }
206 
207     /**
208      * Getter for <code>groupId</code>.
209      * 
210      * @return Returns the groupId.
211      */
212     public String getGroupId()
213     {
214         return groupId;
215     }
216 
217     /**
218      * Setter for <code>groupId</code>.
219      * 
220      * @param groupId The groupId to set.
221      */
222     public void setGroupId( String groupId )
223     {
224         this.groupId = groupId;
225     }
226 
227     /**
228      * Getter for <code>version</code>.
229      * 
230      * @return Returns the version.
231      */
232     public String getVersion()
233     {
234         return version;
235     }
236 
237     /**
238      * Setter for <code>version</code>.
239      * 
240      * @param version The version to set.
241      */
242     public void setVersion( String version )
243     {
244         this.version = version;
245     }
246 
247     /**
248      * Getter for <code>classifier</code>.
249      * 
250      * @return Returns the classifier.
251      */
252     public String getClassifier()
253     {
254         return classifier;
255     }
256 
257     /**
258      * Setter for <code>groupId</code>.
259      * 
260      * @param groupId The groupId to set.
261      */
262     public void setClassifier( String classifier )
263     {
264         this.classifier = classifier;
265     }
266 
267     /**
268      * Getter for <code>referencedProject</code>.
269      * 
270      * @return Returns the referencedProject.
271      */
272     public boolean isReferencedProject()
273     {
274         return referencedProject;
275     }
276 
277     /**
278      * Getter for <code>osgiBundle</code>.
279      * 
280      * @return Returns the osgiBundle.
281      */
282     public boolean isOsgiBundle()
283     {
284         return osgiBundle;
285     }
286 
287     /**
288      * Setter for <code>referencedProject</code>.
289      * 
290      * @param referencedProject The referencedProject to set.
291      */
292     public void setReferencedProject( boolean referencedProject )
293     {
294         this.referencedProject = referencedProject;
295     }
296 
297     /**
298      * Getter for <code>sourceAttachment</code>.
299      * 
300      * @return Returns the sourceAttachment.
301      */
302     public File getSourceAttachment()
303     {
304         return sourceAttachment;
305     }
306 
307     /**
308      * Setter for <code>sourceAttachment</code>.
309      * 
310      * @param sourceAttachment The sourceAttachment to set.
311      */
312     public void setSourceAttachment( File sourceAttachment )
313     {
314         this.sourceAttachment = sourceAttachment;
315     }
316 
317     /**
318      * Getter for <code>systemScoped</code>.
319      * 
320      * @return Returns the systemScoped.
321      */
322     public boolean isSystemScoped()
323     {
324         return systemScoped;
325     }
326 
327     /**
328      * Setter for <code>systemScoped</code>.
329      * 
330      * @param systemScoped The systemScoped to set.
331      */
332     public void setSystemScoped( boolean systemScoped )
333     {
334         this.systemScoped = systemScoped;
335     }
336 
337     /**
338      * Getter for <code>testDependency</code>.
339      * 
340      * @return Returns the testDependency.
341      */
342     public boolean isTestDependency()
343     {
344         return testDependency;
345     }
346 
347     /**
348      * Setter for <code>testDependency</code>.
349      * 
350      * @param testDependency The testDependency to set.
351      */
352     public void setTestDependency( boolean testDependency )
353     {
354         this.testDependency = testDependency;
355     }
356 
357     /**
358      * Getter for <code>file</code>.
359      * 
360      * @return Returns the file.
361      */
362     public File getFile()
363     {
364         return file;
365     }
366 
367     /**
368      * Setter for <code>file</code>.
369      * 
370      * @param file The file to set.
371      */
372     public void setFile( File file )
373     {
374         this.file = file;
375     }
376 
377     /**
378      * Getter for <code>artifactId</code>.
379      * 
380      * @return Returns the artifactId.
381      */
382     public String getId()
383     {
384         return groupId + ':' + artifactId + ':' + version;
385     }
386 
387     /**
388      * Getter for <code>type</code>.
389      * 
390      * @return Returns the type.
391      */
392     public String getType()
393     {
394         return type;
395     }
396 
397     /**
398      * Setter for <code>type</code>.
399      * 
400      * @param type The type to set.
401      */
402     public void setType( String type )
403     {
404         this.type = type;
405     }
406 
407     /**
408      * Getter for <code>addedToClasspath</code>.
409      * 
410      * @return Returns the addedToClasspath.
411      */
412     public boolean isAddedToClasspath()
413     {
414         return addedToClasspath;
415     }
416 
417     /**
418      * Setter for <code>addedToClasspath</code>.
419      * 
420      * @param addedToClasspath The addedToClasspath to set.
421      */
422     public void setAddedToClasspath( boolean addedToClasspath )
423     {
424         this.addedToClasspath = addedToClasspath;
425     }
426 
427     /**
428      * Getter for <code>provided</code>.
429      * 
430      * @return Returns the provided.
431      */
432     public boolean isProvided()
433     {
434         return provided;
435     }
436 
437     /**
438      * Setter for <code>provided</code>.
439      * 
440      * @param provided The provided to set.
441      */
442     public void setProvided( boolean provided )
443     {
444         this.provided = provided;
445     }
446 
447     /**
448      * Getter for <code>eclipseProjectName</code>.
449      * 
450      * @return Returns the eclipseProjectName.
451      */
452     public String getEclipseProjectName()
453     {
454         return eclipseProjectName;
455     }
456 
457     /**
458      * Setter for <code>eclipseProjectName</code>.
459      * 
460      * @param eclipseProjectName The eclipseProjectName to set.
461      */
462     public void setEclipseProjectName( String eclipseProjectName )
463     {
464         this.eclipseProjectName = eclipseProjectName;
465     }
466 
467     /**
468      * Returns the ajdtWeaveDependency.
469      * 
470      * @return the ajdtWeaveDependency.
471      */
472     public boolean isAjdtWeaveDependency()
473     {
474         return ajdtWeaveDependency;
475     }
476 
477     /**
478      * Sets the ajdtWeaveDependency.
479      * 
480      * @param ajdtWeaveDependency the ajdtWeaveDependency.
481      */
482     public void setAjdtWeaveDependency( boolean ajdtWeaveDependency )
483     {
484         this.ajdtWeaveDependency = ajdtWeaveDependency;
485     }
486 
487     /**
488      * Returns the ajdtDependency.
489      * 
490      * @return the ajdtDependency.
491      */
492     public boolean isAjdtDependency()
493     {
494         return ajdtDependency;
495     }
496 
497     /**
498      * Sets the ajdtDependency.
499      * 
500      * @param ajdtDependency the ajdtDependency.
501      */
502     public void setAjdtDependency( boolean ajdtDependency )
503     {
504         this.ajdtDependency = ajdtDependency;
505     }
506 
507     /**
508      * @see java.lang.Object#toString()
509      */
510     public String toString()
511     {
512         return getId();
513     }
514 
515     /**
516      * @see java.lang.Comparable#compareTo(java.lang.Object) Compare using groupId+artifactId+type+classifier Strings
517      */
518     public int compareTo( Object o )
519     {
520         IdeDependency dep = (IdeDependency) o;
521         // in case of system scoped dependencies the files must be compared.
522         if ( isSystemScoped() && dep.isSystemScoped() && getFile().equals( dep.getFile() ) )
523         {
524             return 0;
525         }
526         int equals = this.getGroupId().compareTo( dep.getGroupId() );
527         if ( equals != 0 )
528         {
529             return equals;
530         }
531         equals = this.getArtifactId().compareTo( dep.getArtifactId() );
532         if ( equals != 0 )
533         {
534             return equals;
535         }
536         equals = this.getType().compareTo( dep.getType() );
537         if ( equals != 0 )
538         {
539             return equals;
540         }
541         if ( this.getClassifier() != null && dep.getClassifier() != null )
542         {
543             equals = this.getClassifier().compareTo( dep.getClassifier() );
544         }
545         else if ( this.getClassifier() != null && dep.getClassifier() == null )
546         {
547             return 1;
548         }
549         else if ( this.getClassifier() == null && dep.getClassifier() != null )
550         {
551             return -1;
552         }
553         if ( equals != 0 )
554         {
555             return equals;
556         }
557         return 0;
558     }
559 
560     /**
561      * Is this dependency System scoped outside the eclipse project. This is NOT complete because in reality the check
562      * should mean that any module in the reactor contains the system scope locally!
563      * 
564      * @return Returns this dependency is systemScoped outside the project.
565      */
566     public boolean isSystemScopedOutsideProject( MavenProject project )
567     {
568         File modulesTop = project.getBasedir();
569         while ( new File( modulesTop.getParentFile(), "pom.xml" ).exists() )
570         {
571             modulesTop = modulesTop.getParentFile();
572         }
573         return isSystemScoped() && !getFile().getAbsolutePath().startsWith( modulesTop.getAbsolutePath() );
574     }
575 
576     /**
577      * @return <tt>true</tt> if this dependency is a Java API
578      */
579     public boolean isJavaApi()
580     {
581         return groupId.startsWith( "java." ) || groupId.startsWith( "javax." );
582     }
583 
584     /**
585      * {@inheritDoc}
586      */
587     public boolean equals( Object obj )
588     {
589         return compareTo( obj ) == 0;
590     }
591 
592     /**
593      * {@inheritDoc}
594      */
595     public int hashCode()
596     {
597         if ( isSystemScoped() )
598         {
599             return getFile().hashCode();
600         }
601         else
602         {
603             int hashCode = this.getGroupId().hashCode() ^ this.getArtifactId().hashCode() ^ this.getType().hashCode();
604             if ( this.getClassifier() == null )
605             {
606                 return hashCode;
607             }
608             else
609             {
610                 return hashCode ^ this.getClassifier().hashCode();
611             }
612 
613         }
614     }
615 }