I did not find an existing method for this either so I'm using the following logic. This is a plugin building a customized ear, which adds the needed dependencies to an xml file and include them in the archive. It is using getArtifacts
instead of getDependencyArtifacts
since I'm also interested in transitive dependencies.
Collection<Artifact> dependencies = new ArrayList<Artifact>();
dependencies.addAll(project.getArtifacts());
for (Iterator<Artifact> it=dependencies.iterator(); it.hasNext(); ) {
Artifact dependency = it.next();
String scope = dependency.getScope();
String type = dependency.getType();
if (dependency.isOptional() || !"jar".equals(type) || "provided".equals(scope) || "test".equals(scope) || "system".equals(scope)) {
getLog().debug("Pruning dependency " + dependency);
it.remove();
}
}