4

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?

Chi-Lan
  • 3,575
  • 3
  • 22
  • 24

4 Answers4

1

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:

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
1

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
Daniel
  • 10,115
  • 3
  • 44
  • 62
  • But then what if I want to run `mvn jetty:run` on module `bar`? – Chi-Lan Jul 27 '12 at 11:25
  • The same project structure will work if `bar` is a war project (and `foo` is a jar, and `bar` depends on `foo`). In that case, just run `mvn jetty:run` from the `bar` directory, like you normally would. – Daniel Jul 27 '12 at 11:59
0

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.

Edward Samson
  • 2,395
  • 2
  • 26
  • 39
0

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

Christian Schlichtherle
  • 3,125
  • 1
  • 23
  • 47
  • Wouldn't work at all, since `jetty` plugin is not available at all module. Isn't there another solution? – Chi-Lan Jul 27 '12 at 11:48