Class AddDependencyMojo
java.lang.Object
org.apache.maven.plugin.AbstractMojo
org.apache.maven.plugins.dependency.AbstractDependencyMojo
org.apache.maven.plugins.dependency.AddDependencyMojo
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo
@Mojo(name="add",
requiresProject=true,
threadSafe=true)
public class AddDependencyMojo
extends AbstractDependencyMojo
Adds a dependency to the project's
pom.xml.
Supports adding to <dependencies> or <dependencyManagement>,
with version inference from managed dependencies.
If the dependency already exists, the goal fails with a descriptive error directing the user to remove it first.
The goal uses formatting-preserving DOM manipulation to maintain the POM's existing structure (comments, indentation, encoding). Duplicate detection uses type and classifier-aware matching, and cross-references Maven's resolved model to catch dependencies declared via property references.
Scope values are validated against Maven's known scopes:
compile, provided, runtime, test, system, import.
- Since:
- 3.11.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classHolds the detected conventions for the project.(package private) static enumProperty naming conventions detected in existing POM files. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanWhentrue(the default), automatically detect and follow the project's existing dependency management conventions: If most existing dependencies are version-less, add managed dependency to parent POM If versions use${...}property references, create a version property Property naming follows the detected pattern (e.g.,.version,-version) Explicit parameters (managed,useProperty,propertyName) override detected conventions.private StringDependency classifier (e.g.,sources,javadoc,tests).private StringDependency coordinates:groupId:artifactId[:version]orgroupId:artifactId[:extension[:classifier]]:version.private booleanWhentrue, insert into<dependencyManagement>instead of<dependencies>.private BooleanWhether the dependency is optional.private StringTarget a specific Maven profile by its<id>.private static final Patternprivate StringExplicit property name for the version (e.g.,guava.version).private StringDependency scope.private StringDependency type/packaging (e.g.,jar,pom,war).private BooleanWhen set, controls whether a version property is created in<properties>.Fields inherited from class AbstractDependencyMojo
sessionFields inherited from interface org.apache.maven.plugin.Mojo
ROLE -
Constructor Summary
ConstructorsConstructorDescriptionAddDependencyMojo(org.apache.maven.execution.MavenSession session, org.sonatype.plexus.build.incremental.BuildContext buildContext, org.apache.maven.project.MavenProject project) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddCrossPom(DependencyEntry coords, File childPomFile, File parentPomFile, boolean effectiveUseProperty, String effectivePropertyName) Cross-POM mode: adds a managed dependency (with optional property) to the parent POM, and a version-less dependency reference to the child POM.private voidaddDependency(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile, String profileId, DependencyEntry coords, boolean managed) private voidaddSinglePom(DependencyEntry coords, org.apache.maven.project.MavenProject targetProject, File pomFile, boolean targetManaged, boolean effectiveUseProperty, String effectivePropertyName) Single-POM mode: adds the dependency (optionally with a version property) to the current POM.private eu.maveniverse.domtrip.maven.PomEditor.DependenciesdependenciesFor(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile, String profileId) private AddDependencyMojo.ConventionsdetectConventions(org.apache.maven.project.MavenProject project) Detects the project's dependency management conventions by analyzing existing dependencies.(package private) static AddDependencyMojo.PropertyPatterndetectPropertyPattern(List<String> versions) Analyzes property reference names to detect the naming convention.protected voidprivate static eu.maveniverse.domtrip.ElementfindDependency(eu.maveniverse.domtrip.maven.PomEditor editor, String profileId, String groupId, String artifactId, String type, String classifier, boolean managed) private FilefindManagedDepsPom(org.apache.maven.project.MavenProject project) Walks the parent chain to find the nearest POM that declares<dependencyManagement>.private StringfindManagedVersion(org.apache.maven.project.MavenProject project, String groupId, String artifactId) private static eu.maveniverse.domtrip.ElementgetDependenciesElement(eu.maveniverse.domtrip.maven.PomEditor editor, String profileId, boolean managed) private static longgetDependencyCount(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed) getDependencyVersions(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed) private static eu.maveniverse.domtrip.maven.PomEditorloadPomEditor(File pomFile) private DependencyEntryprivate static voidsavePomEditor(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile) private voidsyncInMemoryModel(org.apache.maven.project.MavenProject targetProject, DependencyEntry coords, boolean targetManaged, boolean versionless) Syncs the in-memory Maven model after POM modifications.Methods inherited from class AbstractDependencyMojo
execute, existsInResolvedModel, getProject, isSilent, isSkip, setSilent, setSkipMethods inherited from class org.apache.maven.plugin.AbstractMojo
getLog, getPluginContext, setLog, setPluginContext
-
Field Details
-
gav
Dependency coordinates:groupId:artifactId[:version]orgroupId:artifactId[:extension[:classifier]]:version. Scope, type, classifier, and optional can be overridden via separate parameters.- Since:
- 3.11.0
-
scope
Dependency scope. Validated against Maven's known scope values:compile,provided,runtime,test,system,import. Invalid values are rejected with aMojoFailureException.- Since:
- 3.11.0
-
type
Dependency type/packaging (e.g.,jar,pom,war).- Since:
- 3.11.0
-
classifier
Dependency classifier (e.g.,sources,javadoc,tests).- Since:
- 3.11.0
-
optional
Whether the dependency is optional.- Since:
- 3.11.0
-
managed
@Parameter(property="managed", defaultValue="false") private boolean managedWhentrue, insert into<dependencyManagement>instead of<dependencies>.- Since:
- 3.11.0
-
align
@Parameter(property="align", defaultValue="true") private boolean alignWhentrue(the default), automatically detect and follow the project's existing dependency management conventions:- If most existing dependencies are version-less, add managed dependency to parent POM
- If versions use
${...}property references, create a version property - Property naming follows the detected pattern (e.g.,
.version,-version)
managed,useProperty,propertyName) override detected conventions.- Since:
- 3.11.0
-
useProperty
When set, controls whether a version property is created in<properties>. Whennull(the default), the behavior is auto-detected from existing conventions ifalign=true.- Since:
- 3.11.0
-
propertyName
Explicit property name for the version (e.g.,guava.version). When not set, the name is derived from the detected naming convention.- Since:
- 3.11.0
-
profile
Target a specific Maven profile by its<id>. When set, the dependency is added to the profile's<dependencies>or<dependencyManagement>section. The profile must already exist in the POM.- Since:
- 3.11.0
-
PROPERTY_REF
-
-
Constructor Details
-
AddDependencyMojo
@Inject public AddDependencyMojo(org.apache.maven.execution.MavenSession session, org.sonatype.plexus.build.incremental.BuildContext buildContext, org.apache.maven.project.MavenProject project)
-
-
Method Details
-
doExecute
protected void doExecute() throws org.apache.maven.plugin.MojoExecutionException, org.apache.maven.plugin.MojoFailureException- Specified by:
doExecutein classAbstractDependencyMojo- Throws:
org.apache.maven.plugin.MojoExecutionException-MojoExecutionExceptionorg.apache.maven.plugin.MojoFailureException-MojoFailureException
-
addCrossPom
private void addCrossPom(DependencyEntry coords, File childPomFile, File parentPomFile, boolean effectiveUseProperty, String effectivePropertyName) throws IOException, org.apache.maven.plugin.MojoFailureException Cross-POM mode: adds a managed dependency (with optional property) to the parent POM, and a version-less dependency reference to the child POM.- Throws:
IOExceptionorg.apache.maven.plugin.MojoFailureException
-
addSinglePom
private void addSinglePom(DependencyEntry coords, org.apache.maven.project.MavenProject targetProject, File pomFile, boolean targetManaged, boolean effectiveUseProperty, String effectivePropertyName) throws IOException, org.apache.maven.plugin.MojoFailureException Single-POM mode: adds the dependency (optionally with a version property) to the current POM.- Throws:
IOExceptionorg.apache.maven.plugin.MojoFailureException
-
syncInMemoryModel
private void syncInMemoryModel(org.apache.maven.project.MavenProject targetProject, DependencyEntry coords, boolean targetManaged, boolean versionless) Syncs the in-memory Maven model after POM modifications. -
resolveCoordinates
- Throws:
org.apache.maven.plugin.MojoFailureException
-
findManagedVersion
-
addDependency
private void addDependency(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile, String profileId, DependencyEntry coords, boolean managed) throws org.apache.maven.plugin.MojoFailureException - Throws:
org.apache.maven.plugin.MojoFailureException
-
dependenciesFor
-
loadPomEditor
private static eu.maveniverse.domtrip.maven.PomEditor loadPomEditor(File pomFile) throws IOException - Throws:
IOException
-
savePomEditor
private static void savePomEditor(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile) throws IOException - Throws:
IOException
-
findDependency
-
getDependenciesElement
private static eu.maveniverse.domtrip.Element getDependenciesElement(eu.maveniverse.domtrip.maven.PomEditor editor, String profileId, boolean managed) -
getDependencyVersions
-
getDependencyCount
private static long getDependencyCount(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed) -
detectConventions
private AddDependencyMojo.Conventions detectConventions(org.apache.maven.project.MavenProject project) throws org.apache.maven.plugin.MojoExecutionException Detects the project's dependency management conventions by analyzing existing dependencies.- Throws:
org.apache.maven.plugin.MojoExecutionException
-
findManagedDepsPom
Walks the parent chain to find the nearest POM that declares<dependencyManagement>. -
detectPropertyPattern
Analyzes property reference names to detect the naming convention.- Returns:
- the most common
AddDependencyMojo.PropertyPattern, ornullif no clear pattern is found
-