Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a dependency for a specific scope only?

I have two dependencies in my pom called A and B. Both A and B have a transitive dependency on an artifact C (cassandra-all). A and B use difference versions of C. Dependency A is the artifact astyanax.

I want to keep the Version of C that comes with B. I accomplished by adding an exclusion in A (Astyanax) for C.

Unfortunately, I want the scope of B to be 'test'. This means that with the exclusion in A, C will not be included outside of the test scope.

How can I resolve this? Can an exclusion be for a specific scope only? Alternatively, can I specify which version to use for a transitive dependency?


Example:
Here is what my pom looks like:

Artifact A (astyanax) with exclusion of dependency on Artifact C (called cassandra-all)

    <dependency>
        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax</artifactId>
        <version>1.0.4</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.cassandra</groupId>
                <artifactId>cassandra-all</artifactId>
            </exclusion>
        </exclusions>  
    </dependency>
    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit</artifactId>
        <version>1.1.1.1</version>
        <scope>test</scope>
    </dependency>

So concretely: how can I include cassandra-all when I run code outside of the test scope and still keep the scope of cassandraunit test only?

like image 729
Lolo Avatar asked Nov 26 '12 20:11

Lolo


People also ask

How do I exclude a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

Can we exclude a class from Maven dependency?

Since Maven resolves dependencies transitively, it is possible for unwanted dependencies to be included in your project's classpath. For example, a certain older jar may have security issues or be incompatible with the Java version you're using. To address this, Maven allows you to exclude specific dependencies.

How do you exclude a dependency globally?

Conclusion – By using the <scope>provided</scope> for a dependency you can globally exclude the dependency from getting packaged in the application lib instead it's loaded by the server classloader thus avoiding any LinkageError or ClassCastExceptions from arising due to jar conflicts.


2 Answers

I apologize if my question wasn't as clear as it could have been. The way I resolved this wasn't hard at all:

  • I added a separate dependency for C in my pom
  • I kept the exclusion of C in A

Concretely here, I just added:

    <dependency>
        <groupId>org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>1.1.5</version>
    </dependency>

and also the following dependency that was missing at runtime otherwise.

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
like image 60
Lolo Avatar answered Nov 04 '22 09:11

Lolo


I am not sure I understood everything, but, in any case, you should be able to achieve this with profiles.

In your pom, create a profile A in which you add your dependency A with exclusion of B and a profile B in which you'll have a dependency with exclusion of A.

On runtime, depending on which of the profile you have selected you'll include one or the other.

HIH

like image 42
poussma Avatar answered Nov 04 '22 11:11

poussma