Chi-Lan
Chi-Lan

Reputation: 3805

How can a maven project depend on another local maven project?

I'm having two maven project project/foo/pom.xml and project/bar/pom.xml. I have foo depend on bar, and I want that every timefoo/pom.xmlcompiles, it'll automatically compilebar`.

How can I do that with maven?

Update: I can tuck them both into a parent project, but then, what will I do if I want to run mvn jetty:run on a child project?

Upvotes: 4

Views: 4128

Answers (4)

Daniel
Daniel

Reputation: 10245

You can combine them into one project, with two modules.

An example project structure:

parent (pom) 
    |- foo (jar)
    |- bar (jar)

In the parent pom:

    <groupId>org.me</groupId>
    <artifactId>parent-project</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>foo</module>
        <module>bar</module>
    </modules>

In each child pom:

    <artifactId>foo</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.me</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

To build the project (both modules) with Maven, execute this from the parent directory:

$ mvn install

Upvotes: 1

Christian Schlichtherle
Christian Schlichtherle

Reputation: 3155

First, setup a multi-module build as Daniel has shown. Then change to the directory with the parent POM. Then, for example to install foo with automatically installing bar before, too, type:

mvn --also-make --projects foo install

This will check the dependencies within the reactor and then resolve to run the install life cycle on bar before it runs the install life cycle on foo.

This works with any life cycle or goal. Mind you however that if you specify jetty:run, then this goal would be run in both projects, which is probably not quite what you want.

You can find more information about this feature here: http://maven.apache.org/guides/mini/guide-multiple-modules.html

Upvotes: 0

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 78011

Option 1

Setup both builds in Jenkins, which can detect dependencies between projects

Automatic build chaining from module dependencies

Jenkins reads dependencies of your project from your POM, and if they are also built on Jenkins, triggers are set up in such a way that a new build in one of those dependencies will automatically start a new build of your project.

Option 2

If the two Maven projects are closely related (released together, sharing the same revision number) then perhaps they're really two modules of the same project?

If that is the case read the following document for guidelines on how to create a parent POM:

Upvotes: 1

Edward Samson
Edward Samson

Reputation: 2425

This is not possible using maven alone. The closest you can get is to make both as modules of an aggregator POM, so you can build both with a single command. Note that a mvn install will already consider if there have been changes since the last build so you're saving a few CPU cycles there.

Upvotes: 0

Related Questions