You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Carson Black <ca...@gmail.com> on 2012/05/06 01:48:24 UTC

inject dependencies programatically in maven

Is there a way to inject dependencies programatically in a maven plugin as
if they were declared in the POM itself?

Why do I want to do this?

Assume I have a fruit pom which imports a bom-pom which declares version
numbers for fruits, vegetables and other artifacts.  Example:

fruit pom:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com</groupId>
      <artifactId>foods</artifactId>
      <version>10.0-SNAPSHOT</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupid>com</groupId>
    <artifactId>apple</artifactId>
    <classifier>fruit</classifier>
  </dependency>
  <dependency>
    <groupid>com</groupId>
    <artifactId>orange</artifactId>
    <classifier>fruit</classifier>
  </dependency>
</dependencies>

food pom:

<dependencies>
  <dependency>
    <groupid>com</groupId>
    <artifactId>apple</artifactId>
    <version>1.0-SNAPSHOT</version>
    <classifier>fruit</classifier>
  </dependency>
  <dependency>
    <groupid>com</groupId>
    <artifactId>orange</artifactId>
    <version>2.0-SNAPSHOT</version>
    <classifier>fruit</classifier>
  </dependency>
  <dependency>
    <groupid>com</groupId>
    <artifactId>artichoke</artifactId>
    <version>3.0-SNAPSHOT</version>
    <classifier>vegetable</classifier>
  </dependency>
  <dependency>
    <groupid>com</groupId>
    <artifactId>asparagus</artifactId>
    <version>4.0-SNAPSHOT</version>
    <classifier>vegetable</classifier>
  </dependency>
</dependencies>

The fruit pom only needs to inject dependencies that have a classifier of
type fruit.  The goal is to do this programatically so user's do not have
to edit multiple pom files when adding a new dependency.

The real use case is more complicated than this but I hope the above
example gets at the heart of the question.

I understand I can use the maven model api to read and write poms but that
seems to require an intermediate step and perhaps a second instance of
maven to be spawned in order to consume the intermediate pom.  Ideally I
would like to avoid this step and inject dependencies programatically into
the pom which declares the plugin-in itself.  Is this even possible or is
the pom immutable?

Re: inject dependencies programatically in maven

Posted by Wayne Fay <wa...@gmail.com>.
> Is there a way to inject dependencies programatically in a maven plugin as
> if they were declared in the POM itself?

Not that I am aware of. And it sounds like a bad idea to me even if
you think you have a perfectly reasonable need for it.

Wayne

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org