Maven插件工件过滤不重要的依赖项

发布于 2024-12-28 11:54:41 字数 236 浏览 6 评论 0原文

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

有没有一些方法可以过滤所有非运行时 jar?如果我只是获取范围并比较 scope.equals("runtime") 我将抛出编译和其他重要的依赖项。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2025-01-04 11:54:41

我也没有找到现有的方法,所以我使用以下逻辑。这是一个构建自定义 Ear 的插件,它将所需的依赖项添加到 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();
        }
    }

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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文