0
点赞
收藏
分享

微信扫一扫

maven--pom.xml--标签大全

椰果玩安卓 2022-02-25 阅读 98


简介

说明

        本文介绍maven的pom.xml的结构及常用标签的使用。详细讲解的标签包括:parent,relativePath,modules,repositories,profiles,build等。

官网

​​pom.xml - maven​​

简介

对于Idea来说,最后maven的插件总会覆盖掉生成的pom.xml文件,这时可以使用ctrl+z来取消。

pom.xml构成

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 基础设置 -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>

<name>...</name>
<description>...</description>
<url>...</url>

<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>

<!--构建设置 -->
<build>...</build>
<reporting>...</reporting>

<!-- 更多项目信息 -->
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>

<!-- 环境设置-->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>

parent

简介

其他网址

​​Maven 的聚合(多模块)和 Parent 继承_偶尔记一下 - mybatis.io-​​

实例项目

​​Idea项目系列--多模块项目(淘淘商城)_开发工具_feiying0canglang的博客-​​

​​Dubbo系列--快速入门_feiying0canglang的博客​​

简介

        parent作用:继承。继承可以使得子POM可以获得 parent 中的各项配置,可以对子pom进行统一的配置和依赖管理。比如:parent指定的项目里的pom.xml引入了abc依赖,则子项目无需引入abc依赖可以直接使用(如果子项目也引入了abc依赖,则以子项目引入的版本为准)。

以下父POM元素不能被子POM继承:


  • artifactId
  • name
  • prerequisites

以下父POM元素能被子POM继承:


  • groupId
  • version
  • description
  • url
  • inceptionYear
  • organization
  • licenses
  • developers
  • contributors
  • mailingLists
  • scm
  • issueManagement
  • ciManagement
  • properties
  • dependencyManagement
  • dependencies
  • repositories
  • pluginRepositories
  • build

  • plugin executions with matching ids
  • plugin configuration
  • etc.

  • reporting
  • profiles

用法

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.example.abc</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
<!---> relativePath元素不是必须的,指定后会优先从指定的位置查找父pom。 <--->
</parent>

<artifactId>my-project</artifactId>
</project>

注意:parent项目的打包方式必须是pom,本处的my-parent就要这么指定:<packaging>pom</packaging>

项目结构

以下两种结构都需要parent节点。

maven--pom.xml--标签大全_maven

maven--pom.xml--标签大全_xml_02

pom.xml示例

此例子中,“spring-boot-starter-parent”会为开发者提供常用jar的版本管理。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

不用parent

        并非所有的公司都需要这个parent,有的时候,公司会自定义parent,我们的SpringBoot项目只能继承一个parent,继承了公司内部的parent就不能再继承这个了,这个时候怎么办呢?

一个简单的办法就是我们自行定义dependencyManagement节点,然后在里面定义好版本号,接下来引用依赖时也就不用写版本号了,像下面这样:

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

        这样写之后,就可以不用继承spring-boot-starter-parent了,但依赖的版本号问题虽然解决了,但是关于打包的插件、编译的JDK版本、文件的编码格式等等这些配置,在没有parent的时候,这些统统要自己去配置。

Java版本的配置很简单,添加一个plugin即可:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

编码格式,在pom.xml中加入如下配置:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

建议:公司自定义的parent继承官方的SpringBoot parent或者将官方的parent拷贝到自定义的parent中。

relativePath

其他网址

​​Maven parent.relativePath 说明_gzt19881123的专栏-​​

​​pom.xml中<parent>和<modules>详解_盛夏与微风​​

 简介

 网上一大堆不准确说明。获得准确说明的方法:直接在Idea里边,点击parent标签,进去查看。


  1. relativePath 是Maven为了寻找父模块pom.xml所额外增加的一个寻找路径
  2. relativePath 默认值为 /pom.xml
  3. Maven 寻找父模块pom.xml 的顺序如下:


    1. first in the reactor of currently building projects
      这里一个maven概念 反应堆(reactor ),意思就是先从工程里面有依赖相关的模块中找你引入的 parent 的pom.xml。
    2. then in this location on the filesystem
      然后从 你定义的  <relativePath > 路径中找。如果设为空值(<relativePath />),则跳过该步骤。默认值 ../pom.xml 则是从上级目录中找啦。
    3. then the local repository
      如果 (1)(2) 步骤没有则从 本地仓库找啦。
    4. and lastly in the remote repo
      上面都找不到了,最后只能从远程仓库找啦,再找不到就报错给你看 



实例

采用默认值

默认值为/pom.xml。如果该工程属于父工程的子工程(即子工程文件夹在父工程文件夹内),则<relativePath>取默认值即可。如下所示

project A
----pom.xml
project B
----pom.xml

 自定义位置

如果该工程是父工程的平级工程(即父工程和子工程的文件夹均在同一个目录下),则需要显示指出父工程的pom.xml的位置

project A
----pom.xml
project B
----pom.xml

此时<relativePath>为…/${project A foldername}/pom.xml

示例:

<parent>
<!-- 父工程的相对目录 -->
<relativePath>../parent/pom.xml</relativePath>
<groupId>com.summerzhou</groupId>
<artifactId>yycgproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

modules

其他网址

​​Maven 的聚合(多模块)和 Parent 继承_偶尔记一下 - mybatis.io-​​

简介

        具有模块的项目被称为多模块或聚合项目。模块是此POM列出并作为一组执行的项目。通过一个pom.xml打包的项目可以将它们列为模块来聚合成一组项目进行构建,这些模块名是这些项目的相对目录。

        打包方式要改为pom,不能使用jar。

示例:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>

<modules>
<module>my-project</module>
<module>another-project</module>
</modules>
</project>

实例项目


​​Dubbo系列--SpringBoot整合Dubbo实例_feiying0canglang的博客​​


项目结构

maven--pom.xml--标签大全_maven

下边这种结构不需要modules节点

maven--pom.xml--标签大全_xml_02

repositories

<repositories>
<repository>
<name>Nexus Snapshots</name>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<layout>default</layout>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>

profiles

其他网址

​​Maven 教程(17)— Maven Profile 和 Filtering 简介_挖坑埋你​​

​​maven使用filter动态配置项目_登上庐山-​​

​​maven profile <filtering>true</filtering>的作用_坚持,让梦想闪耀!​​

build

其他网址

​​maven--pom.xml(build元素)_feiying0canglang的博客​​

properties

是为pom定义一些常量,在pom中的其它地方可以直接引用。例如:

<properties>
<mysql.version>5.1.41</mysql.version>
<file.encoding>UTF-8</file_encoding>
</properties>

<dependencyManagement>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencyManagement>

还可以使用project.xxx引用pom里定义的其它属性:如$(project.version} 

杂项

groupId​:项目或组织的唯一标志,且配置时生成路径也由此生成,如org.myproject.mojo生成的相对路径为:/org/myproject/mojo

artifactId​:项目的通用名称

version​:项目的版本

packaging​:打包机制,如:pom,jar,war,maven-plugin,ejb,ear,rar,par

        groupId,artifactId,version,packaging这四项组成了项目的唯一坐标。一般前面三项就可以组成项目的唯一坐标了。

name​:用户描述项目的名称,无关紧要的东西,可选

url​:只是写明开发团队的网站,无关紧要,可选

其他网址

​​Maven pom.xml中的元素modules、parent、properties以及import - 青石路​​

​​maven核心,pom.xml详解(转) - 一个人的天空@ - ​​

​​maven--Idea操作_feiying0canglang的博客-​​

《maven实战》


举报

相关推荐

0 条评论