Why does maven recognize dependencies only on installed POM files?

I have a project with Maven in which one subproject (A) wants to depend on another subproject (B) that uses the "pom" package.

If I do it straightforwardly, where A sets the dependency on B with <type>pom</type> , everything works fine if I do "mvn install", but if I ran any phase earlier than install, for example mvn compile or mvn package , then it does not work, trying to build A: it looks for B pom in the repository and does not find it.

I do not want this pom in the repository, because it is part of our active source code and often changes.

For all projects created by jar-packaged, we work perfectly to prevent them from being repository, build using the mvn package , and Maven knows how to find all the dependencies in the source and build the trees that it manages without resorting to the repository; however, for a pom-packaged project, he always wants to go to the repository.

A few things I learned while trying to figure this out:

  • Maven's best methods encourage you to use pom-packaged projects to group dependencies , but with the added step "mvn install" to the POM project
  • Maven's life-cycle documentation says that "a project that is purely metadata (the meaning of pom packaging) only associates goals with the installation and deployment phases"; perhaps that’s why the POM project is invisible as a dependency target if I did not invoke the installation phase? I tried to bind the compiler plugin to the compilation phase, and this did not seem to help.

Is it possible to specify a POM subproject as a dependency of another subproject in the same parent project without installing the POM project in the repository?

+6
source share
2 answers

It is not just a question of what goals are associated with the phases of the POM project life cycle. If this were so, then binding the target of the “package” would solve the problem.

When creating a multi-module project, Maven reads the POM of all modules to determine the dependencies between the modules so that it can create modules depending on them before the dependent modules. It can achieve this even when the “package” target is run (so that module-specific modules are not yet in the local repository).

Therefore, the code that creates the classpath for the assembly should handle several cases, in particular:

  • dependencies due to an additional project, where he looks for POM in the local repository, processes its dependencies and adds the POM jar to the class path
  • project-dependent pom, where it searches for POM in the local repository and processes its dependencies
  • dependency between jar projects, where it searches for POM in the project tree, processes its dependencies and adds this module target / classes folder to the class path
  • dependency between pom projects, where for some reason it does not look for POM in the project tree and therefore does not handle dependencies.

Note the asymmetry in the last two cases compared to the first two.

I see two solutions to your problem. One of them is to report an error, or rather, a request for a change in behavior (since it is clearly intentional), perhaps only for cases between projects depending on multi-module projects. Or really offer a patch. But since the behavior is intentional, you may encounter failure. At best, you have a long wait. (I voted for your bug report, though - I was amazed at the same behavior in a different context.)

Another solution is to simply start the installation in your project. I really don’t understand why you don’t want the POM project in your repository: if necessary, you can use the snapshot repository where it doesn’t matter if things change often to avoid pollution of your main repository.

+4
source

Setting maven-install-plugin to run at compile time and copying the corresponding pom.xml to the repository seems to do what I wanted as far as Maven is concerned, although m2eclipse still doesn’t like it (it throws “failed to read artifact descriptor errors "without further description for pom.xml, which has a dependency on another POM project).

0
source

Source: https://habr.com/ru/post/887248/


All Articles