Maven pom.xml常用标签说明

本人在进行项目开发时进行进行一些pom依赖的配置,总结一下常用标签,方便自己以后开发。


目录

modelVersion

parent

groupId

artifactId

version

relativePath

packaging

modules

name、url、description

developers

properties

dependencies

dependency

dependencyManagement

scope

type

exclusions

optional

classifier

build

finalName

plugins

pluginManagement

configuration

source

target

encoding


modelVersion

  • 描述这个pom文件是遵从哪个版本的项目描述符
<modelVersion>4.0.0</modelVersion>

parent

  • 用在子模块中,实现对父模块的继承

groupId

  • 项目主标识,用于定义当前项目属于的实际项目,格式与项目创建的包是一样的,通常为公司域名反写

artifactId

  • 项目名或模块名或项目名+模块名组成

version

  • 当前项目版本号,一般由三个数字组成,第一个0表示大版本号,第二个0表示分支版本号,第三个1表示小版本号SNAPSHOT代表当前版本类型为快照版本,还有alpha内部版本、beta公测版本、release发布版本、ga正式版本等 

relativePath

  • 表示父模块POM的相对路径
  • 设定一个空值将始终从仓库中获取,不从本地路径获取
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

packaging

  • maven打包方式,默认为jar,还有:pom,maven-plugin,war,rar,zip
  • 项目中一般使用maven进行模块管理,每个模块下对应都有一个pom文件,pom文件维护了各个模块之间的依赖和继承关系。项目模块化可以将通用的部分抽离出来,方便重用;修改一部分代码不再是build整个项目,缩短了build时间;此外各模块都有自己的pom文件,结构更清晰。
<packaging>jar</packaging>

modules

  • 聚合多个maven项目,同时对所有聚合项目进行编译
<modules>
    <module>module-a</module>
    <module>module-b</module>
</modules>

name、url、description

  • 项目描述名,url,详细描述,产生项目文档使用
<name>Harris_j</name>
<url>http://maven.apache.org</url>
<description>maven pom.xml常用标签说明</description>

developers

  • 开发人员列表,项目发布使用
<developers>
    <!-- 某个项目开发者的信息 -->
    <developer>
        <!-- 项目开发者的唯一标识符 -->
        <id>001</id>
        <!-- 项目开发者的全名 -->
        <name>harris_j</name>
        <!-- 项目开发者的email -->
        <email> [email protected] </email>
        <!-- 项目开发者的主页的URL -->
        <url>http://******.com</url>
        <!-- 项目开发者在项目中扮演的角色,角色元素描述了各种角色 -->
        <roles>
            <role>developer</role>
        </roles>
        <!-- 项目开发者所属组织 -->
        <organization>com.qmxc</organization>
        <!-- 项目开发者所属组织的URL -->
        <organizationUrl>https://www.venustech.com.cn/</organizationUrl>   
    </developer>
</developers>

properties

  • 属性列表,相当于定义的公共常量,引用方式比如:${spring-cloud.version}
<properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <java.version>1.8</java.version>
     <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

dependencies

  • 依赖列表

dependency

  • 具体依赖项,下面主要包含依赖的坐标、类型、范围等信息
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

dependencyManagement

  • 作用其实相当于一个对所依赖jar包进行版本管理的管理器,只对版本进行管理,不会实际引入jar
  • pom.xml文件中,jar的版本判断的两种途径:
  1. 如果dependencies里的dependency自己没有声明version元素,那么maven就  会倒dependencyManagement里面去找有没有对该artifactId和groupId进行过版本声明,如果有,就继承它,如果  没有就会报错,告诉你必须为dependency声明一个version  
  2. 如果dependencies中的dependency声明了version,那么无论dependencyManagement中有无对该jar的version声明,都以dependency里的version为准。
<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>
        </dependencies>
</dependencyManagement>

scope

  • compile:默认的scope。任何定义在compile scope下的依赖将会在所有的class paths下可用。maven工程会将其打包到最终的artifact中。如果你构建一个WAR类型的artifact,那么在compile scope下引用的JAR文件将会被集成到WAR文件内。
  • provided:这个scope假定对应的依赖会由运行这个应用的JDK或者容器来提供。最好的例子就是servlet API。任何在provided scope下定义的依赖在构建时的类路径里是可用的,但是不会被打包到最终的artifact中。如果是一个WAR的文件,servlet API在构建时的类路径里是可用的,但是并不会被打包到WAR文件中。
  • runtime:在runtime scope下定义的依赖只会在运行期可用,而在构建期的类路径下不可用。这些依赖将会被打包到最终的artifact中。比如你有一个基于web的应用需要在运行时访问MySQL数据库。你的代码没有任何MySQL数据库驱动的硬依赖。你的代码仅仅是基于JDBC API来编写,在构建期并不需要MySQL数据库驱动。然而,在运行期,就需要相应的驱动来操作MySQL数据库了。因此,这个驱动应该被打包到最终的artifact中。
  • test:只用于测试变异的依赖(比如JUnit),execution必须定义在test scope下。这些依赖不会被打包到最终的artifact中。
  • system:于provided scope很像。唯一的区别在于,在system scope中,你需要告诉Maven如何去找到这个依赖。如果你要引用的依赖在Maven仓库中不存在时,就可以用这个scope。不推荐使用system依赖。
  • import:从其它的pom文件中导入依赖设置。
<scope>import</scope>

type

  • 依赖的类型
<type>pom</type>

exclusions

  • 排除依赖传递列表,比如A依赖B,B依赖C,但是A并没有使用C的功能,可以把C排除
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>1.2.6</version>
   <exclusions>
       <exclusion>
            <groupId></groupId>
            <artifactId></artifactId>
       </exclusion>
   </exclusions>
</dependency>

optional

  • 主动设置禁止自己被传递,只在当前项目中使用
<optional>true</optional>

classifier

  • 在相同版本下针对不同的环境或者jdk使用的jar,如果配置了这个元素,则会将这个元素名在加在最后来查找相应的jar
  • 主要是用来标识特殊jar包的
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <classifier>9.4-1201-jdbc41</classifier>
</dependency>

build

  • 构建插件

finalName

  • Maven定制化打包后的包名

plugins

  • 插件列表

pluginManagement

  • 插件管理列表

configuration

  • 插件扩展配置

source

  • 源代码编译版本

target

  • 目标平台编译版本

encoding

  • 编译字符集编码
  <build>
    <finalName>HarrisProject</finalName>  

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>
            <plugin>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

</build>

该篇文章参考诸多大佬文章:
参考原文:https://blog.csdn.net/cnweike/article/details/52221410 

参考原文:https://blog.csdn.net/javaloveiphone/article/details/52080886

猜你喜欢

转载自blog.csdn.net/qq_32352777/article/details/85329933
今日推荐