Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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