java 从 jar Maven 依赖项中排除 .class 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31240913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:19:08  来源:igfitidea点击:

excluding .class file from jar maven dependency

javamaven

提问by Raaji

I had 3 maven projects A,B,C. A is dependent on B which in-turn depends on C. A's POM has dependency for B and B's POM has dependency for C. I want to exclude a class file in C when building A.

我有 3 个 maven 项目 A、B、C。A 依赖于 B,而 B 又依赖于 C。A 的 POM 依赖于 B,B 的 POM 依赖于 C。我想在构建 A 时排除 C 中的类文件。

How can I do this ? I tried doing this using excludes of maven-jar-plugin, but not able to succeed.

我怎样才能做到这一点 ?我尝试使用排除 maven-jar-plugin 来执行此操作,但无法成功。

回答by codeaholicguy

Use maven-jar-plugin

利用 maven-jar-plugin

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
      <execution>
        <id>default-jar</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        <configuration>
          <excludes>
            <exclude><!-- package you want to exclude --></exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
</plugin>

回答by coderplus

There is no quick and easy way of doing this. If you add a JAR as a dependency to your project, the JAR will be added to the test or compile classpath(based on the scope you provide).

没有快速简便的方法可以做到这一点。如果您将 JAR 作为依赖项添加到您的项目,JAR 将被添加到测试或编译类路径中(基于您提供的范围)。

The maven-dependency-pluginprovides an unpackgoal which can be used to unpack artifacts to a specific directory.

maven-dependency-plugin提供了一个unpack可用于向解压缩伪影,以一个特定的目录目标。

As a work around for your problem, you can unpack artifact C into target/classes of A during build. The unpackgoal provides a way to exclude certain file patterns (so you can exclude specific classes).

作为解决您问题的方法,您可以在构建期间将工件 C 解压缩到 A 的目标/类中。该unpack目标提供了一种排除某些文件模式的方法(因此您可以排除特定的类)。

Also make sure that C JAR is not added to A's classpath(Adjust your dependencies accordingly - probably add a dependency for C of type pominstead of jar).

还要确保 C JAR 没有添加到 A 的类路径中(相应地调整您的依赖项 - 可能为 C 类型添加一个依赖项pom而不是jar)。

https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html

https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html

How to exclude C jar from A without changing B

如何在不改变 B 的情况下从 A 中排除 C jar

    <dependency>
        <artifactId>B</artifactId>
        <groupId>bgroupId</groupId>
        <version>bverison</version>
        <exclusions>
            <exclusion>
                <artifactId>C</artifactId>
                <groupId>cgroupId</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
      <artifactId>C</artifactId>
        <groupId>cgroupId</groupId>
        <version>cverison</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>

Now you can use the dependency plugin to unpack the C JAR appropriately(excluding certain files)

现在您可以使用依赖插件适当地解压 C JAR(不包括某些文件)