常用maven标签

packaging

<packaging>pom</packaging>

pom:父项目

jar:内部调研或作服务使用

war:需要部署的项目

默认是jar


type与scope实现多继承

我们知道Maven的继承和Java的继承一样,只能单继承,无法实现多继承,你是否想过我们如果要继承多个父模块的时候应该怎么做呢?或许你会想只往一个父模块中添加jar包依赖,只继承一个父模块就可以了,但是这样的话所有的jar包都混合在一起了,jar包分类就不在清晰了,其实我们可以用另外一种方式实现多模块继承,这个方法就是使用<type>pom</type><scope>import</scope>,解释一下:type标签的默认值是jar,代表我们依赖导入的是一个jar包,现在我们设置成了pom,说明导入的是一个父模块,后面的scope标签中的值import代表把父模块中的jar包导入进来,不过需要注意的是<type>pom</type><scope>import</scope>,这种方式只能用在<dependencyManagement></dependencyManagement>中,那么再来一个例子吧,在SpringCloud项目中,我们导入SpringCloud依赖和SpringBoot依赖的代码如下:

<dependencyManagement>

	<dependencies>
		<!-- SpringCloud的jar包依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Dalston.SR1</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<!-- SpringBoot的jar包依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>1.5.9.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		</dependencies>
		
	</dependencyManagement>
12345678910111213141516171819202122

上面的做法其实就相当于实现了多继承,毕竟我们导入了SpringCloud和SpringBoot的两个父模块的jar包依赖


spring-boot-dependencies

spring 提供了 spring-boot-dependencies 用于方便管理项目的依赖,只需在 dependencyManagement 里面配置好 spring-boot-dependencies 的版本,在 dependencies 里面就不用再指定其它版本号了,如:


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
</dependencies>

其实 spring-boot-dependencies 就是一个 pom.xml,里面管理了很多常见第三方的依赖和版本。


本科计算机bi-ye-she-ji辅导,远程安装运行+部署,欢迎各位小伙伴打扰~企鹅号:298-150-5753

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值