码迷,mamicode.com
首页 > 其他好文 > 详细

Maven之pom.xml配置文件详解

时间:2019-06-05 23:48:28      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:url   源文件   compile   基本   settings   clu   develop   includes   依赖   

Maven之pom.xml配置文件详解

                                                                                                                                                                                                                                                                                                                                                                                                                          2019-06-05

一、什么是pom?

 

       POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示,名称叫做pom.xml。作用类似ant的build.xml文件,功能更强大。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。事实上,在Maven世界中,project可以什么都没有,甚至没有代码,但是必须包含pom.xml文件。

   

二、概览

 下面是一个POM项目中的pom.xml文件中包含的元素。注意,其中的modelVersion是4.0.0,这是当前仅有的可以被Maven2&3同时支持的POM版本,它是必须的。

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

       

        <!-- 基本设置 The Basics -->

        <groupId>...</groupId>

        <artifactId>...</artifactId>

        <version>...</version>

        <packaging>...</packaging>

        <dependencies>...</dependencies>

        <parent>...</parent>

        <dependencyManagement>...</dependencyManagement>

        <modules>...</modules>

        <properties>...</properties>

       

        <!-- 构建过程的设置 Build Settings -->

        <build>...</build>

        <reporting>...</reporting>

       

        <!-- 项目信息设置 More Project Information -->

        <name>...</name>

        <description>...</description>

        <url>...</url>

        <inceptionYear>...</inceptionYear>

        <licenses>...</licenses>

        <organization>...</organization>

        <developers>...</developers>

        <contributors>...</contributors>

       

        <!-- 环境设置 Environment Settings -->

        <issueManagement>...</issueManagement>

        <ciManagement>...</ciManagement>

        <mailingLists>...</mailingLists>

        <scm>...</scm>

        <prerequisites>...</prerequisites>

        <repositories>...</repositories>

        <pluginRepositories>...</pluginRepositories>

        <distributionManagement>...</distributionManagement>

        <profiles>...</profiles>

    </project>

 

三、maven POM.xml详解

1、基本配置

modelVersion

Maven模块版本,目前我们一般都取值4.0.0

groupId

整个系统的名称。

artifactId

子模块名称。

packaging

打包类型,可取值:jar,war等等,这个配置用于package的phase,具体可以参见package运行的时候启动的plugin,后面有机会我们会讲述如何配置打包的插件。

   

 

 

 

 

 

 

 

2、dependencies

    依赖关系。实际上pom之间存在好三种关系:继承、依赖、聚合。我们先讲依赖,这也是最重要的关系。

 

groupId

依赖项的groupId

artifactId

依赖项的artifactId

version

依赖项的版本

 

 

scope

依赖项的适用范围:

    • compile,缺省值,适用于所有阶段,会随着项目一起发布。
    • provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
    • runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
    • test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
    • system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。

之前例子里的junit就只用在了test中。

exclusions

排除项目中的依赖冲突时使用。

2.1、我们可能经常会遇到这样一个问题:我们的项目有两个依赖项:A & B,而且A和B同时依赖了C,但不是同一个版本。那么我们怎么办呢?

2.1.1、添加检查插件

            <reporting>

                    <plugins>

                        <plugin>

                            <groupId>org.apache.maven.plugins</groupId>

                            <artifactId>maven-project-info-reports-plugin</artifactId>

                        </plugin>

                    </plugins>

                </reporting>

            然后运行:mvn project-info-reports:dependencies,来查看依赖项报告。

2.1.2、去除依赖项

        如果我们需要在某个dependency中去除某个依赖项,直接这样即可:

        <!-- Struts2 -->

                <dependency>

                    <groupId>org.apache.struts</groupId>

                    <artifactId>struts2-core</artifactId>

                    <version>${struts.version}</version>

                    <exclusions>

                        <exclusion>

                            <groupId>org.freemarker</groupId>

                            <artifactId>freemarker</artifactId>

                        </exclusion>

                    </exclusions>

                </dependency>

3、继承

    我的repository下面有个例子就直接拿来用了:

    <modelVersion>4.0.0</modelVersion>

      <parent>

        <groupId>com.thoughtworks.xstream</groupId>

        <artifactId>xstream-parent</artifactId>

        <version>1.4.3</version>

      </parent>

      <artifactId>xstream</artifactId>

      <packaging>jar</packaging>

      <name>XStream Core</name>

    其中的parent表示父pom是com.thoughtworks.xstream的xstream-parent的1.4.3版本。继承关系比较简单,这里不做过多介绍。

4、聚合

    我们可以通过一个大的项目来整合各个小的模块:

    <modules>

       <module>my-app</module>

    </modules>

   

5、属性

    属性表述类似于EL表达式,ANT中也同样有,所以我们的properties字段可以这样使用:

   

    <!-- mysql -->

    <dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

        <version>${mysql.version}</version>

    </dependency>

6、构建

6.1 plugin

    Plugin的配置如下:

   

    <pluginManagement>

        <plugins>

            <plugin>

                <groupId>org.apache.tomcat.maven</groupId>

                <artifactId>tomcat7-maven-plugin</artifactId>

                <version>2.1</version>

                <configuration>

                <tomcat-url>http://localhost:8080/manager/html</tomcat-url>

                <server>tomcat_localtest</server>

                </configuration>

            </plugin>

        </plugins>

    </pluginManagement>

    我们可以看到同样要哟偶groupId、artifactId、version还有一些配置参数。

   

6.2 resource

    指定你在Build时需要的资源文件:

   

    <resources>

        <resource>

            <targetPath>WEB-INF/resource</targetPath>

            <!-- 不对文件中的表达式进行处理 -->

            <filtering>false</filtering>

            <directory>${basedir}/src/test/resources</directory>

                <includes>

                    <include>include.xml</include>

                </includes>

                <excludes>

                    <exclude>exclude.xml</exclude>

                </excludes>

        </resource>

    </resources>

 

 

宝宝们有啥问题可以联系我呦!!!

QQ邮箱:2504900804@qq.com

Maven之pom.xml配置文件详解

标签:url   源文件   compile   基本   settings   clu   develop   includes   依赖   

原文地址:https://www.cnblogs.com/MrZhangxd/p/10982380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!