Maven pom文件格式


<parent>
	<!--父pom文件-->
	<!--可以从父文件继承 dependencies,dependencyManagement 等-->
	<!--如果只是想要方便子项目jar包的版本管理,可以只在dependencyManagement中定义jar包,
	不需要这个jar包的子项目不会引入这个jar包,需要这个jar包的子项目引入jar包就不用定义版本了-->
</parent>
<!--打包类型-->
<!--在父级项目中的pom.xml文件使用的packaging配置一定为pom。
父级的pom文件只作项目的子模块的整合,在maven install时不会生成jar/war压缩包。-->
<packaging>pom</packaging>
<properties>
	<!--属性,配合占位符使用${}-->
	<!--注意:Maven中有就近依赖原则,在 parent 中定义的 property 
	可以在子 pom 文件中覆盖 -->
	
	<!--例1-->
	<bom.version>4.45.0</bom.version>
	<framework.artid>framework-bom</framework.artid>
</properties>
<dependencyManagement>
	<!--只是对版本进行管理,不会实际引入jar-->
	<!--如果 dependencies 有指定版本,以 dependencies 为主-->

	<!--例1-->
	<dependency>
		<groupId>com.xxx.framework</groupId>
    	<artifactId>${framework.artid}</artifactId>
    	<version>${bom.version}</version>
    	<!--引入为pom文件,默认为jar包-->
    	<type>pom</type>
    	<!-- 控制 Jar 包在哪些范围被加载和使用 -->
    	<scope>import</scope>
    </dependency>   
</dependencyManagement>
<dependencies>

</dependencies>

 <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <!--whether or not to load extensions of this plugin-->
        <extensions>false</extensions>
        <!--whether or not this plugin configuration should apply to POMs which inherit from this one. 
        Default value is true-->
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>...</dependencies>
        <executions>...</executions>
      </plugin>
    </plugins>
  </build>
  
<profiles>
	<!--多环境控制-->
	<!-- 可以让maven在不同环境加载不同依赖等 -->
    <profile>
         <id>dev</id>
         <dependencies></dependencies>
         ....
    </profile>
</profiles> 

scope的值

  1. compile
    不声明scope元素的情况下的默认值;compile表示被依赖包需要参与当前项目的编译,测试,运行,是一个比较强的依赖。
  2. test
    依赖包只有在测试编译和测试运行阶段可用,不会被打包到项目jar包中,同时如果项目A依赖于项目B,项目B中的test作用域下的依赖不会被继承。
  3. import
    通过parent继承的方式,只能继承一个pom文件。实际开发中,用户很可能需要继承自己公司的标准parent配置,这个时候可以使用 scope=import 来实现多继承,注意此继承只继承dependencyManagement。

依赖冲突在这里插入图片描述

第一原则:路径最近者优先

A --> B --> X(1.1)         // dist(A->X) = 2
A --> C --> D --> X(1.0)   // dist(A->X) = 3
//结果是使用X(1.1)作为依赖

第二原则:第一声明者优先

//路径长度相同
A --> B --> X(1.1)   // dist(A->X) = 2
A --> C --> X(1.0)   // dist(A->X) = 2
<dependencies>
    dependency B
    dependency C
</dependencies>
//最终会选择X(1.1)依赖

其它情况:覆盖策略
上面两条原则是maven应对间接依赖的解决办法,对于直接依赖,最终会引入最后一个声明的依赖。
解决依赖冲突:
排除依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

版本锁定
使用dependencyManagement 进行版本锁定,dependencyManagement可以统一管理项目的版本号,确保应用的各个项目的依赖和版本一致。

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        
    </dependencies>

profiles

常用环境激活方式:
idea
勾选自己想要的环境配置
在这里插入图片描述

插件executions字段

	    <executions>
          <execution>
            <id>echodir</id>
            <!--插件的目标,可以在idea的maven面板中点 Plugins->具体插件 查看-->
            <goals>
              <goal>run</goal>
            </goals>
            <!--执行的阶段,取值范围为maven生命周期-->
            <phase>verify</phase>
            <inherited>false</inherited>
            <configuration>
              <tasks>
                <echo>Build Dir: /home/jenkins/82467a7c/workspace/aven_maven-box_maven-site_master/target</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>

https://blog.csdn.net/x2hg123/article/details/109203759
https://blog.csdn.net/qq_29235677/article/details/118528996
参考:
http://mvnbook.com/maven-deep-thinking.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值