测试环境 maven 3.3.9

想必大家在做SpringBoot应用的时候,都会有如下代码:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.3.3.RELEASE</version>
  5. </parent>

继承一个父模块,然后再引入相应的依赖

假如说,我不想继承,或者我想继承多个,怎么做?

我们知道Maven的继承和Java的继承一样,是无法实现多重继承的,如果10个、20个甚至更多模块继承自同一个模块,那么按照我们之前的做法,这个父模块的dependencyManagement会包含大量的依赖。如果你想把这些依赖分类以更清晰的管理,那就不可能了,import scope依赖能解决这个问题。你可以把dependencyManagement放到单独的专门用来管理依赖的pom中,然后在需要使用依赖的模块中通过import scope依赖,就可以引入dependencyManagement。例如可以写这样一个用于依赖管理的pom:

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.test.sample</groupId>
  4. <artifactId>base-parent1</artifactId>
  5. <packaging>pom</packaging>
  6. <version>1.0.0-SNAPSHOT</version>
  7. <dependencyManagement>
  8. <dependencies>
  9. <dependency>
  10. <groupId>junit</groupId>
  11. <artifactid>junit</artifactId>
  12. <version>4.8.2</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>log4j</groupId>
  16. <artifactid>log4j</artifactId>
  17. <version>1.2.16</version>
  18. </dependency>
  19. </dependencies>
  20. </dependencyManagement>
  21. </project>

然后我就可以通过非继承的方式来引入这段依赖管理配置

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.test.sample</groupId>
  5. <artifactid>base-parent1</artifactId>
  6. <version>1.0.0-SNAPSHOT</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactid>junit</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>log4j</groupId>
  18. <artifactid>log4j</artifactId>
  19. </dependency>

注意:import scope只能用在dependencyManagement里面

这样,父模块的pom就会非常干净,由专门的packaging为pom来管理依赖,也契合的面向对象设计中的单一职责原则。此外,我们还能够创建多个这样的依赖管理pom,以更细化的方式管理依赖。这种做法与面向对象设计中使用组合而非继承也有点相似的味道。

那么,如何用这个方法来解决SpringBoot的那个继承问题呢?

配置如下:

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-dependencies</artifactId>
  6. <version>1.3.3.RELEASE</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. </dependencies>

这样配置的话,自己的项目里面就不需要继承SpringBoot的module了,而可以继承自己项目的module了。

转载地址:https://blog.csdn.net/mn960mn/article/details/50894022

pom.xml中使用“import”的scope来解决Maven项目单继承问题的更多相关文章

  1. Maven pom.xml中的元素modules、parent、properties以及import

    前言 项目中用到了maven,而且用到的内容不像利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)用的那么简单:maven的核心是pom.xml,那么我就它 ...

  2. Maven pom.xml中的元素modules、parent、properties以及import(转)

    前言 项目中用到了maven,而且用到的内容不像利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)用的那么简单:maven的核心是pom.xml,那么我就它 ...

  3. (转)如何在maven的pom.xml中添加本地jar包

    1 maven本地仓库认识 maven本地仓库中的jar目录一般分为三层:图中的1 2 3分别如下所示: 1 groupId 2 artifactId 3 version 4 jar包的依赖 如果要将 ...

  4. (转)如何在maven的pom.xml中添加本地jar包

    转载自: https://www.cnblogs.com/lixuwu/p/5855031.html 1 maven本地仓库认识 maven本地仓库中的jar目录一般分为三层:图中的1 2 3分别如下 ...

  5. eclipse中基于maven构建的web项目pom.xml中指定的jar包无法发布到tomcat中

    eclipse运行maven web项目报错: 信息: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 07, 2015 11:50:44 下午 or ...

  6. 只要项目是maven构建的,pom.xml中依赖的jar包全都默认去你电脑本地仓库去找

    只要项目是maven构建的,pom.xml中依赖的jar包全都默认去你电脑本地仓库去找

  7. 在maven pom.xml中加载不同的properties ,如localhost 和 dev master等jdbc.properties 中的链接不一样

    [参考]:maven pom.xml加载不同properties配置[转] 首先 看看效果: 点开我们项目中的Maven projects 后,会发现右侧 我们profile有个可勾选选项.默认勾选l ...

  8. pom.xml中添加远程仓库

    maven的pom.xml中添加远程仓库 <repositories> <repository> <id>mvnrepository</id> < ...

  9. maven 在pom.xml 中指定仓库位置

    ...... 在pom.xml 中添加 仓库位置(这样遇到私服没有的依赖,就会去这下载) </properties> <repositories><!-- 代码库 --& ...

随机推荐

  1. CAS自旋volatile变量

    public final int getAndIncrement() { for (;;) { int current = get(); // 取得AtomicInteger里存储的数值 int ne ...

  2. HTML5 Canvas实战之刮奖效果【转】

    开源项目地址:https://github.com/artwl/Lottery 作者博客地址:http://www.cnblogs.com/jscode/p/3580878.html 谢谢浏览!

  3. Redis 获取和设置密码

    1.config get reuqirepass //获取当前密码 2.config set requirepass "password"//设置当前密码,双引号里面为密码

  4. git 忽略提交

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法. git目录下新建一个.gitignore(window下使用git bash工具或者cmd ...

  5. 为元素添加 title 属性

    ---恢复内容开始--- 可以使用title属性(不要与title元素混淆)为网站上任何部分加上提示标签. ... <ul title="Table of Contents" ...

  6. CSS3 2D 变换

    CSS2D transform 表示2D变换,目前获得了各主流浏览器的支持,是CSS3中具有颠覆性的特征之一,可以实现元素的位移.旋转.倾斜.缩放,甚至支持矩阵方式,可以取代大量之前只能靠Flash才 ...

  7. Hibernate 5.x 生成 SessionFactory 源码跟踪分析

    我们要使用 Hibernate 的功能,首先需要读取 Hibernate 的配置文件,根据配置启动 Hibernate ,然后创建 SessionFactory. 创建 SessionFactory ...

  8. jq 实现切换菜单选中状态

    点击导航菜单,切换选中状态 效果: 思路:首先获取选中的URL,再通过正则判断是否相同,相同就加上相应的属性,不相同就去除相应的属性. html代码 <div class="layui ...

  9. Java开发环境之Gradle

    查看更多Java开发环境配置,请点击<Java开发环境配置大全> 拾伍章:Gradle安装教程 1)下载Gradle安装包 官网下载:https://gradle.org/releases ...

  10. C#-Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046}

    异常信息如下 捕获到执行这句时异常: Excel.Application ep = new Excel.ApplicationClass(); Retrieving the COM class fac ...