Question

If I have one project of mine myproject1 that has the following depenedencies:

        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.1</version>
            <scope>test</scope>
        </dependency>

And i include myproject1 in myproject2 like so:

    <dependency>
        <groupId>com.mydomain</groupId>
        <artifactId>myproject1</artifactId>
        <version>1.0</version>
    </dependency>
    <!-- Android Logging-->
    <dependency>
        <groupId>com.github.tony19</groupId>
        <artifactId>logback-android-core</artifactId>
        <version>1.1.1-2</version>
    </dependency>
    <dependency>
        <groupId>com.github.tony19</groupId>
        <artifactId>logback-android-classic</artifactId>
        <version>1.1.1-2</version>
    </dependency>

I am still seeing logback classes being included in myproject2. My understanding is that the test scope should prevent those jars from bleeding into parent projects. Am I misunderstanding something? I am currently running into compilation issues as my parent project relies on logback-android and so I have multiple classes with the same name e.g. ch.qos.logback.classic.AsyncAppender.

Was it helpful?

Solution

You can exclude transitive dependencies from myproject1 and you have two options here.

Optional dependency. Mark logback-core in myproject1 as optional:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.1</version>
    <scope>test</scope>
    <optional>true</optional>
</dependency>

Excluded dependency. Exclude logback-core when including myproject1:

<dependency>
    <groupId>com.mydomain</groupId>
    <artifactId>myproject1</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Also I think if you run build with tests off (-Dmaven.test.skip=true) logback-core will not be included anyway.

Run mvn dependency:tree to see origin of your dependencies and their scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top