1

I'm developing a Maven plugin and using the MavenProject object to access my dependencies with project.getDependencyArtifacts(), but this gives my all jar, even the test only jars.

Is there some method to filter all non runtime jar? If I just get the scope and compare for scope.equals("runtime") I will throw out the compile and other important dependencies.

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
  • You might want to take a look at the source for the Maven War Plugin. In particular, the ArtifactsPackagingTask class handles the adding of artifacts to the war: http://svn.apache.org/viewvc/maven/plugins/tags/maven-war-plugin-2.1.1/src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java?view=markup – Brent Worden Jan 21 '12 at 17:17

1 Answers1

1

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();
        }
    }
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • getArtifacts returns an empty set. I execute it in another project in the package phase. Is there some trick to get that working? – Franz Kafka Jan 24 '12 at 14:41