0

Suppose I have a mutli-module maven project; sometimes I try to run something like mvn test and maven complains that it can't load some module from maven central. And of course, it's true. The code exists only on my laptop. So run "mvn install" so load the module in my local maven repo. But this is not nice. The module now exists in two places, and they might get out of sync. Is there a way to avoid this step and have maven look for the module in the right place?

EDIT: I was asked for a working example, so here's a small-ish one with three pom.xml files

First, we have parent/pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>this-is-our-group</groupId>
    <artifactId>this-is-the-parent</artifactId>
    <version>some-version</version>
    <packaging>pom</packaging>
    <modules>
        <module>../core</module>
        <module>../util</module>
    </modules>
</project>

Then we have core/pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>util-is-you-till</artifactId>
    <parent>
        <groupId>this-is-our-group</groupId>
        <artifactId>this-is-the-parent</artifactId>
        <version>some-version</version>
    </parent>
</project>

And finally util/pom.xml is basically the same as core except for the artifact ID:

<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>util-is-you-till</artifactId>
    <parent>
        <groupId>this-is-our-group</groupId>
        <artifactId>this-is-the-parent</artifactId>
        <version>some-version</version>
    </parent>
</project>

We put the above files in the obvious directory hierarchy, and then run mvn test -f parent/pom.xml. Maven will immediately start trying to fetch these things from central, even though they are meant to be all local.

Any fix?

Mark VY
  • 1,489
  • 16
  • 31
  • 1
    usually no `mvn install` should be necessary - how exactly did you reference your "other" module in the module that depends on it? (please add it to the question) – cyberbrain Aug 18 '22 at 20:45
  • Thanks!! I'll see if I can reproduce this in a small enough example. This might take a while. – Mark VY Aug 18 '22 at 20:55
  • 1
    I recently have solved that for my projects: https://stackoverflow.com/questions/73255826/executing-individual-maven-plugin-goals-in-multi-module-project – Andrey B. Panfilov Aug 18 '22 at 20:56
  • interesting! so there's no "out of the box" solution here? – Mark VY Aug 18 '22 at 21:35
  • @MarkVY that is bit unclear what do you mean under "ootb" in case of maven: it's design assumes activities are performed by plugins or extensions when maven just coordinates those activities by providing state machine and information store, from that perspective even activity `compiling java sources` is not out of the box feature. I do agree the behaviour you expect from maven is more obvious/natural than the current one, however I doubt they implement it in observable future. – Andrey B. Panfilov Aug 18 '22 at 22:07
  • 1
    If you run `mvn test` within a multi module build and that fails which can be solved by using `mvn install` your build has issues...wrong/missing dependencies between modules.. – khmarbaise Aug 19 '22 at 10:27
  • @cyberbrain I added an example – Mark VY Aug 20 '22 at 00:14
  • What I actually was looking for was the reference from _core_ to _util_ in the ``. You could add a `` element inside the `` in your modules so maven doesn't have to resolve the parent via a repo and can find it directly. But probably that is not your problem. – cyberbrain Aug 20 '22 at 14:20
  • Never heard of `relativePath`. I'm going to try that and get back to you. I admit having util depend on core would have been more realistic, but I wanted a small example. – Mark VY Aug 21 '22 at 00:14
  • Okay, so: adding relativePath fixed my example, and then I prepared to add to the "real" codebase, and it turns out it's already there. So something else is not quite right. – Mark VY Aug 23 '22 at 15:27
  • progress! I found a module in the codebase which listed "this-is-the-parent" as a plain dependency instead of as the parent; let me try to fix that – Mark VY May 12 '23 at 18:37

0 Answers0