Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does maven recognize dependencies on only installed POM files?

Tags:

maven

I've got a project with Maven in which one subproject (A) wants to depend on another subproject (B) which uses "pom" packaging.

If I do this the straightforward way, where A specifies a dependency on B with <type>pom</type>, things work perfectly if I do "mvn install", but if I run any phase earlier than install, such as mvn compile or mvn package, then it fails while trying to build A: it goes looking for B's pom in the repository, and doesn't find it.

I don't really want this pom in the repository, because it's part of our active source code and changes frequently.

For all the jar-packaged projects we build, it seems to work fine to keep them out of the repository, build with mvn package, and Maven knows how to find all the dependencies in the source and build trees it manages without resorting to the repository; however for the pom-packaged project it always wants to go to the repository.

A couple things I learned while trying to understand this:

  • Maven best practices encourage you to use pom-packaged projects to group dependencies, but with the added step of "mvn install" on the POM project
  • Maven lifecycle documentation says "a project that is purely metadata (packaging value is pom) only binds goals to the install and deploy phases"; maybe this is why the POM project is invisible as a dependency target unless I invoke the install phase? I tried binding the compiler plugin to the compile phase and this didn't seem to help.

Is there a way that I can specify the POM subproject as a dependency of another subproject in the same parent project, without installing the POM project to the repository?

like image 808
metamatt Avatar asked May 04 '11 00:05

metamatt


People also ask

Why are Maven dependencies not being downloaded?

If you run Maven and it fails to download your required dependencies it's likely to be caused by your local firewall & HTTP proxy configurations. See the Maven documentation for details of how to configure the HTTP proxy.

Which file is used to define dependency in Maven?

Which file is used to define dependency in maven? Explanation: pom. xml is used to define dependency which is used to package the jar. POM stands for project object model.

Where does Maven install dependencies?

The Local Repository When we execute a Maven build, Maven automatically downloads all the dependency jars into the local repository. Usually, this directory is named .

What is a POM file in a Maven project?

What is a POM? A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.


1 Answers

It isn't purely a question of which goals are bound to which lifecycle phases for POM projects. If it were, then binding the "package" goal would solve the problem.

When building a multi-module project, Maven reads the POMs of all modules to determine dependencies between modules, so that it can build the depended-upon modules before the depending modules. It's able to achieve this even when running the "package" goal (such that the depended-upon modules are not yet in the local repository).

Therefore, the code that constructs the classpath for builds must be managing several cases, notably:

  • extra-project jar dependency, where it looks for the POM in the local repository, handles its dependencies, and adds the POM's jar to the classpath
  • extra-project pom dependency, where it looks for the POM in the local repository and handles its dependencies
  • intra-project jar dependency, where it looks for the POM within the project tree, handles its dependencies, and adds that module's target/classes folder to the classpath
  • intra-project pom dependency, where for some reason it doesn't look for the POM within the project tree, and therefore doesn't handle it's dependencies.

Notice the asymmetry in the last two cases, as compared to the first two.

I can see two solutions to your problem. One is to file a bug report, or rather a request to change the behaviour (since it's obviously intentional), perhaps only for the case of intra-project dependencies on multi-module projects. Or indeed propose a patch. But since the behaviour is intentional, you might meet a refusal. In the best of cases, you're in for a long wait. (I'd vote for your bug report though - I've been stung by that same behaviour, in a different context.)

The other solution is simply to run an install on your project. I don't really understand why you don't want the POM project in your repository: if needs be, you can use a snapshot repository, where it doesn't matter if things change frequently, to avoid polluting your main repository.

like image 99
Andrew Spencer Avatar answered Sep 26 '22 06:09

Andrew Spencer