多模块maven项目子模块不能引用其他模块的类

1.问题

子模块scnu-core中不能调用其他模块scnu-common的类。

本项目父模块为srb,scnu-common、scnu-base、scnu-core均为srb的子模块,且scnu-base引用scnu-common依赖、scnu-core引用scnu-base依赖。

2.解决方法

先查看父pom文件有没有定义子模块

父模块为:

    <groupId>com.scnu</groupId>
    <artifactId>srb</artifactId>
    <version>0.0.1-SNAPSHOT</version>

其子模块定义

    <modules>
        <module>scnu-common</module>
        <module>service-base</module>
        <module>service-core</module>
    </modules>

scnu-common、scnu-base、scnu-core每个模块都要有父模块的说明:

    <parent>
        <artifactId>srb</artifactId>
        <groupId>com.scnu</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

再查看子模块有没有引入对应的依赖

scnu-base:

        <dependency>
            <groupId>com.scnu</groupId>
            <artifactId>scnu-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

scnu-core:

        <dependency>
            <groupId>com.scnu</groupId>
            <artifactId>service-base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

这时,如果scnu-core仍然不能引入scnu-common的类,则右键子模块scnu-core,选择Maven,选择Reimport解决问题。

ps:本文方法不具备通用性

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
口: ``` interface PersonInterface { void display(); void work(); } class TeacherClass implements PersonInterface { private String id; private String name; private String gender; private String birthday; private String department; private String title; private在Maven项目中,如果多个模块需要相互引用,可以使用Maven模块依赖来解 double salary; public TeacherClass(String id, String name, String gender, String birthday, String department, String title, double salary决。具体步骤如下: 1.在需要引用其他模块模块的pom.xml文件中) { this.id = id; this.name = name; this.gender = gender; this.birthday = birthday; ,添加对被引用模块的依赖。 例如,假设模块A需要引用模块B中 this.department = department; this.title = title; this.salary = salary; } @Override public void display的,可以在模块A的pom.xml文件中添加如下依赖: ``` <dependency> <groupId() { System.out.println("编号:" + id); System.out.println("姓名:" + name); System.out.println("性>com.example</groupId> <artifactId>module-b</artifactId> <version>1.0.0</version> 别:" + gender); System.out.println("出生日期:" + birthday); System.out.println("所在部门:" + department); System.out.println("职称:" + title); System.out.println("工资:" + salary); } </dependency> ``` 2.在被引用的模块B的pom.xml文件中,添加对模块A @Override public void work() { System.out.println("教课中..."); } } class StudentClass implements PersonInterface { 的依赖。 例如,假设模块B也需要引用模块A中的,可以在模块 private String id; private String name; private String gender; private String birthday; private int entranceScore; privateB的pom.xml文件中添加如下依赖: ``` <dependency> <groupId>com.example</groupId> < String major; public StudentClass(String id, String name, String gender, String birthday, int entranceScore, String major) { artifactId>module-a</artifactId> <version>1.0.0</version> </dependency> ``` 3.在 this.id = id; this.name = name; this.gender = gender; this.birthday = birthday; this.entranceScore = entranceScore; this.major = major; } @Override public void display() { System命令行中执行`mvn install`命令,将依赖的模块安装到本地的Maven仓.out.println("编号:" + id); System.out.println("姓名:" + name); System.out.println("性别:" + gender库中。 这样,在模块A和模块B中都可以引用对方的了。需要注意的); System.out.println("出生日期:" + birthday); System.out.println("高考分数:" + entranceScore); 是,模块之间的依赖关系应该是一个有向无环图(DAG),避免出现循环依赖的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值