View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.maven;
25  
26  import java.util.EnumSet;
27  import java.util.LinkedHashSet;
28  import java.util.Set;
29  import org.modeshape.common.util.CheckArg;
30  
31  /**
32   * The cornerstone of Maven is its dependency list. Most every project depends upon others to build and run correctly, and if all
33   * Maven does for you is manage this list for you, you have gained a lot. Maven downloads and links the dependencies for you on
34   * compilation and other goals that require them. As an added bonus, Maven brings in the dependencies of those dependencies
35   * (transitive dependencies), allowing your list to focus solely on the dependencies your project requires.
36   */
37  public class MavenDependency {
38  
39      /**
40       * The scope of the dependency - <code>compile</code>, <code>runtime</code>, <code>test</code>, <code>system</code>, and
41       * <code>provided</code>. Used to calculate the various classpaths used for compilation, testing, and so on. It also assists
42       * in determining which artifacts to include in a distribution of this project. For more information, see <a
43       * href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the dependency mechanism</a>.
44       */
45      public enum Scope {
46          COMPILE("compile"),
47          TEST("test"),
48          PROVIDED("provided"),
49          SYSTEM("system"),
50          RUNTIME("runtime");
51  
52          private String suffix;
53  
54          private Scope( String suffix ) {
55              this.suffix = suffix;
56          }
57  
58          public String getText() {
59              return this.suffix;
60          }
61  
62          public static Scope valueByText( String suffix,
63                                           boolean useDefault ) {
64              for (Scope type : Scope.values()) {
65                  if (type.suffix.equalsIgnoreCase(suffix)) return type;
66              }
67              return useDefault ? getDefault() : null;
68          }
69  
70          public static Scope getDefault() {
71              return COMPILE;
72          }
73  
74          public static EnumSet<Scope> getRuntimeScopes() {
75              return EnumSet.of(COMPILE, RUNTIME);
76          }
77      }
78  
79      public static final String DEFAULT_TYPE = "jar";
80      public static final boolean DEFAULT_OPTIONAL = false;
81  
82      private MavenId id;
83      private String type = DEFAULT_TYPE;
84      private Scope scope = Scope.getDefault();
85      private String systemPath;
86      private boolean optional = DEFAULT_OPTIONAL;
87      private final Set<MavenId> exclusions = new LinkedHashSet<MavenId>();
88  
89      public MavenDependency( String coordinates ) {
90          this.id = new MavenId(coordinates);
91      }
92  
93      public MavenDependency( MavenId id ) {
94          CheckArg.isNotNull(id, "id");
95          this.id = id;
96      }
97  
98      public MavenDependency( String groupId,
99                              String artifactId,
100                             String version ) {
101         this.id = new MavenId(groupId, artifactId, version);
102     }
103 
104     public MavenDependency( String groupId,
105                             String artifactId,
106                             String version,
107                             String classifier ) {
108         this.id = new MavenId(groupId, artifactId, version, classifier);
109     }
110 
111     /**
112      * The identifier of the artifact for this dependency.
113      * 
114      * @return the identifier
115      */
116     public MavenId getId() {
117         return this.id;
118     }
119 
120     /**
121      * The type of dependency. This defaults to <code>jar</code>. While it usually represents the extension on the filename of the
122      * dependency, that is not always the case. A type can be mapped to a different extension and a classifier. The type often
123      * correspongs to the packaging used, though this is also not always the case. Some examples are <code>jar</code>,
124      * <code>war</code>, <code>ejb-client</code> and <code>test-jar</code>. New types can be defined by plugins that set
125      * <code>extensions</code> to <code>true</code>, so this is not a complete list.
126      * 
127      * @return the dependency type
128      */
129     public String getType() {
130         return this.type;
131     }
132 
133     /**
134      * Set the type of dependency.
135      * 
136      * @param type the new dependency type. If null, then the type will be set to the {@link #DEFAULT_TYPE default dependency
137      *        type}.
138      */
139     public void setType( String type ) {
140         this.type = type != null ? type.trim() : DEFAULT_TYPE;
141     }
142 
143     /**
144      * The scope of the dependency - <code>compile</code>, <code>runtime</code>, <code>test</code>, <code>system</code>, and
145      * <code>provided</code>. Used to calculate the various classpaths used for compilation, testing, and so on. It also assists
146      * in determining which artifacts to include in a distribution of this project. For more information, see <a
147      * href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the dependency mechanism</a>.
148      * 
149      * @return the scope
150      */
151     public Scope getScope() {
152         return this.scope;
153     }
154 
155     /**
156      * @param scope Sets scope to the specified value.
157      */
158     public void setScope( Scope scope ) {
159         this.scope = scope != null ? scope : Scope.getDefault();
160     }
161 
162     public void setScope( String text ) {
163         this.scope = Scope.valueByText(text, true);
164     }
165 
166     /**
167      * FOR SYSTEM SCOPE ONLY. Note that use of this property is <b>discouraged</b> and may be replaced in later versions. This
168      * specifies the path on the filesystem for this dependency. Requires an absolute path for the value, not relative. Use a
169      * property that gives the machine specific absolute path, e.g. <code>${java.home}</code>.
170      * 
171      * @return systemPath
172      */
173     public String getSystemPath() {
174         return this.systemPath;
175     }
176 
177     /**
178      * @param systemPath Sets systemPath to the specified value.
179      */
180     public void setSystemPath( String systemPath ) {
181         this.systemPath = systemPath != null ? systemPath.trim() : null;
182     }
183 
184     /**
185      * Indicates the dependency is optional for use of this library. While the version of the dependency will be taken into
186      * account for dependency calculation if the library is used elsewhere, it will not be passed on transitively.
187      * 
188      * @return true if this is an optional dependency, or false otherwise
189      */
190     public boolean isOptional() {
191         return this.optional;
192     }
193 
194     /**
195      * @param optional Sets optional to the specified value.
196      */
197     public void setOptional( boolean optional ) {
198         this.optional = optional;
199     }
200 
201     /**
202      * Exclusions explicitly tell Maven that you don't want to include the specified project that is a dependency of this
203      * dependency (in other words, its transitive dependency). For example, the maven-embedder requires maven-core , and we do not
204      * wish to use it or its dependencies, then we would add it as an exclusion .
205      * 
206      * @return the set of exclusions
207      */
208     public Set<MavenId> getExclusions() {
209         return this.exclusions;
210     }
211 
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public int hashCode() {
217         return this.id.hashCode();
218     }
219 
220     /**
221      * {@inheritDoc}
222      */
223     @Override
224     public boolean equals( Object obj ) {
225         if (obj == this) return true;
226         if (obj instanceof MavenDependency) {
227             MavenDependency that = (MavenDependency)obj;
228             return this.id.equals(that.id);
229         }
230         return false;
231     }
232 
233     /**
234      * {@inheritDoc}
235      */
236     @Override
237     public String toString() {
238         return this.id.toString();
239     }
240 
241 }