美文网首页
maven之pom.xml配置文件详解

maven之pom.xml配置文件详解

作者: 刘荣杰 | 来源:发表于2021-05-14 14:45 被阅读0次

maven有什么用

我们在进行项目开发时会有很多的依赖项,就是用别人造好的轮子,比如lombok等一系列,但是我们手动来管理就会非常繁琐,所以出现了maven来帮我们自动管理,我们只需要在pom.xml引入我们所需要的包,就会帮我们自动导入相关的jar。

pom.xml的节点分布

<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>


    <!-- 依赖配置 -->
    <dependencies>...</dependencies>
    <parent>...</parent>
    <dependencyManagement>...</dependencyManagement>
    <modules>...</modules>
    <properties>...</properties>

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

    <!-- 项目信息 -->
    <name>...</name>
    <description>...</description>
    <url>...</url>
    <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>

一个基本maven项目的配置

第一部分,项目坐标,信息描述等

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>project</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>test Maven Webapp</name>
    <url>http://maven.apache.org</url>

modelVersion:pom文件的模型版本
groupId:公司名
artifactId:项目名
name和url相当于项目描述
group id+artifactId+version:项目在仓库中地址,可以此三项定位项目。

第二部分,引入jar包

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

groupid+artifactId+version:定位依赖位置
scope:作用范围,test指该依赖仅在maven测试中使用,发布时会忽略。

第三部分,构建项目

    <build>
        <finalName>helloworld</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

build:项目构建时的配置
finalName:在浏览器中的访问路径,如果将它改成helloworld,再执行maven--update,这时运行项目的访问路径是

               http://localhost:8080/helloworld/   而不是项目名的  http://localhost:8080/test

plugins:插件,

group id+artifact id+version:插件在仓库中的坐标

configuration:设置插件的参数值

在这里相信读者和我有一样的疑惑,这里的plugins和之前的dependency有什么差别,如果我们了解java编程机制的话,我们知道jdk会将我们的.class文件编译成.CLASS文件即字节码文件再由虚拟机执行。dependency就是导入相应的.CLASS文件,虚拟机运行时使用使用,而依赖可以在jdk编译时使用,例如lombok添加@Data就应该在编译成CLASS文件添加get,set方法,所以放在build中。

补充

parent标签

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

大家在创建spring项目中会发现该项配置,这是引入该项目的pom中依赖配置。

dependencyManagement

该项和parent标签相关,在父类pom中有,表示子类根据需要选择相应的dependency

举例:父pom

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.demo</groupId>
    <artifactId>demo-platform-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>
 
    <name>demo-platform-parent</name>
    <url>http://demo.com</url>
 
    <properties>
        <junit.version>4.12</junit.version>
       
        <spring.version>4.3.8.RELEASE</spring.version>
    </properties>
 
    
    <dependencyManagement>
       
            <!-- TEST begin -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>
 
        </dependencies>
    </dependencyManagement>
 
</project>

子pom:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependencies><dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
 
 </dependency></dependencies>

我们说groupId+artifactId+version确定依赖,引入父pom后可以根据groupId+artifactId找到父pom相关依赖。

相关文章

网友评论

      本文标题:maven之pom.xml配置文件详解

      本文链接:https://www.haomeiwen.com/subject/klyqjltx.html