Exclude dependency in a profile

Viewed 23155

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?

6 Answers

maven is a tool, we can hack it.

  • maven runs fine if you have the same artifact + version defined as dependency twice.
  • define a profile that eliminates an artifact + version by changing it to another package we already have.

For example, in the pom.xml:

... other pom stuff ...
  <properties>
    <artifact1>artifact1</artifact1>
    <artifact2>artifact2</artifact2>
    <artifact1.version>0.4</artifact1.version>
    <artifact2.version>0.5</artifact2.version>
  </properties>

  <profile>
    <id>remove-artifact2</id>
    <properties>
      <artifact1>artifact1</artifact1>
      <artifact2>artifact1</artifact2>
      <artifact1.version>0.4</artifact1.version>
      <artifact2.version>0.4</artifact2.version>
    </properties>
  </profile>
  • Now if you install this pom.xml without the profile, artifact1:0.4 and artifact2:0.5 will be the dependency.
  • But if you install this pom.xml with the profile mvn -P remove-artifact2 The result pom.xml contains only artifact1:0.4

This comes quite handy during api migration where artifact are renamed and versions are not compatible.

Related