3

我有项目 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>

我参考了这个链接:无论如何要排除从父 POM 继承的工件?. 但这对我也没有帮助。

4

2 回答 2

3

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

于 2013-08-18T11:01:23.177 回答
0

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

来自Maven Pom 参考:“您可以通过向配置元素的子元素添加属性来控制子 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.xml 中为一些工件排除和交换不同的实现。

于 2013-11-30T00:13:03.020 回答