Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven cannot resolve dependency for module in same multi-module project

When running commands such as

mvn dependency:build-classpath 

or

mvn exec:java 

Maven is unable to resolve a dependency of one of my modules on another.

[ERROR] Failed to execute goal on project parser-app: Could not resolve dependencies for project project_group:A:jar:0.1-SNAPSHOT: Could not find artifact project_group:B:jar:0.1-SNAPSHOT

The project structure is as follows:

/pom.xml /A/pom.xml /B/pom.xml 

The parent pom is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>project_group</groupId>   <artifactId>parent</artifactId>   <packaging>pom</packaging>   <version>0.1-SNAPSHOT</version>   <name>parent</name>    <modules>     <module>A</module>     <module>B</module>   </modules> 

The first child module (the one failing to resolve the dependency):

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <parent>     <groupId>parent_group</groupId>     <artifactId>parent</artifactId>     <version>0.1-SNAPSHOT</version>   </parent>   <artifactId>A</artifactId>   <packaging>jar</packaging>   <name>A</name>    <dependencies>     <dependency>       <groupId>parent_group</groupId>       <artifactId>B</artifactId>       <version>0.1-SNAPSHOT</version>     </dependency> 

The second child module (the dependency):

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <parent>     <groupId>parent_group</groupId>     <artifactId>parent</artifactId>     <version>0.1-SNAPSHOT</version>   </parent>   <artifactId>B</artifactId>   <packaging>jar</packaging>   <name>B</name> 
like image 959
Benjamin George Roberts Avatar asked Apr 18 '15 04:04

Benjamin George Roberts


People also ask

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.

How do I run a multi-module project in Maven?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

Why Maven dependencies are not getting downloaded in IntelliJ?

If the dependencies weren't imported correctly (IntelliJ IDEA highlights them), try to perform the following actions: You can check your local maven repository in the Maven | Repositories settings and try to update it. You can check the jar file of the local . m2 repository to see if it was downloaded correctly.


2 Answers

Have you run mvn clean install at least once on the project to install the dependencies within your local repository?

like image 145
Andrew McKee Avatar answered Oct 14 '22 12:10

Andrew McKee


The Maven reactor is weird that way, it keeps modules around only for certain tasks. When running a build target that only does something for one subproject, then even if Maven builds dependencies first, it does not keep them around in the reactor (sometimes).

Installing to the local repository is a workaround, but it is horrible and should be avoided when possible, because you can easily end up with outdated build results.

A slightly less ugly workaround is to combine two build targets, where the second build target does something harmless, but triggers addition to reactor in all subprojects.

As an example you can combine the task you want with the 'compile' or 'package' tasks.

Also see highest voted answer at Maven doesn't recognize sibling modules when running mvn dependency:tree

like image 43
tkruse Avatar answered Oct 14 '22 12:10

tkruse