java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.types.DataType
org.apache.maven.resolver.internal.ant.types.Dependency
All Implemented Interfaces:
Cloneable, DependencyContainer

public class Dependency extends org.apache.tools.ant.types.DataType implements DependencyContainer
Represents a single Maven dependency in an Ant build script.

This Ant DataType defines the coordinates and metadata of a Maven dependency, including its scope, type, classifier, and optional exclusions. It is intended to be used as a nested element within a Dependencies container, which itself is used by tasks such as Resolve, CreatePom, Install, or Deploy.

Usage Example:


 <dependencies>
   <dependency
     groupId="org.apache.commons"
     artifactId="commons-lang3"
     version="3.18.0"
     scope="compile"
     optional="false"
     type="jar">
     <exclusion groupId="commons-logging" artifactId="commons-logging"/>
   </dependency>
 </dependencies>
 
It is also possible to use the short notation when declaring a dependency e.g.

 <dependencies>
   <dependency coords="org.apache.commons:commons-lang3:3.18.0" scope="compile"/>
 </dependencies>
 

Attributes:

  • groupId (required) — the Maven coordinate group ID
  • artifactId (required) — the Maven artifact ID
  • version (required unless specified by a bom in the dependency management section) — the version of the dependency
  • scope — the Maven scope (e.g., compile, runtime, test, system)
  • type — the artifact packaging type (default: jar)
  • classifier — optional classifier (e.g., sources, javadoc)
  • optional — whether the dependency is marked as optional (default: false)
  • systemPath — file path to the artifact for system-scoped dependencies

Nested Elements:

  • <exclusion> — zero or more exclusions to omit specific transitive dependencies. Each exclusion requires groupId and artifactId attributes.

Typical Use Cases:

  • Resolving direct project dependencies for compilation or testing
  • Including dependencies in a generated POM or deployment descriptor
  • Installing and deploying artifacts with well-defined transitive behavior
See Also:
  • Field Summary

    Fields inherited from class org.apache.tools.ant.types.DataType

    checked, ref

    Fields inherited from class org.apache.tools.ant.ProjectComponent

    description, location, project
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor that initializes the dependency with no settings.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Allows ant to add a specified exclusion to omit a specific transitive dependency.
    Returns the artifactId of the dependency.
    Returns the classifier of the dependency, or null if none is set.
    Returns the list of exclusions declared for this dependency.
    Returns the groupId of the dependency.
    protected Dependency
    Resolves this object if defined as a reference and verifies that it is a Dependency instance.
    Returns the Maven dependency scope.
    Returns the system path file attribute for this dependency, if scope is set to system.
    Returns the type (or packaging) of the dependency artifact.
    Returns the version of the dependency.
    Returns a versionless key for the dependency, composed of groupId:artifactId[:type[:classifier]].
    void
    setArtifactId(String artifactId)
    Sets the artifactId of the dependency.
    void
    setClassifier(String classifier)
    Sets the classifier for the dependency.
    void
    setCoords(String coords)
    Sets the Maven coordinates of the dependency using a colon-separated string.
    void
    setGroupId(String groupId)
    Sets the groupId of the dependency.
    void
    setRefid(org.apache.tools.ant.types.Reference ref)
    Sets a reference to another dependency or dependencies container.
    void
    Sets the Maven dependency scope.
    void
    setSystemPath(File systemPath)
    Sets the system path to the artifact file for a dependency with scope="system".
    void
    Sets the type (packaging) of the artifact.
    void
    setVersion(String version)
    Sets the version of the dependency.
    void
    validate(org.apache.tools.ant.Task task)
    Validates the container's internal structure and attributes.

    Methods inherited from class org.apache.tools.ant.types.DataType

    checkAttributesAllowed, checkChildrenAllowed, circularReference, clone, dieOnCircularReference, dieOnCircularReference, dieOnCircularReference, getCheckedRef, getCheckedRef, getCheckedRef, getCheckedRef, getCheckedRef, getDataTypeName, getRefid, invokeCircularReferenceCheck, isChecked, isReference, noChildrenAllowed, pushAndInvokeCircularReferenceCheck, setChecked, tooManyAttributes, toString

    Methods inherited from class org.apache.tools.ant.ProjectComponent

    getDescription, getLocation, getProject, log, log, setDescription, setLocation, setProject

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Dependency

      public Dependency()
      Default constructor that initializes the dependency with no settings.
  • Method Details

    • getRef

      protected Dependency getRef()
      Resolves this object if defined as a reference and verifies that it is a Dependency instance.
      Returns:
      the referenced Dependency instance
      Throws:
      org.apache.tools.ant.BuildException - if the reference is invalid
    • validate

      public void validate(org.apache.tools.ant.Task task)
      Description copied from interface: DependencyContainer
      Validates the container's internal structure and attributes.

      This method is typically invoked by Ant tasks during execution to ensure that dependency definitions are well-formed, unambiguous, and do not conflict.

      Specified by:
      validate in interface DependencyContainer
      Parameters:
      task - the Ant task requesting validation, typically used for error context
    • setRefid

      public void setRefid(org.apache.tools.ant.types.Reference ref)
      Sets a reference to another dependency or dependencies container.

      This allows the current <dependency> element to act as an alias to another dependency or shared container defined elsewhere in the Ant build script. The referenced object must be compatible (e.g., a <dependency> or <dependencies> instance), and its configuration will be used in place of this one.

      This is useful for reusing dependency definitions across multiple tasks without duplicating their configuration.

      Once a reference is set, all other local attributes on this element are ignored.

      Overrides:
      setRefid in class org.apache.tools.ant.types.DataType
      Parameters:
      ref - the Ant Reference to set
      Throws:
      org.apache.tools.ant.BuildException - if this instance is already configured locally and cannot accept a reference
      See Also:
    • getGroupId

      public String getGroupId()
      Returns the groupId of the dependency.

      The groupId uniquely identifies the organization or project to which the artifact belongs. It is a required part of the Maven coordinate and typically uses a reverse-domain naming convention (e.g., org.apache.commons).

      The groupId is used in combination with artifactId and version to uniquely resolve a dependency.

      Returns:
      the groupId (never null if properly configured)
      See Also:
    • setGroupId

      public void setGroupId(String groupId)
      Sets the groupId of the dependency.

      The groupId identifies the organization or project that provides the artifact. It is a required coordinate in Maven and typically follows a reverse domain name pattern (e.g., org.apache.commons).

      The value may include Ant properties (e.g., ${project.groupId}), which will be resolved at runtime using the current Project context.

      Parameters:
      groupId - the group identifier to set (must not be null or empty)
      See Also:
    • getArtifactId

      public String getArtifactId()
      Returns the artifactId of the dependency.

      The artifactId uniquely identifies the artifact within a group and typically corresponds to the base name of the artifact’s file. It is required for Maven coordinate resolution.

      Together with groupId and version, the artifactId forms the core identity of a Maven dependency.

      Returns:
      the artifactId (never null if properly configured)
      See Also:
    • setArtifactId

      public void setArtifactId(String artifactId)
      Sets the artifactId of the dependency.

      The artifactId uniquely identifies an artifact within a groupId. It is a required part of the Maven coordinate and typically corresponds to the name of the library or module.

      The value may include Ant properties (e.g., ${lib.name}), which will be resolved at runtime using the current Project context.

      Parameters:
      artifactId - the artifact identifier to set (must not be null or empty)
      See Also:
    • getVersion

      public String getVersion()
      Returns the version of the dependency.

      The version is a required component of the Maven coordinate unless it is being inherited from a <dependencyManagement> section, such as through the import of a BOM (Bill of Materials). In such cases, the version may be omitted, and the version defined in the imported BOM will apply.

      The value may contain Ant properties (e.g., ${project.version}), which will be resolved at runtime using the current Ant project context.

      Returns:
      the explicitly set version of the dependency, or null if not set and expected to be inherited
      See Also:
    • setVersion

      public void setVersion(String version)
      Sets the version of the dependency.

      The version identifies the specific release of the artifact to be resolved. In most cases, this is a required attribute. However, if the version is provided through an imported BOM in a <dependencyManagement> section, it may be omitted here and inherited automatically.

      The value may include Ant properties (e.g., ${project.version}), which will be resolved at runtime using the current Project context.

      If neither an explicit version nor an inherited version is available at resolution time, a BuildException will typically be thrown.

      Parameters:
      version - the version string to set, or null if it should be inherited
      See Also:
    • getClassifier

      public String getClassifier()
      Returns the classifier of the dependency, or null if none is set.

      The classifier distinguishes artifacts that are built from the same source but differ in content. For example, classifiers like sources, javadoc, or tests refer to alternate artifacts attached to the same Maven coordinates.

      If the classifier is not explicitly set, this method returns null, indicating that the dependency refers to the main artifact for the given groupId, artifactId, version, and type.

      Returns:
      the classifier, or null if not specified
      See Also:
    • setClassifier

      public void setClassifier(String classifier)
      Sets the classifier for the dependency.

      A classifier distinguishes artifacts that are built from the same source but differ in content or purpose. Examples include:

      • sources
      • javadoc
      • tests

      Classifiers are optional and usually accompany a specific artifact type.

      Parameters:
      classifier - the classifier to set
    • getType

      public String getType()
      Returns the type (or packaging) of the dependency artifact.

      The type typically corresponds to the file extension of the artifact and determines how it is resolved and handled. Common values include:

      • jar (default)
      • pom
      • war
      • aar
      • zip, tar.gz, etc.

      If not explicitly set, this method returns "jar" as the default.

      Returns:
      the artifact type, never null; defaults to "jar" if unset
      See Also:
    • setType

      public void setType(String type)
      Sets the type (packaging) of the artifact.

      This corresponds to the Maven artifact type, typically matching the file extension. Common values include:

      • jar (default)
      • pom
      • war
      • aar
      • zip, tar.gz, etc.

      This affects both resolution and how the artifact appears in generated POMs.

      Parameters:
      type - the artifact type (packaging)
    • getScope

      public String getScope()
      Returns the Maven dependency scope.

      The scope controls when and how the dependency is included in the classpath or packaged artifact. Valid values include:

      • compile (default)
      • provided
      • runtime
      • test
      • system
      • import

      If the scope is not explicitly set, this method returns "compile".

      Returns:
      the scope of the dependency; defaults to "compile" if unset
      See Also:
    • setScope

      public void setScope(String scope)
      Sets the Maven dependency scope.

      The scope determines the classpath visibility and transitivity of the dependency. Supported values are:

      • compile (default) – available in all classpaths and inherited transitively
      • provided – required at compile time but not packaged (e.g., servlet APIs)
      • runtime – not required at compile time, but needed at runtime
      • test – used only for test compilation and execution
      • system – similar to provided but with a fixed local systemPath
      • import – used only within dependencyManagement (typically unused in Ant)

      If omitted, the default scope is compile.

      Parameters:
      scope - the Maven scope to set
      See Also:
    • setCoords

      public void setCoords(String coords)
      Sets the Maven coordinates of the dependency using a colon-separated string.

      This is a convenience method that allows setting multiple fields at once. The supported formats are:

      • groupId:artifactId:version
      • groupId:artifactId:packaging:version
      • groupId:artifactId:packaging:classifier:version

      For example:

      • org.apache.commons:commons-lang3:3.18.0
      • com.example:lib:jar:sources:1.0.0

      This method will parse and assign the corresponding fields: groupId, artifactId, type (packaging), classifier, and version.

      The string may include Ant properties (e.g., ${groupId}:my-artifact:${version}), which will be resolved at runtime.

      Parameters:
      coords - the colon-separated Maven coordinate string
      Throws:
      org.apache.tools.ant.BuildException - if the coordinate string is malformed
      See Also:
    • setSystemPath

      public void setSystemPath(File systemPath)
      Sets the system path to the artifact file for a dependency with scope="system".

      This specifies a local file path to be used as the artifact for this dependency, bypassing repository resolution. This is only applicable if the scope is explicitly set to system.

      The path may include Ant properties (e.g., ${basedir}/lib/my-lib.jar) which will be resolved at runtime using the current Project context.

      Note: This attribute is ignored unless scope is system.

      Parameters:
      systemPath - the file path to use for the system-scoped dependency
      See Also:
    • getSystemPath

      public File getSystemPath()
      Returns the system path file attribute for this dependency, if scope is set to system.

      The system path points to a local file that provides the dependency artifact, bypassing standard repository resolution. This is only valid and meaningful when the dependency scope is set to system.

      If the scope is not system, this value is ignored.

      Returns:
      A File representing the system path to the dependency file, or null if not set
      See Also:
    • getVersionlessKey

      public String getVersionlessKey()
      Returns a versionless key for the dependency, composed of groupId:artifactId[:type[:classifier]].

      This is useful for identifying dependencies regardless of version, such as when performing conflict resolution, exclusions, or matching in dependency management sections.

      The returned string includes:

      • groupId
      • artifactId
      • type (if not jar)
      • classifier (if present)

      Example: org.apache.commons:commons-lang3 or com.example:lib:zip:sources

      Returns:
      a versionless coordinate key representing the dependency
    • addExclusion

      public void addExclusion(Exclusion exclusion)
      Allows ant to add a specified exclusion to omit a specific transitive dependency.
      Parameters:
      exclusion - the Exclusion to omit
    • getExclusions

      public List<Exclusion> getExclusions()
      Returns the list of exclusions declared for this dependency.

      Exclusions are used to prevent specific transitive dependencies from being included during resolution. Each exclusion specifies a groupId and artifactId of a dependency that should be omitted when this dependency is resolved transitively.

      Exclusions are typically used to avoid version conflicts, remove unwanted libraries, or override defaults inherited through dependency chains.

      Returns:
      a list of Exclusion elements; may be empty but never null
      See Also: