8.IDEA构建Maven多模块工程项目

需求:以前面创建的demo和demo1为例,新建一个mavenProject工程,将demo和demo1作为mavenProject的两个模块(module),然后在demo1的方法中使用demo类中的方法。

1.将demo和demo1放置到mavenProject的根目录下,并在mavenProject的pom文件中添加模块
在这里插入图片描述

<!--聚合  -->
<modules>
  <module>/demo</module>
  <module>/demo1</module>
</modules>

2.在demo1的pom文件中添加对demo模块的依赖

<dependencies>
    <!--使用Junit4(注解方式运行)-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.steven.maven</groupId><!--项目的包名-->
      <artifactId>demo</artifactId><!--项目的模块名(项目名)-->
      <version>1.0-SNAPSHOT</version><!--快照版本-->
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.6</version>
    </dependency>
</dependencies>

3.在demo1的方法中使用demo类中的方法

//demo的HelloWorld中定义的hello方法
public class HelloWorld {
    public String hello(){
        return "hello world!";
    }
}
public class Speak {
    public String hi(){
        return new HelloWorld().hello();
    }
}

4.测试

public class SpeakTest {
    @Test
    public void testHi(){
        Assert.assertEquals("hello world!",new Speak().hi());
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值