View Javadoc

1   /*
2    * $Id$
3    */
4   
5   package org.apache.maven.model;
6   
7     //---------------------------------/
8    //- Imported classes and packages -/
9   //---------------------------------/
10  
11  import java.util.Date;
12  
13  /**
14   * 
15   *         
16   *         The <code>&lt;dependency&gt;</code> element contains
17   * information about a dependency
18   *         of the project.
19   *         
20   *       
21   * 
22   * @version $Revision$ $Date$
23   */
24  public class Dependency implements java.io.Serializable {
25  
26  
27        //--------------------------/
28       //- Class/Member Variables -/
29      //--------------------------/
30  
31      /**
32       * 
33       *             
34       *             The project group that produced the dependency,
35       * e.g.
36       *             <code>org.apache.maven</code>.
37       *             
38       *           
39       */
40      private String groupId;
41  
42      /**
43       * 
44       *             
45       *             The unique id for an artifact produced by the
46       * project group, e.g.
47       *             <code>maven-artifact</code>.
48       *             
49       *           
50       */
51      private String artifactId;
52  
53      /**
54       * 
55       *             
56       *             The version of the dependency, e.g.
57       * <code>3.2.1</code>. In Maven 2, this can also be
58       *             specified as a range of versions.
59       *             
60       *           
61       */
62      private String version;
63  
64      /**
65       * 
66       *             
67       *             The type of dependency. This defaults to
68       * <code>jar</code>. While it
69       *             usually represents the extension on the filename
70       * of the dependency,
71       *             that is not always the case. A type can be
72       * mapped to a different
73       *             extension and a classifier.
74       *             The type often correspongs to the packaging
75       * used, though this is also
76       *             not always the case.
77       *             Some examples are <code>jar</code>,
78       * <code>war</code>, <code>ejb-client</code>
79       *             and <code>test-jar</code>.
80       *             New types can be defined by plugins that set
81       *             <code>extensions</code> to <code>true</code>, so
82       * this is not a complete list.
83       *             
84       *           
85       */
86      private String type = "jar";
87  
88      /**
89       * 
90       *             
91       *             The classifier of the dependency. This allows
92       * distinguishing two artifacts
93       *             that belong to the same POM but were built
94       * differently, and is appended to
95       *             the filename after the version. For example,
96       * <code>jdk14</code> and <code>jdk15</code>.
97       *             
98       *           
99       */
100     private String classifier;
101 
102     /**
103      * 
104      *             
105      *             The scope of the dependency -
106      * <code>compile</code>, <code>runtime</code>,
107      *             <code>test</code>, <code>system</code>, and
108      * <code>provided</code>. Used to
109      *             calculate the various classpaths used for
110      * compilation, testing, and so on.
111      *             It also assists in determining which artifacts
112      * to include in a distribution of
113      *             this project. For more information, see
114      *             <a
115      * href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the
116      *             dependency mechanism</a>.
117      *             
118      *           
119      */
120     private String scope;
121 
122     /**
123      * 
124      *             
125      *             FOR SYSTEM SCOPE ONLY. Note that use of this
126      * property is <b>discouraged</b>
127      *             and may be replaced in later versions. This
128      * specifies the path on the filesystem
129      *             for this dependency.
130      *             Requires an absolute path for the value, not
131      * relative.
132      *             Use a property that gives the machine specific
133      * absolute path,
134      *             e.g. <code>${java.home}</code>.
135      *             
136      *           
137      */
138     private String systemPath;
139 
140     /**
141      * Field exclusions.
142      */
143     private java.util.List exclusions;
144 
145     /**
146      * Indicates the dependency is optional for use of this
147      * library. While the
148      *             version of the dependency will be taken into
149      * account for dependency calculation if the
150      *             library is used elsewhere, it will not be passed
151      * on transitively.
152      */
153     private boolean optional = false;
154 
155 
156       //-----------/
157      //- Methods -/
158     //-----------/
159 
160     /**
161      * Method addExclusion.
162      * 
163      * @param exclusion
164      */
165     public void addExclusion( Exclusion exclusion )
166     {
167         if ( !(exclusion instanceof Exclusion) )
168         {
169             throw new ClassCastException( "Dependency.addExclusions(exclusion) parameter must be instanceof " + Exclusion.class.getName() );
170         }
171         getExclusions().add( exclusion );
172     } //-- void addExclusion( Exclusion ) 
173 
174     /**
175      * Get 
176      *             
177      *             The unique id for an artifact produced by the
178      * project group, e.g.
179      *             <code>maven-artifact</code>.
180      *             
181      *           
182      * 
183      * @return String
184      */
185     public String getArtifactId()
186     {
187         return this.artifactId;
188     } //-- String getArtifactId() 
189 
190     /**
191      * Get 
192      *             
193      *             The classifier of the dependency. This allows
194      * distinguishing two artifacts
195      *             that belong to the same POM but were built
196      * differently, and is appended to
197      *             the filename after the version. For example,
198      * <code>jdk14</code> and <code>jdk15</code>.
199      *             
200      *           
201      * 
202      * @return String
203      */
204     public String getClassifier()
205     {
206         return this.classifier;
207     } //-- String getClassifier() 
208 
209     /**
210      * Method getExclusions.
211      * 
212      * @return java.util.List
213      */
214     public java.util.List getExclusions()
215     {
216         if ( this.exclusions == null )
217         {
218             this.exclusions = new java.util.ArrayList();
219         }
220     
221         return this.exclusions;
222     } //-- java.util.List getExclusions() 
223 
224     /**
225      * Get 
226      *             
227      *             The project group that produced the dependency,
228      * e.g.
229      *             <code>org.apache.maven</code>.
230      *             
231      *           
232      * 
233      * @return String
234      */
235     public String getGroupId()
236     {
237         return this.groupId;
238     } //-- String getGroupId() 
239 
240     /**
241      * Get 
242      *             
243      *             The scope of the dependency -
244      * <code>compile</code>, <code>runtime</code>,
245      *             <code>test</code>, <code>system</code>, and
246      * <code>provided</code>. Used to
247      *             calculate the various classpaths used for
248      * compilation, testing, and so on.
249      *             It also assists in determining which artifacts
250      * to include in a distribution of
251      *             this project. For more information, see
252      *             <a
253      * href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the
254      *             dependency mechanism</a>.
255      *             
256      *           
257      * 
258      * @return String
259      */
260     public String getScope()
261     {
262         return this.scope;
263     } //-- String getScope() 
264 
265     /**
266      * Get 
267      *             
268      *             FOR SYSTEM SCOPE ONLY. Note that use of this
269      * property is <b>discouraged</b>
270      *             and may be replaced in later versions. This
271      * specifies the path on the filesystem
272      *             for this dependency.
273      *             Requires an absolute path for the value, not
274      * relative.
275      *             Use a property that gives the machine specific
276      * absolute path,
277      *             e.g. <code>${java.home}</code>.
278      *             
279      *           
280      * 
281      * @return String
282      */
283     public String getSystemPath()
284     {
285         return this.systemPath;
286     } //-- String getSystemPath() 
287 
288     /**
289      * Get 
290      *             
291      *             The type of dependency. This defaults to
292      * <code>jar</code>. While it
293      *             usually represents the extension on the filename
294      * of the dependency,
295      *             that is not always the case. A type can be
296      * mapped to a different
297      *             extension and a classifier.
298      *             The type often correspongs to the packaging
299      * used, though this is also
300      *             not always the case.
301      *             Some examples are <code>jar</code>,
302      * <code>war</code>, <code>ejb-client</code>
303      *             and <code>test-jar</code>.
304      *             New types can be defined by plugins that set
305      *             <code>extensions</code> to <code>true</code>, so
306      * this is not a complete list.
307      *             
308      *           
309      * 
310      * @return String
311      */
312     public String getType()
313     {
314         return this.type;
315     } //-- String getType() 
316 
317     /**
318      * Get 
319      *             
320      *             The version of the dependency, e.g.
321      * <code>3.2.1</code>. In Maven 2, this can also be
322      *             specified as a range of versions.
323      *             
324      *           
325      * 
326      * @return String
327      */
328     public String getVersion()
329     {
330         return this.version;
331     } //-- String getVersion() 
332 
333     /**
334      * Get indicates the dependency is optional for use of this
335      * library. While the
336      *             version of the dependency will be taken into
337      * account for dependency calculation if the
338      *             library is used elsewhere, it will not be passed
339      * on transitively.
340      * 
341      * @return boolean
342      */
343     public boolean isOptional()
344     {
345         return this.optional;
346     } //-- boolean isOptional() 
347 
348     /**
349      * Method removeExclusion.
350      * 
351      * @param exclusion
352      */
353     public void removeExclusion( Exclusion exclusion )
354     {
355         if ( !(exclusion instanceof Exclusion) )
356         {
357             throw new ClassCastException( "Dependency.removeExclusions(exclusion) parameter must be instanceof " + Exclusion.class.getName() );
358         }
359         getExclusions().remove( exclusion );
360     } //-- void removeExclusion( Exclusion ) 
361 
362     /**
363      * Set 
364      *             
365      *             The unique id for an artifact produced by the
366      * project group, e.g.
367      *             <code>maven-artifact</code>.
368      *             
369      *           
370      * 
371      * @param artifactId
372      */
373     public void setArtifactId( String artifactId )
374     {
375         this.artifactId = artifactId;
376     } //-- void setArtifactId( String ) 
377 
378     /**
379      * Set 
380      *             
381      *             The classifier of the dependency. This allows
382      * distinguishing two artifacts
383      *             that belong to the same POM but were built
384      * differently, and is appended to
385      *             the filename after the version. For example,
386      * <code>jdk14</code> and <code>jdk15</code>.
387      *             
388      *           
389      * 
390      * @param classifier
391      */
392     public void setClassifier( String classifier )
393     {
394         this.classifier = classifier;
395     } //-- void setClassifier( String ) 
396 
397     /**
398      * Set lists a set of artifacts that should be excluded from
399      * this dependency's
400      *             artifact list when it comes to calculating
401      * transitive dependencies.
402      * 
403      * @param exclusions
404      */
405     public void setExclusions( java.util.List exclusions )
406     {
407         this.exclusions = exclusions;
408     } //-- void setExclusions( java.util.List ) 
409 
410     /**
411      * Set 
412      *             
413      *             The project group that produced the dependency,
414      * e.g.
415      *             <code>org.apache.maven</code>.
416      *             
417      *           
418      * 
419      * @param groupId
420      */
421     public void setGroupId( String groupId )
422     {
423         this.groupId = groupId;
424     } //-- void setGroupId( String ) 
425 
426     /**
427      * Set indicates the dependency is optional for use of this
428      * library. While the
429      *             version of the dependency will be taken into
430      * account for dependency calculation if the
431      *             library is used elsewhere, it will not be passed
432      * on transitively.
433      * 
434      * @param optional
435      */
436     public void setOptional( boolean optional )
437     {
438         this.optional = optional;
439     } //-- void setOptional( boolean ) 
440 
441     /**
442      * Set 
443      *             
444      *             The scope of the dependency -
445      * <code>compile</code>, <code>runtime</code>,
446      *             <code>test</code>, <code>system</code>, and
447      * <code>provided</code>. Used to
448      *             calculate the various classpaths used for
449      * compilation, testing, and so on.
450      *             It also assists in determining which artifacts
451      * to include in a distribution of
452      *             this project. For more information, see
453      *             <a
454      * href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the
455      *             dependency mechanism</a>.
456      *             
457      *           
458      * 
459      * @param scope
460      */
461     public void setScope( String scope )
462     {
463         this.scope = scope;
464     } //-- void setScope( String ) 
465 
466     /**
467      * Set 
468      *             
469      *             FOR SYSTEM SCOPE ONLY. Note that use of this
470      * property is <b>discouraged</b>
471      *             and may be replaced in later versions. This
472      * specifies the path on the filesystem
473      *             for this dependency.
474      *             Requires an absolute path for the value, not
475      * relative.
476      *             Use a property that gives the machine specific
477      * absolute path,
478      *             e.g. <code>${java.home}</code>.
479      *             
480      *           
481      * 
482      * @param systemPath
483      */
484     public void setSystemPath( String systemPath )
485     {
486         this.systemPath = systemPath;
487     } //-- void setSystemPath( String ) 
488 
489     /**
490      * Set 
491      *             
492      *             The type of dependency. This defaults to
493      * <code>jar</code>. While it
494      *             usually represents the extension on the filename
495      * of the dependency,
496      *             that is not always the case. A type can be
497      * mapped to a different
498      *             extension and a classifier.
499      *             The type often correspongs to the packaging
500      * used, though this is also
501      *             not always the case.
502      *             Some examples are <code>jar</code>,
503      * <code>war</code>, <code>ejb-client</code>
504      *             and <code>test-jar</code>.
505      *             New types can be defined by plugins that set
506      *             <code>extensions</code> to <code>true</code>, so
507      * this is not a complete list.
508      *             
509      *           
510      * 
511      * @param type
512      */
513     public void setType( String type )
514     {
515         this.type = type;
516     } //-- void setType( String ) 
517 
518     /**
519      * Set 
520      *             
521      *             The version of the dependency, e.g.
522      * <code>3.2.1</code>. In Maven 2, this can also be
523      *             specified as a range of versions.
524      *             
525      *           
526      * 
527      * @param version
528      */
529     public void setVersion( String version )
530     {
531         this.version = version;
532     } //-- void setVersion( String ) 
533 
534 
535             
536     /**
537      * @see java.lang.Object#toString()
538      */
539     public String toString()
540     {
541         return "Dependency {groupId=" + groupId + ", artifactId=" + artifactId + ", version=" + version + ", type=" + type + "}";
542     }
543             
544           
545             
546     /**
547      * @return the management key as <code>groupId:artifactId:type</code>
548      */
549     public String getManagementKey()
550     {
551         return groupId + ":" + artifactId + ":" + type + ( classifier != null ? ":" + classifier : "" );
552     }
553             
554           
555     private String modelEncoding = "UTF-8";
556 
557     /**
558      * Set an encoding used for reading/writing the model.
559      *
560      * @param modelEncoding the encoding used when reading/writing the model.
561      */
562     public void setModelEncoding( String modelEncoding )
563     {
564         this.modelEncoding = modelEncoding;
565     }
566 
567     /**
568      * @return the current encoding used when reading/writing this model.
569      */
570     public String getModelEncoding()
571     {
572         return modelEncoding;
573     }
574 }