温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Maven 主模块和子模块pom.xml

发布时间:2020-10-28 15:37:14 来源:亿速云 阅读:517 作者:Leah 栏目:开发技术

Maven 主模块和子模块pom.xml?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

背景知识

dependencies与dependencyManagement的区别

  • 父项目中的 <dependencies></dependencies> 中定义的所有依赖,在子项目中都会直接继承。
  • 在父项目中的 <dependencyManagement></dependencyManagement> 中定义的所有依赖,子项目并不会继承,我们还要在子项目中引入我们需要的依赖,才能进行使用。此时我们在子项目中不用设置版本。

实验

为了回答这个问题:“如果依赖全部放入父模块,部分子模块没有用到这些依赖,是否会增加这些子模块打包后的代码体积?”。我们拿一个 maven 多模块项目打包测试一下。

实验材料:

Maven 主模块和子模块pom.xml

如图,一个多模块项目。

其中 wx-common 模块只是放了一些 enums:

Maven 主模块和子模块pom.xml

父模块依赖:

<properties>
  <java.version>11</java.version>
  <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
  <wx-common-version>0.0.1-SNAPSHOT</wx-common-version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.2.6.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.5.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.2.6.RELEASE</version>
    <scope>test</scope>
    <exclusions>
      <exclusion>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
      </exclusion>
    </exclusions>
  </dependency>

  <!--lombok-->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.json/json -->
  <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20190722</version>
  </dependency>

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

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.2.RELEASE</version>
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>com.jellyfishmix.interchange</groupId>
      <artifactId>wx-common</artifactId>
      <version>${wx-common-version}</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

wx-common 模块无单独引入的依赖。

wx-common 模块单独打包后的大小(3982 bytes):

Maven 主模块和子模块pom.xml

接下来我们把父模块的依赖都放入 <dependencyManagement></dependencyManagement> 中,这样子模块就不会全部继承这些依赖,而是需要在子模块的 pom.xml 中也进行声明,子模块才能继承对应的依赖。

按照博主的猜想, 子模块最初继承了很多父模块的依赖,当单独打包子模块时,这些依赖被打入了子模块jar包中。而这些继承过来的父模块的依赖中,有很多是子模块不需要的,因此子模块单独打出的包,会有不少冗余体积

我们把父模块的依赖都挪入 <dependencyManagement></dependencyManagement> 中,而 子模块又没有在自己的 pom.xml 中声明这些依赖,也就不会继承这些依赖,这样子模块单独打出的包,会不会减少很多体积呢 ?

按我们的推测,把父模块的依赖都放入 <dependencyManagement></dependencyManagement> 中,然后对子模块单独打包(3982 bytes):

Maven 主模块和子模块pom.xml

可以看到打包出来的 jar,并没有按照我们预先设想的,体积减少了很多,而是和之前的体积一模一样(都是3982 bytes)。

看到这个结果,博主百思不得其解。难道 子模块继承的父模块的依赖,如果在子模块中没有被使用,在子模块单独打包时,就不会被打入 jar 吗?

我们再做一个实验来验证猜想,现在父模块的依赖还是在 <dependencyManagement></dependencyManagement> 中,需要在子模块的 pom.xml 中也进行声明,子模块才能继承对应的依赖。我们给子模块的 pom.xml 多声明几个依赖:

<!--lombok-->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.12</version>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  <version>2.2.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20190722</version>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
  <version>2.2.2.RELEASE</version>
</dependency>

然后对子模块单独打包(4175 bytes):

Maven 主模块和子模块pom.xml

可以看到我们的 jar 包体积确实增加了(4175 - 3982 = 193 bytes),但这些增加的代码体积,应该是我们的 pom.xml 中新增的一对对 <dependency></dependency> 的体积,而不是真正引入的依赖的代码。

因此,博主确信了推测: 子模块继承的父模块的依赖/子模块声明的依赖,如果在子模块中没有被使用,在子模块单独打包时,就不会被打入 jar 。

进一步实验来确认推测,我们在子模块中使用一下声明的依赖。只在子模块中加两个注解: @FeignClient(name = "interchange-wx") ,对子模块单独打包(4259 bytes):

Maven 主模块和子模块pom.xml

打包结果(4259 - 4175 = 84 bytes)。

因此,maven 打包加入的依赖代码应该是被调用到的部分代码,没有被调用到的依赖代码不会被加入打包后的 jar 包中。

实验结论

  •  子模块继承的父模块的依赖/子模块声明的依赖,如果在子模块中没有被使用,在子模块单独打包时,就不会被打入 jar 。
  • maven 打包加入的依赖代码是被调用到的部分代码,没有被调用到的依赖代码不会被加入打包后的 jar 包中。

推荐做法

对于 “依赖放入子模块还是父模块” 这个问题,推荐将依赖放入父模块的 <dependencyManagement></dependencyManagement> 中,然后子模块有需要的依赖,在子模块的 pom.xml 中声明。这样便于在父模块中统一管理依赖版本,避免子模块依赖版本不一致造成的混乱或冲突。

关于Maven 主模块和子模块pom.xml问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI