Maven 3 plugin - How to programatically exclude a dependency and all its transitive dependencies?
- by electrotype
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?