简体   繁体   中英

how do I add a Maven dependency on another existing project in Eclipse

I'm trying to follow a udemy tutorial on Java Microservices and I'm getting stuck in a Maven issue. I have 3 projects (accounts, cards and loans) in Eclipse as follows:

在此处输入图像描述

Cards and loans depends on accounts. I have added these dependencies in Eclipse by configuring the Build Path and adding the accounts project to the Classpath of loans and cards. The code inside the loans controller refers to a Customer object which is defined in accounts:

package com.eaztbytes.loans.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.eaztbytes.accounts.model.Customer;
import com.eaztbytes.loans.model.Loans;
import com.eaztbytes.loans.repository.LoansRepository;

@RestController
public class LoansController {

    @Autowired
    private LoansRepository loansRepository;

    @PostMapping("/myLoans")
    public List<Loans> getLoansDetails(@RequestBody Customer customer) {
        List<Loans> loans = loansRepository.findByCustomerIdOrderByStartDtDesc(customer.getCustomerId());
        if (loans != null)
            return loans;
        else
            return null;
    }
}

Building and running accounts, loans and cards (as Java applications) from Eclipse does work exactly as it should. When running mvn clean and install for accounts from the command line, things do work without errors. But when running mvn install for loans, I get the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project loans: Compilation failure: Compilation failure: [ERROR] /Users/j/dev/udemy-ms-1/loans/src/main/java/com/eaztbytes/loans/controller/LoansController.java:[10,36] package com.eaztbytes.accounts.model does not exist [ERROR] /Users/j/dev/udemy-ms-1/loans/src/main/java/com/eaztbytes/loans/controller/LoansController.java:[21,57] cannot find symbol [ERROR] symbol: class Customer [ERROR] location: class com.eaztbytes.loans.controller.LoansController [ERROR] -> [Help 1]

The tutorial didn't mention anything about Maven dependencies, and their loan pom.xml does NOT have a dependency on accounts (see their github code here ). I did notice other small inconsistencies between the tutorial steps and my personal experience of going through it, but I thought that must be due to difference in OS, Java versions, Maven versions, etc. So, I right-clicked on Maven -> Add Dependency, and added accounts. pom.xml in loans was updated to include a reference to accounts:

<dependency>
    <groupId>com.eaztbytes</groupId>
    <artifactId>accounts</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Unfortunately this didn't help with the error above. I made sure that the accounts jar exists in the.m2 folder, and I made sure that I built accounts with a clean and install from the command line before I attempted a clean and install for loans. I understand that Eclipse does some magic wrt dependencies, but I don't understand what I'm missing. Why can't maven see the accounts jar?!

You can add a dependency just like any other dependency. However, that dependency only works inside Eclipse. If you want to run Maven on the command line, the reference to the dependency cannot be found. To overcome that, you can use mvn install to install the dependency in your local repository. That makes it available for Maven builds on the same machine. It will not be deployed anywhere remotely (Maven Central, Nexus, whatever); that's what mvn deploy is for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM