如何排除某些依赖项被添加到Maven的类路径中? [英] How to exclude certain dependencies from being added to the Class-Path in maven?

查看:151
本文介绍了如何排除某些依赖项被添加到Maven的类路径中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用maven来构建可执行文件JAR,我想将所有依赖项减去选定的几项添加到我的MANIFEST.MFClass-Path中.到目前为止,我正在为此使用maven-jar-plugin

I'm using maven to build an executable JAR and I want to add all dependencies minus a select few to the Class-Path of my MANIFEST.MF. So far I'm using the maven-jar-plugin for this:

<!-- Add dependent JARs to the classpath -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libraries</classpathPrefix>
                <mainClass>MyMainClass</mainClass>
            </manifest>
            <manifestEntries>
                <Built-By>Me</Built-By>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

这适用于添加所有依赖项.但是,我还想排除某些依赖项,以免它们被添加到Class-Path中.根据文档,您可以使用exclude标记如下:

This works for adding all dependencies. However, I furthermore want to exclude certain dependencies from being added to the Class-Path. According to the documentation you can use the exclude tag as follows:

<configuration>
    <!-- ... -->
    <excludes>
        <exclude>**selenium*</exclude>
    </excludes>
    <!-- ... -->
</configuration>

我希望这会排除名称中具有selenium但不起作用的任何依赖项.例如,我想从Class-Path中排除libraries/selenium-json-4.0.0-alpha-6.jar.即使我指定了确切的名称,它也不排除任何内容.我还想提供groupId排除项,类似于maven-dependency-pluginexcludeGroupIds标记的工作方式.

I would expect this to exclude any dependency which has selenium in the name but it does not work. For example I would like to exclude libraries/selenium-json-4.0.0-alpha-6.jar from the Class-Path. Even if I specify that exact name it does not exclude anything. I would also like to provide the groupId for exclusion similar to how the maven-dependency-plugin's excludeGroupIds tag works.

如何使用maven完成所需的Class-Path管理?

How can the desired Class-Path management be done using maven?

我也在使用maven-shade-plugin来生成可执行文件(胖)JAR,但是它的清单操作工具似乎鲜为人知/已记录.通过答案使用maven-shade-plugin设置Class-Path可行,但是如何用排除项而不是强制性的填充我的依赖项?编码一切吗?

I'm also using maven-shade-plugin for building the executable (fat) JAR but its manifest manipulation facilities seem lesser known/documented. Setting the Class-Path using maven-shade-plugin via this answer works but how can I populate my dependencies with exclusions instead of hard-coding everything?

推荐答案

TL; DR

Maven JAR插件的includesexcludes参数适用于文件,而不适用于依赖项.

TL;DR

The includes and excludes parameters of the Maven JAR Plugin work on files, not on dependencies.

硒通常用作测试依赖项. Selenium是否声明为Maven依赖项?如果仅用于测试,我希望它具有<scope>test</scope>.在这种情况下,它不会最终出现在JAR清单中.

Selenium is typically used as a test dependency. Is Selenium declared as a Maven dependency? If it's only used for tests, I would expect it to have <scope>test</scope>. In that case, it will not end up in the JAR manifest.

如果您不使用Selenium作为测试依赖项,但仍然不希望它最终出现在清单中,则可以考虑添加<scope>provided</scope>.提供的依赖项也不会出现在JAR清单中.这也意味着在运行时,一旦调用Selenium API,它就有可能崩溃...

If you don't use Selenium as a test dependency and still don't want it to end up in the manifest, you may consider adding <scope>provided</scope>. Provided dependencies don't end up in the JAR manifest either. This would also mean that at runtime, as soon as you invoke a Selenium API it will probably crash...

这篇关于如何排除某些依赖项被添加到Maven的类路径中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆