0

I have the following project package structure.

parent
|- module1
|- module2    
|- module3

I am able to include module3 as a dependency in both pom.xml files of module1 & module2. This gives me access to all of the dependencies & code from module3.

<dependency>
    <groupId>my.group</groupId>
    <artifactId>module3</artifactId>
    <version>1.0.0</version>
</dependency>

However, when I run maven commands such as dependency:tree on the pom file of module1 or module2, maven will try to search on certain <servers> that are listed in my settings.xml file in order to try & download module3 as an artifact. module3 is not deployed to any <server> at the moment so, the maven command fails with following error

[ERROR] Failed to execute goal on project module1: Could not resolve dependencies for project my.group:module1:jar:1.0.0-SNAPSHOT: Failed to collect dependencies at my.group:module3:jar:1.0.0-SNAPSHOT: Failed to read artifact descriptor for my.group:module3:jar:1.0.0-SNAPSHOT: Could not transfer artifact my.group:module3:jar:1.0.0-SNAPSHOT from/to MyServer (https://example): Access Denied to: .......etc.

Ideally, module3 would not be deployed at all as an artifact but, instead it might simply have <packaging>pom</packaging>. module3 is just some module that should only be used by module1 & module2. Also, FYI the dependency tree of module3 builds perfectly fine.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
duppydodah
  • 165
  • 1
  • 3
  • 17
  • Shouldn't you be having `module3` as the first module of `parent`? – ernest_k Jun 04 '21 at 05:10
  • @ernest_k The ordering is simply the package structure based on naming conventions. Should I rename module3 so that it goes before ? – duppydodah Jun 04 '21 at 05:41
  • 1
    The point I was trying to make is that `module3` should be built before the others. That's because the other two depend on it. However they're named, the module on which others depend should be declared before in the parent pom. – ernest_k Jun 04 '21 at 05:47
  • module3 is being built before others but, it still always try's to search remotely when there already is a local version available – duppydodah Jun 04 '21 at 16:28

3 Answers3

1
  1. Dependencies should never be of packaging pom. The need to be proper JARs and also need to be deployed to the repository.

  2. Usually, you run build commands on the parent of the multi module project. If you want to restrict the build to one module, use -pl.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

As far as I understand the question, the following maven module are part of the same reactor:

parent
|- module1
|- module2    
|- module3

parent should have:

<modules>
  <module>module1</module>
  <module>module2</module>
  <module>module3</module>
</modules>

Otherwise, Maven will not look in the file system but in the local repository.

Also, to my understanding, in your example module1 and module2 are two jar packaged module (eg: <packaging>jar</packaging> or omitted) but parent and module3 are two pom packaged module (eg: <packaging>pom</packaging>).

I suppose you want to create some kind of module importing only dependencies without code, in which case you should import it using:

<dependency>
    <groupId>my.group</groupId>
    <artifactId>module3</artifactId>
    <version>1.0.0</version>
    <type>pom</type>
</dependency>

Strange as it may be, this should work.

You can also use --offline to force maven to not go online.

NoDataFound
  • 11,381
  • 33
  • 59
  • This is actually something I haven't covered in my answer, but it is a valid use case. – J Fabian Meier Jun 04 '21 at 14:02
  • What do you mean some meta module ? Including pom didn't resolve the issue of it trying to pull from sources, in fact it creates issue in module1 & module2 for recognizing module3. Is there a way to have the equivalent of -o or -offline but, written in the pom itself ? – duppydodah Jun 04 '21 at 15:01
  • By that I meant a module that brings dependency but not being itself a jar. – NoDataFound Jun 04 '21 at 15:07
  • I'm pretty sure it should work with type=pom. However, you have two use case 1) mvn clean install 2) mvn dependency:tree. The dependency:tree might not use artifact from the reactor, and if you don't do a mvn install first, this will download it. – NoDataFound Jun 04 '21 at 15:12
  • If I use type=pom then, I lose access to all of the classes that dependency has. I am guessing that it just only brings in the dependencies from module3 and none of the code. – duppydodah Jun 04 '21 at 16:08
  • Then let me ask: what is the packaging of each module? jar? If so, the type=pom is unrelated and the fact dependency:tree tries to download is completely unrelated to that. Please provide the complete error message. – NoDataFound Jun 04 '21 at 18:15
  • 1
    If something has classes, it cannot be of packaging `pom`. – J Fabian Meier Jun 05 '21 at 06:21
-1
  • Make sure the version of the module is correct.

Normally, when you run "mvn clean install" in parent project, all modules will be built, Order builds module3 -> module1 and module2. module3 is always built first.

Thanks

thanhncs
  • 1
  • 1
  • 1
    Im not running a maven command against the parent pom though. It is simply against, module1 or module2. Would renaming module3 -> module0 resolve ? if it puts that module ahead of the others in the package structure ? – duppydodah Jun 04 '21 at 05:44
  • No, it can not resolve by change name of module. - Make sure something: + include and order all modules in the parent pom. + just use 1 version, which is the same as the version of parent -> You don't need to declare the version in depencies. + check the dependencies in project to make sure no conflict. Correct everything, if the bug still happens, please desc more detail or add the setting.xml files of your project. Not enough information for me can answer detail, what's exactly the bug. Thanks – thanhncs Jun 04 '21 at 07:10