POM 项目对象模型及 POM.XML文件结构解析

2 篇文章 0 订阅

POM

POM 全称是 Project Object Model ,即项目对象模型。 pom.xml 是 maven 的项目描述文件。 pom.xml 文件以 xml 的形式描述项目的信息,包括项目名称、版本、项目 id、项目的依赖关系、编译环境、持续集成、项目团队、贡献管理、生成报表等等。

基本配置

<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>org.seandeng</groupId>

<artifactId>myapp</artifactId>

<version>1.0</version>

</project>

modelVersion 描述这个 POM 文件是遵从哪个版本的项目描述符,目前只能是 4.0.0。

groupId 针对一个项目的普遍唯一识别符。通常用一个完全正确的包的名字来与其他项目的类似名字来进行区分(比如: org.apache.maven) 。

artifactId 在给定 groupID 的 group 里面为 artifact 指定的标识符是唯一的 , artifact代表的是被制作或者被一个 project 应用的组件 ( 产出物 ) 。

version 当前项目产生的 artifact 的版本

以上 4 个元素缺一不可,其中 groupId, artifactId, version 描述依赖的项目唯一标志。

文件结构

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>...</groupId>

<artifactId>...</artifactId>

<version>...</version>

<packaging>...</packaging>  // 打包机制,如 pom,jar,maven-plugin,ejb,war,ear,rar,par

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

POM 很重要的 3 个关系

POM 有 3 个很重要的关系:依赖、继承、合成。

依赖关系

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.0</version>

<type>jar</type>

<scope>test</scope>

<optional>true</optional>

</dependency>

...

</dependencies>

如果想依赖一个 maven 库中没有的一个 jar 包,方法很简单,就是先将此 jar 包使用以下的命令安装到本地 maven 库中:

mvn install:install-file -Dfile=my.jar -DgroupId=mygroup -DartifactId=myartifactId -Dversion=1

再把依赖关系加进去即可。

继承关系

父项目配置

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>com.seandeng</groupId>

<artifactId>my-parent</artifactId>

<version>2.0</version>

<packaging>pom</packaging>

</project>

packaging 类型,定义值为 pom 用于定义为 parent 和合成多个项目。 当然我们创建的 maven 项目的 pom 都继承 maven 的 super pom , 如果想看项目 ( 父或子 ) 的完全的 pom 结构,可以运行:

mvn help:effective-pom

就可以了。

子项目配置

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>com.seandeng</groupId>

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

<parent>

<groupId>com.seandeng</groupId>

<artifactId>my-parent</artifactId>

<version>2.0</version>

<relativePath>../my-parent</relativePath>

</parent>

</project>

relativePath 可以不需要,但是用于指明 parent 的目录,用于快速查询。

合成关系

一个项目有多个模块。 如下的定义:

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>com.seandeng</groupId>

<artifactId>my-parent</artifactId>

<version>2.0</version>

<modules>

<module>my-child-project1<module>

<module>my-child-project2<module>

</modules>

</project>

其中 module 描述的是子项目的相对路径 。

dependencies

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.0</version>

<type>jar</type>

<scope>test</scope>

<optional>true</optional>

</dependency>

scope: compile (编译范围) , provided (已提供范围) , runtime (运行时范围) , test (测试范围) , system (系统范围)

exclusions : 如果 X 需要 A,A 包含 B 依赖,那么 X 可以声明不要 B 依赖,只要在exclusions 中声明 exclusion.

Properties

POM 片段:

<properties>

<hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>

</properties>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-annotations</artifactId>

<version>${hibernate.annotations.version}</version>

</dependency>

特殊的属性引用片段:

<parent> 

… …

<version>1.0-SNAPSHOT</version> 

</parent>

<dependencies> 

<dependency> 

… …

<version>${project.version}</version>// 这个值是一个属性引用,指向了 POM 的project/version 的值 。

</dependency>

</dependencies>

dependencyManagement

Maven 还我们提供了一个 dependencyManagement 元素,用来提供了一种方式来统一依赖版本号。 dependencyManagement 元素一 般用在顶层的父 POM 。使用pom.xml 中的 dependencyManagement 元素能让你在子项目中引用一个依赖而不用显式的列出版本号。 Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用在这个dependencyManagement 元素中指定的版本号,这样就解决了修改依赖版本号不完全的问题。示例如下:

顶层的父 POM 片段:

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring</artifactId>

<version>2.0.7</version>

</dependency>

</dependencies>

</dependencyManagement>

子项目 POM 片段:

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring</artifactId>

</dependency>

</dependencies>

Repositories

Repositories :个性化的指定仓库地址。

<repositories> 

<repository> 

<releases> 

<enabled>false</enabled> 

<updatePolicy>always</updatePolicy> 

<checksumPolicy>warn</checksumPolicy> 

</releases> 

<snapshots> 

<enabled>true</enabled> 

<updatePolicy>never</updatePolicy> 

<checksumPolicy>fail</checksumPolicy> 

</snapshots> 

<id>codehausSnapshots</id> 

<name>Codehaus Snapshots</name> 

<url>http://snapshots.maven.codehaus.org/maven2</url> 

<layout>default</layout> 

</repository> 

</repositories> 

release 和 snapshots :是 artifact 的两种 policies , pom 可以选择那种政策有效。

updatePolicy:

说明更新发生的频率   always   或者   never   或者   daily   (默认的)或者   interval:X     X   是分钟数)    

checksumPolicy :当 Maven 的部署文件到仓库中,它也部署了相应的校验和文件。您可以选择忽略,失败,或缺少或不正确的校验和警告。

layout : maven1.x 与 maven2 有不同的 layout ,所以可以声明为 default 或者是legacy (遗留方式 maven1.x )。

Profile

Maven 的 Profile 元素可以为一个特殊的环境自定义一个特殊的构建,使得不同环境间构建的可移植性成为可能。比如要使用 production profile 来运行 mvn install ,你需要在命令行传入 -Pproduction 参数,这里 production 是 profile 的 id 。要验证production profile 覆盖了默认的 Compiler 插件配置,可以像这样以开启调试输入 (-X)的方式运行 Maven 。

它包含可选的 activation ( profile 的触发器)和一系列的 changes 。例如 test 过程可能会指向不同的数据库(相对最终的 deployment )或者不同的 dependencies 或者不同的 repositories ,并且是根据不同的 JDK 来改变的。那么结构如下:  

<profiles> 

<profile> 

<id>test</id> 

<activation>...</activation> 

<build>...</build> 

<modules>...</modules> 

<repositories>...</repositories> 

<pluginRepositories>...</pluginRepositories> 

<dependencies>...</dependencies> 

<reporting>...</reporting> 

<dependencyManagement>...</dependencyManagement> 

<distributionManagement>...</distributionManagement> 

</profile> 

</profiles> 

Activation :   触发这个 profile 的条件配置如下例:(只需要其中一个成立就可以激活 profile ,如果第一个条件满足了,那么后面就不会在进行匹配。  

<profile> 

<id>test</id> 

<activation> 

<activeByDefault>false</activeByDefault> 

<jdk>1.5</jdk> 

<os> 

<name>Windows XP</name> 

<family>Windows</family> 

<arch>x86</arch> 

<version>5.1.2600</version> 

</os> 

<file> 

<exists>${basedir}/file2.properties</exists> 

<missing>${basedir}/file1.properties</missing> 

</file> 

</activation> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值