反正有没有排除从父POM继承的工件?

Mig*_*uel 113 java maven-2 pom.xml

可以通过<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>
Run Code Online (Sandbox Code Playgroud)

base工件,取决于 javax.mail:mail-1.4.jar,并ALL-DEPS取决于同一个库的另一个版本.由于这样的事实mail.jar,从ALL-DEPS存在于执行环境,虽然没有出口,碰撞与mail.jar那对父母,这是作用域确定为存在compile.

解决方案可能是从父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>
...
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 46

一些想法:

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

  2. 要测试的另一件事是使用父pom 下的mail所需版本声明工件以强制收敛(虽然我不确定这将解决范围问题).ALL-DEPSdependencyManagement

<dependencyManagement>
  <dependencies>
    <dependency>    
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>???</version><!-- put the "right" version here -->
    </dependency>
  </dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)
  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>
Run Code Online (Sandbox Code Playgroud)
  1. 或者您可以恢复到log4j的版本1.2.14而不是异端的1.2.15版本(为什么他们不将上述依赖项标记为可选?!).


tim*_*nen 24

您可以使用pomSonatypes 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>
Run Code Online (Sandbox Code Playgroud)

并从您的父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>
Run Code Online (Sandbox Code Playgroud)

你的子项目像以前一样继承了这个父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>
Run Code Online (Sandbox Code Playgroud)

  • 为此+1,尽管子pom应该使用<dependencies>部分而不是<dependencyManagement>,因为后者用于管理父pom内的依赖关系的*版本*. (3认同)
  • 这也称为 BOM,又名物料清单 :-) (2认同)

Bax*_*Bax 9

使用scope系统指向空jar 重新定义依赖项(在子pom中):

<dependency>
    <groupId>dependency.coming</groupId>
    <artifactId>from.parent</artifactId>
    <version>0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/empty.jar</systemPath>
</dependency>
Run Code Online (Sandbox Code Playgroud)

该jar只能包含一个空文件:

touch empty.txt
jar cvf empty.txt
Run Code Online (Sandbox Code Playgroud)

  • 看起来是不好的做法 (2认同)

Sri*_*bat 8

不要使用父pom

这可能听起来很极端,但同样的方式"继承地狱"是一些人背弃面向对象编程的原因,或者外科医生通过冠状动脉搭桥手术让你活着的同样方式:删除有问题的<parent>块并复制并粘贴<dependencies>你需要的任何东西.

假设将poms分成父母和孩子以"重用"和"避免冗余"应该被忽略,你应该首先满足你的直接需求.冗余有其优点 - 即外部并发症的独立性.

如果您生成有效的pom(eclipse提供它但您可以从命令行生成它),这比听起来更容易.

我想使用,mvn help:effective但我的父pom使用logback,我不想去,必须将其他孩子对log4j的依赖推入他们自己的log4j文件,以便我的通畅.

你可以从父母那里得到很多,但有时你的父母不知道什么对你有好处.


por*_*e91 6

您是否尝试过明确声明所需的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>
Run Code Online (Sandbox Code Playgroud)


小智 5

在子 pom.xml 中重复父级的依赖项并在其中插入排除:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)