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

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


Some ideas:

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

  2. Another thing to test would be to declare the mail artifact with the version required by ALL-DEPS under the dependencyManagement in the parent pom to force the convergence (although I'm not sure this will solve the scoping problem).

<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 mail dependency from log4j if you're not using the features relying on it (and this is what I would do):
<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?!).


You can group your dependencies within a different project with packaging pom as described by 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>):

<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 dependencyManagement block:

<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>


Don't use a parent 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 paste whatever <dependencies> you need (if your team gives you this liberty).

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).

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).

Example

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