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 Classes
    Modifier and Type
    Class
    Description
    (package private) static class 
    Holds the detected conventions for the project.
    (package private) static enum 
    Property naming conventions detected in existing POM files.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    When true (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 String
    Dependency classifier (e.g., sources, javadoc, tests).
    private String
    Dependency coordinates: groupId:artifactId[:version] or groupId:artifactId[:extension[:classifier]]:version.
    private boolean
    When true, insert into <dependencyManagement> instead of <dependencies>.
    private Boolean
    Whether the dependency is optional.
    private String
    Target a specific Maven profile by its <id>.
    private static final Pattern
     
    private String
    Explicit property name for the version (e.g., guava.version).
    private String
    Dependency scope.
    private String
    Dependency type/packaging (e.g., jar, pom, war).
    private Boolean
    When set, controls whether a version property is created in <properties>.

    Fields inherited from class AbstractDependencyMojo

    session

    Fields inherited from interface org.apache.maven.plugin.Mojo

    ROLE
  • Constructor Summary

    Constructors
    Constructor
    Description
    AddDependencyMojo(org.apache.maven.execution.MavenSession session, org.sonatype.plexus.build.incremental.BuildContext buildContext, org.apache.maven.project.MavenProject project)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    addCrossPom(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 void
    addDependency(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile, String profileId, DependencyEntry coords, boolean managed)
     
    private void
    addSinglePom(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.Dependencies
    dependenciesFor(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile, String profileId)
     
    detectConventions(org.apache.maven.project.MavenProject project)
    Detects the project's dependency management conventions by analyzing existing dependencies.
    (package private) static AddDependencyMojo.PropertyPattern
    Analyzes property reference names to detect the naming convention.
    protected void
     
    private static eu.maveniverse.domtrip.Element
    findDependency(eu.maveniverse.domtrip.maven.PomEditor editor, String profileId, String groupId, String artifactId, String type, String classifier, boolean managed)
     
    private File
    findManagedDepsPom(org.apache.maven.project.MavenProject project)
    Walks the parent chain to find the nearest POM that declares <dependencyManagement>.
    private String
    findManagedVersion(org.apache.maven.project.MavenProject project, String groupId, String artifactId)
     
    private static eu.maveniverse.domtrip.Element
    getDependenciesElement(eu.maveniverse.domtrip.maven.PomEditor editor, String profileId, boolean managed)
     
    private static long
    getDependencyCount(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed)
     
    private static List<String>
    getDependencyVersions(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed)
     
    private static eu.maveniverse.domtrip.maven.PomEditor
     
     
    private static void
    savePomEditor(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile)
     
    private void
    syncInMemoryModel(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 org.apache.maven.plugin.AbstractMojo

    getLog, getPluginContext, setLog, setPluginContext

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • gav

      @Parameter(property="gav") private String gav
      Dependency coordinates: groupId:artifactId[:version] or groupId:artifactId[:extension[:classifier]]:version. Scope, type, classifier, and optional can be overridden via separate parameters.
      Since:
      3.11.0
    • scope

      @Parameter(property="scope") private String scope
      Dependency scope. Validated against Maven's known scope values: compile, provided, runtime, test, system, import. Invalid values are rejected with a MojoFailureException.
      Since:
      3.11.0
    • type

      @Parameter(property="type") private String type
      Dependency type/packaging (e.g., jar, pom, war).
      Since:
      3.11.0
    • classifier

      @Parameter(property="classifier") private String classifier
      Dependency classifier (e.g., sources, javadoc, tests).
      Since:
      3.11.0
    • optional

      @Parameter(property="optional") private Boolean optional
      Whether the dependency is optional.
      Since:
      3.11.0
    • managed

      @Parameter(property="managed", defaultValue="false") private boolean managed
      When true, insert into <dependencyManagement> instead of <dependencies>.
      Since:
      3.11.0
    • align

      @Parameter(property="align", defaultValue="true") private boolean align
      When true (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.
      Since:
      3.11.0
    • useProperty

      @Parameter(property="useProperty") private Boolean useProperty
      When set, controls whether a version property is created in <properties>. When null (the default), the behavior is auto-detected from existing conventions if align=true.
      Since:
      3.11.0
    • propertyName

      @Parameter(property="propertyName") private String 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

      @Parameter(property="profile") private String 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

      private static final Pattern 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:
      doExecute in class AbstractDependencyMojo
      Throws:
      org.apache.maven.plugin.MojoExecutionException - MojoExecutionException
      org.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:
      IOException
      org.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:
      IOException
      org.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

      private DependencyEntry resolveCoordinates() throws org.apache.maven.plugin.MojoFailureException
      Throws:
      org.apache.maven.plugin.MojoFailureException
    • findManagedVersion

      private String findManagedVersion(org.apache.maven.project.MavenProject project, String groupId, String artifactId)
    • 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

      private eu.maveniverse.domtrip.maven.PomEditor.Dependencies dependenciesFor(eu.maveniverse.domtrip.maven.PomEditor editor, File pomFile, String profileId) throws org.apache.maven.plugin.MojoFailureException
      Throws:
      org.apache.maven.plugin.MojoFailureException
    • 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

      private static eu.maveniverse.domtrip.Element findDependency(eu.maveniverse.domtrip.maven.PomEditor editor, String profileId, String groupId, String artifactId, String type, String classifier, boolean managed)
    • getDependenciesElement

      private static eu.maveniverse.domtrip.Element getDependenciesElement(eu.maveniverse.domtrip.maven.PomEditor editor, String profileId, boolean managed)
    • getDependencyVersions

      private static List<String> getDependencyVersions(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed)
    • 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

      private File findManagedDepsPom(org.apache.maven.project.MavenProject project)
      Walks the parent chain to find the nearest POM that declares <dependencyManagement>.
    • detectPropertyPattern

      static AddDependencyMojo.PropertyPattern detectPropertyPattern(List<String> versions)
      Analyzes property reference names to detect the naming convention.
      Returns:
      the most common AddDependencyMojo.PropertyPattern, or null if no clear pattern is found