1

我正在开发一个 Maven 插件并使用该MavenProject对象来访问我的依赖项project.getDependencyArtifacts(),但这给了我所有的 jar,甚至是仅测试的 jar。

有什么方法可以过滤所有非运行时 jar 吗?如果我只是获得范围并进行比较,scope.equals("runtime")我将丢弃编译和其他重要的依赖项。

4

1 回答 1

1

我也没有找到现有的方法,所以我使用以下逻辑。这是一个构建自定义耳朵的插件,它将所需的依赖项添加到 xml 文件中并将它们包含在存档中。它使用getArtifacts而不是getDependencyArtifacts因为我也对传递依赖感兴趣。

    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();
        }
    }
于 2012-01-22T13:44:33.373 回答