How to exclude certain dependencies from being added to the Class-Path in maven?

前提是你 提交于 2020-06-17 09:43:24

问题


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>

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>

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.

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

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?


回答1:


TL;DR

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

Longer version

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.

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...



来源:https://stackoverflow.com/questions/62173831/how-to-exclude-certain-dependencies-from-being-added-to-the-class-path-in-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!