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

后端 未结 9 1192
礼貌的吻别
礼貌的吻别 2020-11-30 22:02

Artifacts from dependencies can be excluded by declaring an element inside a But in this case it\'s needed to

相关标签:
9条回答
  • 2020-11-30 22:30

    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?!).
    0 讨论(0)
  • 2020-11-30 22:32

    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>
    
    0 讨论(0)
  • 2020-11-30 22:33

    Redefine the dependency (in the child pom) with scope system pointing to an empty jar :

    <dependency>
        <groupId>dependency.coming</groupId>
        <artifactId>from.parent</artifactId>
        <version>0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/empty.jar</systemPath>
    </dependency>
    

    The jar can contain just a single empty file :

    touch empty.txt
    jar cvf empty.txt
    
    0 讨论(0)
  • 2020-11-30 22:39

    We can add the parent pom as a dependency with type pom and make exclusion on that. Because anyhow parent pom is downloaded. This worked for me

    <dependency>
      <groupId>com.abc.boot</groupId>
      <artifactId>abc-boot-starter-parent</artifactId>
      <version>2.1.5.RELEASE</version>
      <type>pom</type>
      <exclusions>
        <exclusion>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
        </exclusion>
      </exclusions>   
    </dependency>
    
    0 讨论(0)
  • 2020-11-30 22:44

    Best bet is to make the dependencies you don't always want to inherit intransitive.

    You can do this by marking them in the parent pom with scope provided.

    If you still want the parent to manage versions of these deps, you can use the <dependencyManagement> tag to setup the versions you want without explicitly inheriting them, or passing that inheritance along to children.

    0 讨论(0)
  • 2020-11-30 22:45

    I really needed to do this dirty thing... Here is how

    I redefined those dependencies with scope test. Scope provided did not work for me.

    We use spring Boot plugin to build fat jar. We have module common which defines common libraries, for example Springfox swagger-2. My super-service needs to have parent common (it does not want to do so, but company rules force!)

    So my parent or commons has pom.

    <dependencyManagement>
    
        <!- I do not need Springfox in one child but in others ->
    
        <dependencies>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-bean-validators</artifactId>
                <version>${swagger.version}</version>
            </dependency>
    
           <!- All services need them ->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${apache.poi.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    And my super-service pom.

    <name>super-service</name>
    <parent>
        <groupId>com.company</groupId>
        <artifactId>common</artifactId>
        <version>1</version>
    </parent>
    
    <dependencies>
    
        <!- I don't need them ->
    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-core</artifactId>
            <version>2.8.0</version>
            <scope>test</scope>
        </dependency>
    
        <!- Required dependencies ->
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
         <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>
    </dependencies>
    

    This is size of the final fat artifact

    82.3 MB (86,351,753 bytes) - redefined dependency with scope test
    86.1 MB (90,335,466 bytes) - redefined dependency with scope provided
    86.1 MB (90,335,489 bytes) - without exclusion
    

    Also this answer is worth mentioning - I wanted to do so, but I am lazy... https://stackoverflow.com/a/48103554/4587961

    0 讨论(0)
提交回复
热议问题