Maven is unable to find a package from the local repository

74 Views Asked by At

I am encountering a weird issue.

I have two spring-boot projects built using maven. They are named customers and rest-client. They are based on spring-boot 3.2.2 and use Maven as the build tool.

Maven version on my machine:

mvn -v
Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584)

Java version:

java 21 2023-09-19
Java(TM) SE Runtime Environment Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15, mixed mode, sharing)

The rest-client project is dependent on the customers project as it uses a DTO (Java record).

I have added the dependency in the rest-client pom.xml as below

        <dependency>
            <groupId>com.adityasamant.learnings</groupId>
            <artifactId>customers</artifactId>
            <version>2.0.0-SNAPSHOT</version>
        </dependency>

The jar is present in the local Maven repository as seen below:

/Users/adityasamant/.m2/repository/com/adityasamant/learnings/customers/2.0.0-SNAPSHOT
adityasamant@Adityas-MacBook-Pro 2.0.0-SNAPSHOT % ls
_remote.repositories            customers-2.0.0-SNAPSHOT.jar    customers-2.0.0-SNAPSHOT.pom    maven-metadata-local.xml

I have pointed to the local maven repository using the below field in my settings.xml <localRepository>/Users/adityasamant/.m2/repository</localRepository>

The dependencies seem to get resolved in IntelliJ IDEA.

But when I build the rest-client project using mvn clean install, maven is not able to resolve packages from the customers module and throws exceptions in compilation.

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project rest-client: Compilation failure: Compilation failure: 
[ERROR] /Users/adityasamant/Documents/work/code/spring/rest-client/src/main/java/com/adityasamant/learnings/restclient/controller/RestClientController.java:[3,50] package com.adityasamant.learnings.customers.model does not exist

Any help would be greatly appreciated.

I have tried the following:

  1. I have pointed to the local maven repository using the below field in my settings.xml <localRepository>/Users/adityasamant/.m2/repository</localRepository>

  2. I have also tried to rename the _remote.repositories file under /Users/adityasamant/.m2/repository/com/adityasamant/learnings/customers/2.0.0-SNAPSHOT

This did not help.

As the dependencies get resolved in IntelliJ IDEA, I was expecting that the Maven build would pass seamlessly as well.

2

There are 2 best solutions below

3
pochopsp On

You could try something like this: in your pom.xml add this repository as child of your repositories tag:

<repository>
    <id>in-project</id>
    <name>In Project Repo</name>
    <url>file://${project.basedir}/maven_repo</url>
</repository>

In the directory where you have your pom file (that is the project.basedir for maven), create a dir named maven_repo and inside it place your directories so that you will form the path

maven_repo/com/adityasamant/learnings/customers/2.0.0-SNAPSHOT

(and in the last folder of your path you will place your jar of course)

1
Aditya Samant On

The solution is present here:- https://www.baeldung.com/spring-boot-dependency