0

Is there a way I can set up dependancies according to a given condition when I buid the maven project.

mvn package someCondition

So if I say mvn install A it should add one dependancy and if a say mvn install B it should add another type of dependancy.

Please help. Thank You

P.S. Is there a way I can do this by creating multiple profiles?

Ravindu
  • 2,408
  • 8
  • 30
  • 46

1 Answers1

6

Using profile is the most straight-forward way.

in brief, consider having something like this:

<project>
  ...
  <profiles>
    <profile>
      <id>profile-a</id>
      <dependencies>
        <dependency>
          // dependency 1
        </dependency>
      </dependencies>
    </profile>
    <profile>
      <id>profile-b</id>
      <dependencies>
        <dependency>
          // dependency 2
        </dependency>
      </dependencies>
    </profile>
  <profiles>
</project>

Then you can simply do mvn install -P profile-a which will do what you ask for.

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • let's say I have some common dependencies. So instead of repeating then in both profiles is there a way to use them in both profiles – Ravindu Aug 07 '13 at 07:26
  • No straight-forward way in Maven. You may have a separate POM containing the shared dependencies, and set the dependency to that shared POM (using import scope maybe). However it does not necessary make the story easier. However, if it is shared across different profiles, why don't you just put in project's dependency then? Anyway, it is a unrelated discussion and if you want the way to deal with it, ask another question. – Adrian Shum Aug 07 '13 at 07:31