Kamil
Kamil

Reputation: 2288

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)?

Upvotes: 7

Views: 7245

Answers (2)

Dennis
Dennis

Reputation: 3741

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.

Upvotes: 3

FrVaBe
FrVaBe

Reputation: 49371

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.

Upvotes: 15

Related Questions