Class Dependency
- All Implemented Interfaces:
Cloneable,DependencyContainer
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 requiresgroupIdandartifactIdattributes.
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, refFields inherited from class org.apache.tools.ant.ProjectComponent
description, location, project -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor that initializes the dependency with no settings. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddExclusion(Exclusion exclusion) 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, ornullif none is set.Returns the list of exclusions declared for this dependency.Returns the groupId of the dependency.protected DependencygetRef()Resolves this object if defined as a reference and verifies that it is aDependencyinstance.getScope()Returns the Maven dependency scope.Returns the system path file attribute for this dependency, ifscopeis set tosystem.getType()Returns the type (or packaging) of the dependency artifact.Returns the version of the dependency.Returns a versionless key for the dependency, composed ofgroupId:artifactId[:type[:classifier]].voidsetArtifactId(String artifactId) Sets theartifactIdof the dependency.voidsetClassifier(String classifier) Sets the classifier for the dependency.voidSets the Maven coordinates of the dependency using a colon-separated string.voidsetGroupId(String groupId) Sets thegroupIdof the dependency.voidsetRefid(org.apache.tools.ant.types.Reference ref) Sets a reference to another dependency or dependencies container.voidSets the Maven dependency scope.voidsetSystemPath(File systemPath) Sets the system path to the artifact file for a dependency withscope="system".voidSets the type (packaging) of the artifact.voidsetVersion(String version) Sets the version of the dependency.voidvalidate(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, toStringMethods inherited from class org.apache.tools.ant.ProjectComponent
getDescription, getLocation, getProject, log, log, setDescription, setLocation, setProject
-
Constructor Details
-
Dependency
public Dependency()Default constructor that initializes the dependency with no settings.
-
-
Method Details
-
getRef
Resolves this object if defined as a reference and verifies that it is aDependencyinstance.- Returns:
- the referenced
Dependencyinstance - 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:DependencyContainerValidates 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:
validatein interfaceDependencyContainer- 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:
setRefidin classorg.apache.tools.ant.types.DataType- Parameters:
ref- the AntReferenceto set- Throws:
org.apache.tools.ant.BuildException- if this instance is already configured locally and cannot accept a reference- See Also:
-
getGroupId
Returns the groupId of the dependency.The
groupIduniquely 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
groupIdis used in combination withartifactIdandversionto uniquely resolve a dependency.- Returns:
- the groupId (never
nullif properly configured) - See Also:
-
setGroupId
Sets thegroupIdof the dependency.The
groupIdidentifies 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 currentProjectcontext.- Parameters:
groupId- the group identifier to set (must not benullor empty)- See Also:
-
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
groupIdandversion, the artifactId forms the core identity of a Maven dependency.- Returns:
- the artifactId (never
nullif properly configured) - See Also:
-
setArtifactId
Sets theartifactIdof 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 currentProjectcontext.- Parameters:
artifactId- the artifact identifier to set (must not benullor empty)- See Also:
-
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
nullif not set and expected to be inherited - See Also:
-
setVersion
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 currentProjectcontext.If neither an explicit version nor an inherited version is available at resolution time, a
BuildExceptionwill typically be thrown.- Parameters:
version- the version string to set, ornullif it should be inherited- See Also:
-
getClassifier
Returns the classifier of the dependency, ornullif none is set.The classifier distinguishes artifacts that are built from the same source but differ in content. For example, classifiers like
sources,javadoc, ortestsrefer 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 givengroupId,artifactId,version, andtype.- Returns:
- the classifier, or
nullif not specified - See Also:
-
setClassifier
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:
sourcesjavadoctests
Classifiers are optional and usually accompany a specific artifact type.
- Parameters:
classifier- the classifier to set
-
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)pomwaraarzip,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
Sets the type (packaging) of the artifact.This corresponds to the Maven artifact type, typically matching the file extension. Common values include:
jar(default)pomwaraarzip,tar.gz, etc.
This affects both resolution and how the artifact appears in generated POMs.
- Parameters:
type- the artifact type (packaging)
-
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)providedruntimetestsystemimport
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
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 transitivelyprovided– required at compile time but not packaged (e.g., servlet APIs)runtime– not required at compile time, but needed at runtimetest– used only for test compilation and executionsystem– similar to provided but with a fixed localsystemPathimport– used only withindependencyManagement(typically unused in Ant)
If omitted, the default scope is
compile.- Parameters:
scope- the Maven scope to set- See Also:
-
setCoords
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:versiongroupId:artifactId:packaging:versiongroupId:artifactId:packaging:classifier:version
For example:
org.apache.commons:commons-lang3:3.18.0com.example:lib:jar:sources:1.0.0
This method will parse and assign the corresponding fields:
groupId,artifactId,type(packaging),classifier, andversion.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
Sets the system path to the artifact file for a dependency withscope="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 currentProjectcontext.Note: This attribute is ignored unless
scopeissystem.- Parameters:
systemPath- the file path to use for the system-scoped dependency- See Also:
-
getSystemPath
Returns the system path file attribute for this dependency, ifscopeis set tosystem.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
nullif not set - See Also:
-
getVersionlessKey
Returns a versionless key for the dependency, composed ofgroupId: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:
groupIdartifactIdtype(if notjar)classifier(if present)
Example:
org.apache.commons:commons-lang3orcom.example:lib:zip:sources- Returns:
- a versionless coordinate key representing the dependency
-
addExclusion
Allows ant to add a specified exclusion to omit a specific transitive dependency.- Parameters:
exclusion- theExclusionto omit
-
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
groupIdandartifactIdof 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
Exclusionelements; may be empty but nevernull - See Also:
-