Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude dependency in a profile

Tags:

I have a maven module which has some dependencies. In a certain profile, I want to exclude some of those dependencies (to be exact, all dependencies with a certain group id). They however need to be present in all other profiles. Is there a way to specify exclusions from the dependencies for a profile?

like image 389
Thomas Lötzer Avatar asked Nov 24 '09 13:11

Thomas Lötzer


People also ask

What is exclusion in dependency?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.

How do you avoid optional dependency?

In order to exclude these special dependencies from the main project, we can apply Maven's <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn't need them.

What is optional in Maven dependency?

While adding dependencies, we can use the optional flag, or set the scope to “provided”. In both cases, the dependencies will be in the classpath of the module where they are declared, but they will not be added transitively in other projects using the module that defines them as dependencies.


1 Answers

To my knowledge, no, you can't deactivate dependencies (you can exclude transitive dependencies but this is not what you are asking for) and yes, what you are currently doing with the POM (manually editing it) is wrong.

So, instead of removing dependencies, you should put them in a profile and either:

  • Option #1: use the profile when required or
  • Option #2: mark the profile as activated by default or put it in the list of active profiles and deactivate it when required.

A third option would be (not profile based):

  • Option #3: separate things in two separated modules (as you have separated concerns) and use inheritance.
like image 103
Pascal Thivent Avatar answered Sep 20 '22 13:09

Pascal Thivent