Reputation: 99
I have project B inherit from project A. In Project A, I have to copy jar file downloaded from Maven into lib folder. Project A's pom file :
<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>
When i build, maven copied jar file to lib folder but it also create lib folder in Project B and copy jar file defined in Project A's pom file into it. Now i want to exclude it in project B. I tried with this script below but it doesn't work.
<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>
I refer this link: Is there anyway to exclude artifacts inherited from a parent POM?. But it doesn't help me also.
Upvotes: 3
Views: 12149
Reputation: 163
You exclusion was listed in the wrong place. You want to exclude the artifact from the maven-dependency-plugin's configuration, not from the project dependencies. The magic ingredient here is the attribute combine.self="override"
From Maven Pom Reference: "You can control how child POMs inherit configuration from parent POMs by adding attributes to the children of the configuration element. The attributes are combine.children and combine.self. Use these attributes in a child POM to control how Maven combines plugin configuration from the parent with the explicit configuration in the child."
You need to change the setup in your child pom to something like this:
<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>
I'm not sure if it's possible to just exclude a particular artifact, rather than over-riding the entire artifactItems declaration, that is what I was looking for when I stumbled on your question. To get around this, my setup will likely involve multi pom inheritance, as we list close to 100 artifacts in our plugin configuration and I only want to exclude and swap in different implementations for a few artifacts in my child pom.
Upvotes: 0
Reputation: 12345
Although I have my doubts if your project structure is correct (I can't think of a good example where a parent pom should copy dependencies...), but you are probably looking for the inherited-tag
Upvotes: 3