Search code examples
mavenbuildmaven-3

Building specific modules in multimodular maven project + dependencies conditionally


When running mvn install -pl "project1, project2, etc." -am -Pmy-profile, maven will build the specified modules and all their dependencies which can take a while if there are a lot of dependencies.

That being said, some dependencies being compiled (due to the -am flag) may already exist in the local .m2 repository and/or in my-profile, rendering their recompilation useless and often time consuming.

Is there any way to specify that maven should only build a given -am dependency if it does not already exist in my-profile nor locally in .m2?


Regarding the potential duplicate Maven 2: Only build artifact if not already installed in local repository:

In my case I do want to build the projects referenced after the -pl flag regardless of whether they were already built or not. It's the dependencies that are my problem. Also, that question is regarding Maven 2, perhaps things have changed since.


Solution

  • Short answer: Maven as shipped cannot do this. Once you use -am to specify that dependencies should be included in the reactor (or include/exclude them through profiles) there's nothing special about the original -pl list, so this is essentially a duplicate of Maven 2: Only build artifact if not already installed in local repository, which has a similar answer to this one:

    The short answer is: there's no sane way to do that, Maven doesn't work that way.

    You may get better results from use of incremental builds, or from building your own scripting around Maven. You may also be able to make use of profiles to focus on a subset of the project, but there's no existing command-line flag or configuration option to get what you're asking for.