How to get access to Maven's dependency hierarchy within a plugin

前端 未结 7 1661
生来不讨喜
生来不讨喜 2020-11-29 05:25

In my plugin I need to process the dependency hierarchy and get information (groupId, artifactId, version etc) about each dependency and if it was excluded. What is the best

7条回答
  •  清歌不尽
    2020-11-29 05:52

    You could use MavenProject#getDependencyArtifacts() or MavenProject#getDependencies() (the later one returns also transitive dependencies).

    /**
     * Test Mojo
     *
     * @goal test
     * @requiresDependencyResolution compile
     */
    public class TestMojo extends AbstractMojo {
    
        /**
         * The Maven Project.
         *
         * @parameter expression="${project}"
         * @required
         * @readonly
         */
        private MavenProject project = null;
    
        /**
         * Execute Mojo.
         *
         * @throws MojoExecutionException If an error occurs.
         * @throws MojoFailureException If an error occurs.
         */
        public void execute() throws MojoExecutionException,
    MojoFailureException {
    
            ...
    
            Set dependencies = project.getDependencies();
    
           ...
        }
    
    }
    

    I'm not totally sure but I think both methods return a collection of Artifact implementations that expose getters for groupId, artifactId, version, etc.

提交回复
热议问题