maven的标准pom.xml详解

maven是构建和管理理项目的利器,pom.xml 是其核心。一个标准的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">

    <!--指定了当前pom模型的版本,必须这样写,不可更改-->
    <modelVersion>4.0.0</modelVersion>

    <!--坐标信息,并未固定要求,但约定俗成,模块概念和idea有些类似,一个项目分为多个模块-->
    <groupId>包名</groupId>
    <artifactId>实际项目名+模块名</artifactId>

    <!--1表示大版本号,0表示小版本号,
    SNAPSHOT:快照,表示该版本正在开发中
    release:稳定版本
    beta:公测版,玩过游戏的都懂
    alpha:内部测试版
    GA:正式发布版-->
    <version>1.0-SNAPSHOT</version>

    <!--maven项目的打包方式,默认为jar,可供选择的有war、zip、pom-->
    <packaging></packaging>

    <!--项目描述名-->
    <name></name>

    <!--项目地址-->
    <url></url>

    <!--项目描述-->
    <description></description>

    <!--开发人员的列表信息-->
    <developers>
        <developer></developer>
    </developers>

    <!--许可证信息-->
    <licenses></licenses>

    <!--组织信息-->
    <organization></organization>

    <!--依赖项信息,依赖到的jar包-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <type></type>

            <!--依赖的范围,表示本依赖应用于项目中的哪些阶段如下:
            compile:默认值。表示该依赖在编译、测试、运行阶段都有效
            provided:在编译和测试时有效,在运行时不会被加入
            runtime:在测试和运行时有效
            test:在测试范围内有效
            system:在编译和测试时有效,与provided类似,不过要与本地系统相关联,可移植性差
            import:在dependenceManagement中使用,表示导入别的项目的依赖到本项目中,关于本条请参看文末引用的别人的博客-->
            <scope>test</scope>

            <!--设置依赖是否可选,取值为true或false,默认是false,如果是false,则子项目必然继承父项目的依赖(不可选)
            若为true,则子项目可以自己选择是否需要父项目的依赖,若需要就手动引入,若不需要就不引入-->
            <optional>true</optional>

            <!--排除依赖列表,如果a依赖b,b依赖c,那么默认的a依赖c,但是我a就是不想依赖c,则可以在这里排除掉c-->
            <exclusions>
                <exclusion></exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!--一般为了统一管理多个项目,让他们的依赖都具有相同的版本,在所有子模块中的依赖标签都不指定明确的版本号,
    maven会自动向其父级查找,直到找到一个父模块拥有dependencyManagement标签,指定了所有依赖的版本号。
    这就保证所有模块的依赖版本都来自于同一个父模块的dependencyManagement指定。-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId></groupId>
                <artifactId></artifactId>
                <version></version>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <!--对构建项目的支持-->
    <build>

        <!--插件-->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>

                <!--表示该插件在什么时候执行-->
                <executions>
                    <execution>

                        <!--表示在打包阶段之后执行本插件-->
                        <phase>package</phase>

                        <!--执行方式,一般是与java的启动参数类似,例如:run等-->
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!--用来指定继承的父模块-->
    <parent></parent>

    <!--多模块共同管理,一起编译-->
    <modules>
        <module>A</module>
        <module>B</module>
        <module>C</module>
    </modules>

    <!--属性,可以指定变量,在其他地方用${junit.version}来代替4.10,如下所示-->
    <properties>
        <junit.version>4.10</junit.version>
    </properties>

</project>

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

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>

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pom.xmlMaven项目的核心配置文件,它包含了项目的基本信息、依赖关系、构建配置等重要信息。下面是pom.xml文件的详细解释: 1. 项目基本信息 ``` <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> ``` - groupId:项目的组织或公司名称。 - artifactId:项目的名称。 - version:项目的版本号。 2. 依赖关系 ``` <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.6.RELEASE</version> </dependency> </dependencies> ``` - dependencies:依赖关系列表。 - dependency:依赖项。 - groupId:依赖项的组织或公司名称。 - artifactId:依赖项的名称。 - version:依赖项的版本号。 3. 插件 ``` <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> ``` - build:构建配置。 - plugins:插件列表。 - plugin:插件。 - groupId:插件的组织或公司名称。 - artifactId:插件的名称。 - version:插件的版本号。 - configuration:插件的配置。 4. 项目打包方式 ``` <packaging>jar</packaging> ``` - packaging:项目的打包方式,常见的有jar、war、pom等。 5. 项目依赖管理 ``` <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.6.RELEASE</version> </dependency> </dependencies> </dependencyManagement> ``` - dependencyManagement:依赖管理。 - dependencies:依赖关系列表。 - dependency:依赖项。 - groupId:依赖项的组织或公司名称。 - artifactId:依赖项的名称。 - version:依赖项的版本号。 6. 项目构建描述 ``` <description>This is a sample Maven project.</description> ``` - description:项目的描述。 7. 仓库配置 ``` <repositories> <repository> <id>central</id> <url>http://central.maven.org/maven2/</url> </repository> </repositories> ``` - repositories:仓库列表。 - repository:仓库。 - id:仓库的唯一标识符。 - url:仓库的URL。 8. 插件仓库配置 ``` <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central.maven.org/maven2/</url> </pluginRepository> </pluginRepositories> ``` - pluginRepositories:插件仓库列表。 - pluginRepository:插件仓库。 - id:插件仓库的唯一标识符。 - url:插件仓库的URL。 9. 构建配置 ``` <build> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> ``` - build:构建配置。 - sourceDirectory:源代码目录。 - testSourceDirectory:测试代码目录。 - resources:资源目录列表。 - resource:资源目录。 - directory:资源目录的路径。 - testResources:测试资源目录列表。 - testResource:测试资源目录。 - plugins:插件列表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值