Maven 3 plugin - How to programatically exclude a dependency and all its transitive dependencies?

Posted by electrotype on Stack Overflow See other posts from Stack Overflow or by electrotype
Published on 2012-11-25T17:01:16Z Indexed on 2012/11/25 17:03 UTC
Read the original article Hit count: 197

Filed under:
|
|
|

I'm developing a Maven 3 plugin and I want to exclude some dependencies, and their transitive dependencies, when a configuration is set to true in the plugin.

I don't want to use <exclusions> in the POM itself, even in a profile. I want to exclude those dependencies programatically. In fact, what I want is to prevent the dependency jars to be included in the final war (I'm building a war), when a plugin configuration is set to true.

I tried :

@Mojo(requiresDependencyResolution=ResolutionScope.COMPILE, name="compileHook",defaultPhase=LifecyclePhase.COMPILE)
public class compileHook extends AbstractMojo
{
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException
    {
        // ...
        Set<Artifact> artifacts = this.project.getArtifacts();
        for(Artifact artifact : artifacts)
        {
            if("org.package.to.remove".equalsIgnoreCase(artifact.getGroupId()))
            {
                artifact.setScope("provided");
            }
        }
        // ...
    }
}

Since this occures at the compile phase, it will indeed remove the artifacts with a group id "org.package.to.remove" from having their jars included in the war when packaged. But this doesn't remove the transitive artifacts those dependencies add!

What is the best way to programatically remove some dependencies, and their transitive dependencies, from being included in a final .jar/.war?

© Stack Overflow or respective owner

Related posts about java

Related posts about maven