1

I have several Maven projects which share a common parent as following:

project-parent
    project-1
    project-2: includes project-1
    project-3: includes project-2

Here, project-3 has project-2 in its dependencies and project-2 has project-1 in its dependencies, as an example.

When I push a project to git, Jenkins automatically rebuilds to dependent projects. For example, when I push project-3, project-2 will be rebuilt. Because of that, project-1 will be rebuilt as well. This happens because Jenkins has knowledge about the dependency graph.

However, when I build project-3 locally on my development machine, I have to remember to rebuild project-2 (and project-1) as well.

I could add a <modules>...</modules> section to the parent project and simply rebuild the parent project. This will rebuild the whole project, all child projects included. However, I would rather not do that, as there are a lot of projects and some of them require a lot of time to build.

So I want to build one project (the one I'm working on) and somehow automatically (or conveniently) rebuild all dependent projects, similar to the Jenkins behavior. Basically, I want to achieve the Jenkins behavior locally on my development machine. Is this possible?

Jekker
  • 13
  • 3
  • Build the parent project all the children project will be built automatically. – Sambit Jul 26 '19 at 18:09
  • @Sambit As said, I want to avoid that, as this will build all children, which takes a lot of time. I could select specific projects with -pl, but this kinda voids my question, as I have to remember to do that (and I have to know the dependency graph as well). – Jekker Jul 26 '19 at 18:18
  • In case of Jenkins or any CI CD approach, you should build the whole project or you have to separate out the small module as independent project. Besides in this you are only left with -pl option in maven reactor. – Sambit Jul 26 '19 at 18:20
  • Note that IDEs like Eclipse allow you to do "workspace resolution", which means that dependencies are automatically replaced by workspace elements. This is not a rebuild, but helps alot. – J Fabian Meier Jul 26 '19 at 19:44

2 Answers2

1

You can also create an aggregator completely independent of the rest: each <module> point to a path of a folder containing a pom.xml (it may also directly target a pom.xml).

<project> 
  ...
  <modules>
    <module>project-parent</module>
    <module>project-1</module>
    <module>project-2</module>
    <module>project-3</module>
  </modules>
  ...
</project>

In this scenario, building from that aggregator will allow maven to take full knowledge of your dependency graph and build in order. You will only have to take care to update properly the version to ensure it match (if project-2 depends of version 2 of project-1 and project-1 is in version 3, then this won't work).

You need not put module you don't want, however be aware that if a module have children <module>, it will also build them.

NoDataFound
  • 11,381
  • 33
  • 59
  • Thank you, but this is not really my question. My goal is not to automatically rebuild the whole project, as you explained. My goal is to build one project and all dependent projects (recursively). – Jekker Jul 26 '19 at 18:21
  • I know. But that's a solution to keep in mind if you don't want to modify the parent pom (even if locally and not meant to be published). You can also use -am or -amd, but that would really work if the project in the execution directory reference the other one way or another. – NoDataFound Jul 26 '19 at 20:52
  • That's actually a great idea. I did not completely understood I could create a *new* project which just references the modules. This doesn't impact the existing projects. – Jekker Jul 27 '19 at 05:23
  • I don't have enough reputation to upvote unfortunately. In any case, thanks for your idea, I will combine the two answers: separate module-only project and make use of `-am`/`-amd`. – Jekker Jul 27 '19 at 05:25
1

You can execute the following command from your parent project (assuming you have the my-child-module in the <module>...</module>:

mvn clean install -pl my-child-module -am

This command will build the project my-child-module and its dependencies.

-pl, --projects
Build specified reactor projects instead of all projects

-am, --also-make
If project list is specified, also build projects required by the list

-amd, --also-make-dependents If project list is specified, also build projects that depend on projects on the list

Source

Arnaud Claudel
  • 3,000
  • 19
  • 25