Maven profile removing dependency
Asked Answered
S

2

7

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

Sporting answered 11/12, 2012 at 11:36 Comment(2)
Your question is about <dependencies> section, right?Yearlong
Andrew Logvinov - Righ, dependencies A,B,C are declared in <dependencies/> section, I want to remove dependency C using profile. Is that possible?Sporting
M
15

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.

Marquand answered 11/12, 2012 at 12:11 Comment(1)
That would be the best solution, I guess.Yearlong
F
3

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.

Fructose answered 12/3, 2015 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.