Class 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
    • Field Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addCrossPom​(DependencyEntry coords, java.io.File childPomFile, java.io.File parentPomFile, boolean effectiveUseProperty, java.lang.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, java.io.File pomFile, java.lang.String profileId, DependencyEntry coords, boolean managed)  
      private void addSinglePom​(DependencyEntry coords, org.apache.maven.project.MavenProject targetProject, java.io.File pomFile, boolean targetManaged, boolean effectiveUseProperty, java.lang.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, java.io.File pomFile, java.lang.String profileId)  
      private AddDependencyMojo.Conventions detectConventions​(org.apache.maven.project.MavenProject project)
      Detects the project's dependency management conventions by analyzing existing dependencies.
      (package private) static AddDependencyMojo.PropertyPattern detectPropertyPattern​(java.util.List<java.lang.String> versions)
      Analyzes property reference names to detect the naming convention.
      protected void doExecute()  
      private static eu.maveniverse.domtrip.Element findDependency​(eu.maveniverse.domtrip.maven.PomEditor editor, java.lang.String profileId, java.lang.String groupId, java.lang.String artifactId, java.lang.String type, java.lang.String classifier, boolean managed)  
      private java.io.File findManagedDepsPom​(org.apache.maven.project.MavenProject project)
      Walks the parent chain to find the nearest POM that declares <dependencyManagement>.
      private java.lang.String findManagedVersion​(org.apache.maven.project.MavenProject project, java.lang.String groupId, java.lang.String artifactId)  
      private static eu.maveniverse.domtrip.Element getDependenciesElement​(eu.maveniverse.domtrip.maven.PomEditor editor, java.lang.String profileId, boolean managed)  
      private static long getDependencyCount​(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed)  
      private static java.util.List<java.lang.String> getDependencyVersions​(eu.maveniverse.domtrip.maven.PomEditor editor, boolean managed)  
      private static eu.maveniverse.domtrip.maven.PomEditor loadPomEditor​(java.io.File pomFile)  
      private DependencyEntry resolveCoordinates()  
      private static void savePomEditor​(eu.maveniverse.domtrip.maven.PomEditor editor, java.io.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 java.lang.Object

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

      • gav

        @Parameter(property="gav")
        private java.lang.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 java.lang.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 java.lang.String type
        Dependency type/packaging (e.g., jar, pom, war).
        Since:
        3.11.0
      • classifier

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

        @Parameter(property="optional")
        private java.lang.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 java.lang.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 java.lang.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 java.lang.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 java.util.regex.Pattern PROPERTY_REF
    • Constructor Detail

      • AddDependencyMojo

        @Inject
        public AddDependencyMojo​(org.apache.maven.execution.MavenSession session,
                                 org.sonatype.plexus.build.incremental.BuildContext buildContext,
                                 org.apache.maven.project.MavenProject project)
    • Method Detail

      • 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,
                                 java.io.File childPomFile,
                                 java.io.File parentPomFile,
                                 boolean effectiveUseProperty,
                                 java.lang.String effectivePropertyName)
                          throws java.io.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:
        java.io.IOException
        org.apache.maven.plugin.MojoFailureException
      • addSinglePom

        private void addSinglePom​(DependencyEntry coords,
                                  org.apache.maven.project.MavenProject targetProject,
                                  java.io.File pomFile,
                                  boolean targetManaged,
                                  boolean effectiveUseProperty,
                                  java.lang.String effectivePropertyName)
                           throws java.io.IOException,
                                  org.apache.maven.plugin.MojoFailureException
        Single-POM mode: adds the dependency (optionally with a version property) to the current POM.
        Throws:
        java.io.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 java.lang.String findManagedVersion​(org.apache.maven.project.MavenProject project,
                                                    java.lang.String groupId,
                                                    java.lang.String artifactId)
      • addDependency

        private void addDependency​(eu.maveniverse.domtrip.maven.PomEditor editor,
                                   java.io.File pomFile,
                                   java.lang.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,
                                                                                    java.io.File pomFile,
                                                                                    java.lang.String profileId)
                                                                             throws org.apache.maven.plugin.MojoFailureException
        Throws:
        org.apache.maven.plugin.MojoFailureException
      • loadPomEditor

        private static eu.maveniverse.domtrip.maven.PomEditor loadPomEditor​(java.io.File pomFile)
                                                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • savePomEditor

        private static void savePomEditor​(eu.maveniverse.domtrip.maven.PomEditor editor,
                                          java.io.File pomFile)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • findDependency

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

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

        private static java.util.List<java.lang.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 java.io.File findManagedDepsPom​(org.apache.maven.project.MavenProject project)
        Walks the parent chain to find the nearest POM that declares <dependencyManagement>.