【问题标题】:Exclude artifacts inherited from a parent POM?排除从父 POM 继承的工件?
【发布时间】:2013-08-20 07:19:33
【问题描述】:

我的项目 B 继承自项目 A。在项目 A 中,我必须将从 Maven 下载的 jar 文件复制到 lib 文件夹中。项目 A 的 pom 文件:

<groupId>com.penguin.com.projecta</groupId>
<artifactId>penguin-common--pom</artifactId>
<packaging>pom</packaging>
<version>${com.penguin.common.projecta}</version>
<name>Penguin COMMON POM</name>

<modules>
    <module>projectb</module>
</modules>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-jars</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <overWriteIfNewer>true</overWriteIfNewer>                
                        <artifactItems>
                            <artifactItem>
                                <groupId>ant-contrib</groupId>
                                <artifactId>ant-contrib</artifactId>
                                <version>1.1</version>
                                <type>jar</type>
                                <destFileName>ant-contrib.jar</destFileName>
                                <outputDirectory>lib</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>        
    </plugins>
</build>

当我构建时,maven 将 jar 文件复制到 lib 文件夹,但它也在项目 B 中创建 lib 文件夹,并将项目 A 的 pom 文件中定义的 jar 文件复制到其中。现在我想在项目 B 中排除它。我尝试使用下面的这个脚本,但它不起作用。

<dependencies>
    <dependency>
        <groupId>com.penguin.com.projecta</groupId>
        <artifactId>penguin-common--pom</artifactId>
        <version>${com.penguin.common.projecta}</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>ant-contrib</groupId> 
                <artifactId>ant-contrib</artifactId>                        
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

我参考这个链接:Is there anyway to exclude artifacts inherited from a parent POM?。但这对我也没有帮助。

【问题讨论】:

    标签: maven maven-3 maven-plugin


    【解决方案1】:

    虽然我怀疑你的项目结构是否正确(我想不出一个父 pom 应该复制依赖项的好例子......),但你可能正在寻找inherited-tag

    【讨论】:

      【解决方案2】:

      您的排除项列在错误的位置。您想从 maven-dependency-plugin 的配置中排除工件,而不是从项目依赖项中排除。这里的神奇成分是属性combine.self="override"

      来自Maven Pom Reference: “您可以通过向配置元素的子元素添加属性来控制子 POM 如何从父 POM 继承配置。属性是 combine.children 和 combine.self。在子 POM 中使用这些属性来控制 Maven 如何从父级在子级中具有显式配置。”

      您需要将子 pom 中的设置更改为以下内容:

      <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-dependency-plugin</artifactId>
              <executions>
                  <execution>
                      <id>copy-jars</id>
                      <phase>process-sources</phase>
                      <goals>
                          <goal>copy</goal>
                      </goals>
                      <configuration>
                          <overWriteIfNewer>true</overWriteIfNewer>                
                          <artifactItems combine.self="override">
      
                          </artifactItems>
                      </configuration>
                  </execution>
              </executions>
          </plugin>        
      </plugins>
      

      我不确定是否可以仅排除特定的工件,而不是覆盖整个 artifactItems 声明,这就是我偶然发现您的问题时所寻找的。为了解决这个问题,我的设置可能会涉及多 pom 继承,因为我们在插件配置中列出了近 100 个工件,我只想在我的子 pom 中排除和交换一些工件的不同实现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-28
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 2012-12-22
        • 2012-03-17
        • 2016-12-17
        • 2016-04-11
        相关资源
        最近更新 更多