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

108,742

Solution 1

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

Solution 2

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

Solution 3

This error might also be caused by Maven being in offline mode.

Sometimes I seem to accidentally enable offline mode in IntelliJ IDEA. To disable it, toggle the Toggle Offline Mode toggle in the Maven Toolbar

enter image description here

or uncheck the Work Offline checkbox in the settings under Build, Execution, Deployment > Build Tools > Maven.

enter image description here

Share:
108,742
Benjamin George Roberts
Author by

Benjamin George Roberts

Software Engineering Graduate at the Australian National University (Computer Engineering Major). Cyber Security practitioner.

Updated on July 05, 2022

Comments

  • Benjamin George Roberts
    Benjamin George Roberts almost 2 years

    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>
    
    • Saurabh Jhunjhunwala
      Saurabh Jhunjhunwala about 9 years
      try interchanging the build structure, I mean rewrite your pom.xml to have <module>B</module> <module>A</module>. classes in module A needs classes from module B, then B has to be built first
    • Benjamin George Roberts
      Benjamin George Roberts about 9 years
      no change, as far as I knew the reactor should work out the order from the dependencies
    • khmarbaise
      khmarbaise about 9 years
      Correct the reactor should handle the order of building. No manuall handling needed nor should it be done.
    • sschuberth
      sschuberth about 7 years
  • Benjamin George Roberts
    Benjamin George Roberts about 9 years
    That worked. This means I'd need to run install every time I update the dependant module right? I assumed there was a way for it to use most recently built copy (ie root/B/target/B-0.1-SNAPSHOT.jar)
  • Andrew McKee
    Andrew McKee about 9 years
    I think what you are looking for then is: stackoverflow.com/questions/4367665/… Most of the methods to do this are less well documented however as maven relies on the dependency model whereby your dependencies are installed within the local repository
  • Benjamin George Roberts
    Benjamin George Roberts about 9 years
    I can mvn package and have the packages build against each other, however I assumed other actions (like generating the classpath or executing) would also use the most recently built packages, not whichever I installed to my local repository last. I'm guessing this might just be a compromise I have to live with?
  • gfan
    gfan almost 8 years
    I found the parent project also need to install to local repo.
  • Chad
    Chad over 6 years
    To me, it's ridiculous that maven would need to look in the local repository for submodules! Seriously? What a hack.
  • Albert Zhong
    Albert Zhong over 6 years
    Sad to see if I have to install it to the local repo for depedency:tree! My module is still under development, installing them to local repo might cause some issue.
  • Krishna Kumar Singh
    Krishna Kumar Singh over 4 years
    Run this command in Parent & in all child projects. It works for me. Thanks
  • Ali Bigdeli
    Ali Bigdeli over 2 years
    for me this method works. The command mvn clean install must executed at least one time. After that everything works fine.