Java 无论如何要排除从父 POM 继承的工件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2681759/
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-08-13 10:45:00  来源:igfitidea点击:

Is there anyway to exclude artifacts inherited from a parent POM?

javamaven-2pom.xml

提问by Miguel

Artifacts from dependencies can be excluded by declaring an <exclusions>element inside a <dependency>But in this case it's needed to exclude an artifact inherited from a parent project. An excerpt of the POM under discussion follows:

可以通过<exclusions>在 a 中声明一个元素来排除依赖项中的工件,<dependency>但在这种情况下,需要排除从父项目继承的工件。正在讨论的 POM 摘录如下:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>jruby</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <artifactId>base</artifactId>
        <groupId>es.uniovi.innova</groupId>
        <version>1.0.0</version>
    </parent>

    <dependencies>      
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>ALL-DEPS</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>

baseartifact, depends on javax.mail:mail-1.4.jar, and ALL-DEPSdepends on another version of the same library. Due to the fact that mail.jarfrom ALL-DEPSexist on the execution environment, although not exported, collides with the mail.jarthat exists on the parent, which is scoped as compile.

base工件,取决于 javax.mail:mail-1.4.jar,并且ALL-DEPS取决于同一库的另一个版本。由于mail.jarfromALL-DEPS存在于执行环境中,虽然未导出,但与mail.jar存在于父级上的 发生冲突,其作用域为compile

A solution could be to rid off mail.jar from the parent POM, but most of the projects that inherit base, need it (as is a transtive dependency for log4j). So What I would like to do is to simply exclude parent's library from the child project, as it could be done if basewas a dependency and not the parent pom:

一个解决方案可能是从父 POM 中删除 mail.jar,但大多数继承 base 的项目都需要它(就像 log4j 的传递依赖项)。所以我想做的是简单地从子项目中排除父级的库,因为如果base是依赖项而不是父 pom就可以做到:

...
    <dependency>
        <artifactId>base</artifactId>
        <groupId>es.uniovi.innova</groupId>
        <version>1.0.0</version>
        <type>pom<type>
        <exclusions>
          <exclusion>
             <groupId>javax.mail</groupId>
             <artifactId>mail</artifactId>
          </exclusion>
        </exclusions>
    </dependency>
...

采纳答案by Pascal Thivent

Some ideas:

一些想法:

  1. Maybe you could simply not inherit from the parent in that case (and declare a dependency on basewith the exclusion). Not handy if you have lot of stuff in the parent pom.

  2. Another thing to test would be to declare the mailartifact with the version required by ALL-DEPSunder the dependencyManagementin the parent pom to force the convergence (although I'm not sure this will solve the scoping problem).

  1. 也许在这种情况下你不能从父级继承(并声明依赖于base排除)。如果父 pom 中有很多东西,那就不太方便了。

  2. 另一件事是测试将是宣布mail与所需的版本神器ALL-DEPS之下dependencyManagement父POM强制收敛(虽然我不知道这将解决作用域问题)。

<dependencyManagement>
  <dependencies>
    <dependency>    
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>???</version><!-- put the "right" version here -->
    </dependency>
  </dependencies>
</dependencyManagement>
  1. Or you could exclude the maildependency from log4j if you're not using the features relying on it (and this is what I would do):
  1. 或者mail,如果您不使用依赖它的功能,则可以从 log4j 中排除依赖项(这就是我要做的):
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.15</version>
  <scope>provided</scope>
  <exclusions>
    <exclusion>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
    </exclusion>
    <exclusion>
      <groupId>javax.jms</groupId>
      <artifactId>jms</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.jdmk</groupId>
      <artifactId>jmxtools</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.jmx</groupId>
      <artifactId>jmxri</artifactId>
    </exclusion>
  </exclusions>
</dependency>
  1. Or you could revert to the version 1.2.14 of log4j instead of the heretic 1.2.15 version (why didn't they mark the above dependencies as optional?!).
  1. 或者您可以恢复到 log4j 的 1.2.14 版本而不是异端的 1.2.15 版本(为什么他们不将上述依赖项标记为可选?!)。

回答by porterhouse91

Have you tried explicitly declaring the version of mail.jar you want? Maven's dependency resolution should use this for dependency resolution over all other versions.

您是否尝试过明确声明所需的 mail.jar 版本?Maven 的依赖解析应该使用它来解决所有其他版本的依赖。

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>jruby</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <artifactId>base</artifactId>
        <groupId>es.uniovi.innova</groupId>
        <version>1.0.0</version>
    </parent>
    <dependencies>          
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>VERSION-#</version>
            <scope>provided</scope>
        </dependency> 
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>ALL-DEPS</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>

回答by Ajax

Best bet is to make the dependencies you don't always want to inherit intransitive.

最好的办法是使您并不总是想继承的依赖项不传递。

You can do this by marking them in the parent pom with scope provided.

您可以通过在提供范围的父 pom 中标记它们来做到这一点。

If you still want the parent to manage versions of these deps, you can use the <dependencyManagement>tag to setup the versions you want without explicitly inheriting them, or passing that inheritance along to children.

如果你仍然希望父级管理这些 deps 的版本,你可以使用<dependencyManagement>标签来设置你想要的版本,而无需显式继承它们,或者将该继承传递给子级。

回答by timomeinen

You can group your dependencies within a different project with packaging pomas described by Sonatypes Best Practices:

您可以pom按照 Sonatypes Best Practices 的描述,使用打包将不同项目中的依赖项分组:

<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>base-dependencies</artifactId>
    <groupId>es.uniovi.innova</groupId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <dependencies>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
</project>

and reference them from your parent-pom (watch the dependency <type>pom</type>):

并从您的 parent-pom 中引用它们(注意依赖项<type>pom</type>):

<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>base</artifactId>
    <groupId>es.uniovi.innova</groupId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <dependencies>
        <dependency>
            <artifactId>base-dependencies</artifactId>
            <groupId>es.uniovi.innova</groupId>
            <version>1.0.0</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>

Your child-project inherits this parent-pom as before. But now, the mail dependency can be excluded in the child-project within the dependencyManagementblock:

您的子项目像以前一样继承了这个 parent-pom。但是现在,可以在dependencyManagement块内的子项目中排除邮件依赖项:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>jruby</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <artifactId>base</artifactId>
        <groupId>es.uniovi.innova</groupId>
        <version>1.0.0</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <artifactId>base-dependencies</artifactId>
                <groupId>es.uniovi.innova</groupId>
                <version>1.0.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>javax.mail</groupId>
                        <artifactId>mail</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

回答by bastienb1991

When you call a package but do not want some of its dependencies you can do a thing like this (in this case I did not want the old log4j to be added because I needed to use the newer one):

当你调用一个包但不想要它的一些依赖项时,你可以做这样的事情(在这种情况下,我不想添加旧的 log4j,因为我需要使用更新的):

<dependency>
  <groupId>package</groupId>
  <artifactId>package-pk</artifactId>
  <version>${package-pk.version}</version>

  <exclusions>
    <exclusion>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<!-- LOG4J -->
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.5</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.5</version>
</dependency>

This works for me... but I am pretty new to java/maven so it is maybe not optimum.

这对我有用......但我对 java/maven 很陌生,所以它可能不是最佳的。

回答by Sridhar Sarnobat

Don't use a parent pom

不要使用父 pom

This might sound extreme, but the same way "inheritance hell" is a reason some people turn their backs on Object Oriented Programming (or prefer composition over inheritance), remove the problematic <parent>block and copy and pastewhatever <dependencies>you need (if your team gives you this liberty).

这听起来可能很极端,但同样的“继承地狱”也是一些人拒绝面向对象编程(或者更喜欢组合而不是继承)的原因,删除有问题的<parent>块并复制和粘贴任何<dependencies>你需要的东西(如果你的团队给你这种自由)。

The assumption that splitting of poms into a parent and child for "reuse" and "avoidance of redunancy" should be ignored and you should serve your immediate needs first (the cure is worst than the disease). Besides, redundancy has its advantages - namely independence of external changes (i.e stability).

应该忽略将 poms 分成父子和子子以“重用”和“避免冗余”的假设,您应该首先满足您的直接需求(治愈比疾病更糟糕)。此外,冗余还有其优点——即独立于外部变化(即稳定性)。

This is easier than it sounds if you generate the effective pom (eclipse provides it but you can generate it from the command line with mvn help:effective).

如果您生成有效的 pom,这比听起来容易(eclipse 提供了它,但您可以从命令行使用 生成它mvn help:effective)。

Example

例子

I want to use logbackas my slf4j binding, but my parent pom includes the log4jdependency. I don't want to go and have to push the other children's dependence on log4j down into their own pom.xmlfiles so that mine is unobstructed.

我想logback用作我的 slf4j 绑定,但我的父 pom 包含log4j依赖项。我不想去,不得不把其他孩子对 log4j 的依赖推到他们自己的pom.xml文件中,这样我的文件就畅通无阻了。

回答by Bax

Redefine the dependency (in the child pom) with scopesystem pointing to an empty jar :

使用scopesystem 指向一个空 jar重新定义依赖项(在子 pom 中):

<dependency>
    <groupId>dependency.coming</groupId>
    <artifactId>from.parent</artifactId>
    <version>0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/empty.jar</systemPath>
</dependency>

The jar can contain just a single empty file :

jar 可以只包含一个空文件:

touch empty.txt
jar cvf empty.txt

回答by Yan Khonski

I really needed to do this dirty thing... Here is how

我真的需要做这件肮脏的事情……这是怎么做的

I redefined those dependencies with scope test. Scope provideddid not work for me.

我用 scope 重新定义了这些依赖项test。范围provided对我不起作用。

We use spring Boot plugin to build fat jar. We have module commonwhich defines common libraries, for example Springfox swagger-2. My super-serviceneeds to have parent common(it does not want to do so, but company rules force!)

我们使用spring Boot插件来构建fat jar。我们有模块common定义了公共库,例如 Springfox swagger-2。我的超级服务需要有父公用(它不想这样做,但公司规则强制!)

So my parent or commons has pom.

所以我的父母或公地有 pom。

<dependencyManagement>

    <!- I do not need Springfox in one child but in others ->

    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>${swagger.version}</version>
        </dependency>

       <!- All services need them ->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${apache.poi.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

And my super-servicepom.

还有我的超级服务pom。

<name>super-service</name>
<parent>
    <groupId>com.company</groupId>
    <artifactId>common</artifactId>
    <version>1</version>
</parent>

<dependencies>

    <!- I don't need them ->

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-bean-validators</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-core</artifactId>
        <version>2.8.0</version>
        <scope>test</scope>
    </dependency>

    <!- Required dependencies ->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
    </dependency>
</dependencies>

This is size of the final fat artifact

这是最终脂肪神器的大小

82.3 MB (86,351,753 bytes) - redefined dependency with scope test
86.1 MB (90,335,466 bytes) - redefined dependency with scope provided
86.1 MB (90,335,489 bytes) - without exclusion

Also this answer is worth mentioning - I wanted to do so, but I am lazy... https://stackoverflow.com/a/48103554/4587961

这个答案也值得一提-我想这样做,但我很懒... https://stackoverflow.com/a/48103554/4587961

回答by Viki Jain

We can add the parent pom as a dependency with type pom and make exclusion on that. Because anyhow parent pom is downloaded. This worked for me

我们可以将父 pom 添加为 pom 类型的依赖项并对其进行排除。因为无论如何都下载了父pom。这对我有用

<dependency>
  <groupId>com.abc.boot</groupId>
  <artifactId>abc-boot-starter-parent</artifactId>
  <version>2.1.5.RELEASE</version>
  <type>pom</type>
  <exclusions>
    <exclusion>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
    </exclusion>
  </exclusions>   
</dependency>