Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven profile removing dependency

I have pom with declared dependencies A,B and C. Is it possible to create a profile which removes dependencies, so that when I compile with that profile, I end up for example with compiled dependency A and B (without C)?

like image 741
Kamil Avatar asked Dec 11 '12 11:12

Kamil


People also ask

How do I delete a dependency in Maven?

Open the pom file and click on dependency Hierarchy. Select the the jar you want to delete. Right click and click on Exclude Maven Artifact.

Is it possible to add library dependencies inside a profile in Maven?

Within each profile element, we can configure many elements such as dependencies, plugins, resources, finalName. So, for the example above, we could add plugins and their dependencies separately for integration-tests and mutation-tests.

How do I exclude a specific version of 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.


2 Answers

I do not know a way to exclude top level dependencies inside a profile (<exlusions> is only available for transitive dependencies). But you can specify your 'normal' dependencies in a default profile and your reduced dependencies in a seperate profile as for example:

<profiles>

    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.14</version>
            </dependency>
        </dependencies>
    </profile>

    <profile>
        <id>excludeDependency</id>
        <dependencies>
        </dependencies>
    </profile>

</profiles>

Compiling with the 'excludeDependency' profile will fail if you use log4j somewhere.

Your use case is not clear. Maybe some other solutions like optional dependencies or provided dependencies will also fit your needs. Have a look at these possibilities.

like image 120
FrVaBe Avatar answered Jan 11 '23 15:01

FrVaBe


In Maven the profile are additive to the base profile so I have found this pattern of usage good for excluding dependencies:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
</dependencies>

<profiles>
    <profile>
        <id>Exclude</id>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.14</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

Then you "exclude" the dependency by changing the scope to test. Doesn't suit all usages, but just as another option.

like image 37
Dennis Avatar answered Jan 11 '23 16:01

Dennis