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)?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With