2

I have my own slf4j implementation so I want to remove logback from many dependencies, e.g.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>...</version>
        <exclusions>
            <exclusion>
                <artifactId>logback-classic</artifactId>
                <groupId>ch.qos.logback</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    ...

But I find a quick way by adding it as test scope:

    <dependency>
        <artifactId>logback-classic</artifactId>
        <groupId>ch.qos.logback</groupId>
        <version>...</version>
        <scope>test</scope>
    </dependency>

Is this a good practice in maven?

auntyellow
  • 2,423
  • 2
  • 20
  • 47
  • probably not a good practice but certainly a nice hack !!! See http://stackoverflow.com/questions/4716310/is-there-a-way-to-exclude-a-maven-dependency-globally for a similar alternative – Gab Feb 14 '17 at 16:35

1 Answers1

4

I can see how test scope does the trick, but I would personally oppose from such hacks. If your team loose discipline and start incorporating hacks into your application just to save few lines of code, your application will be unmaintainable pretty soon.

You also shouldn't specify versions of spring managed dependencies. You should just define Spring boot version in parent section of your pom. You can find matrix of spring managed dependencies in spring-boot-dependencies file.

I would suggest excluding spring-boot-starter-logging instead of logback directly. So if you are using log4j2 (hopefully you are not planning to use log4j anymore), just do this:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
luboskrnac
  • 23,973
  • 10
  • 81
  • 92