Maven入门-3.pom文件

1、pom.xml文件介绍

Maven项目的核心是pom.xml,pom(Project  Object Model项目对象模型)

pom.xml文件定义了项目的基本信息,项目构建,项目依赖等。

1
2
3
4
5
6
7
8
9
     < modelVersion >4.0.0</ modelVersion >
     < groupId >com.fz.shiro</ groupId >
     < artifactId >ShiroTest</ artifactId >
     < packaging >war</ packaging >
     < version >0.0.1-SNAPSHOT</ version >
     < name >ShiroTest Maven Webapp</ name >
</ project >

porject:是所有元素的根元素

以下元素(groupId,artifactId,packaging,version)是maven的坐标,他们唯一标识了一个项目

modelVersion:指定了当前模型的版本,对于Maven2和Maven3来说,它只能说4.0.0

groupId:定义了项目属于哪个组,假如你在github上建立一个demo项目,那groupId应该com.github.demo

                如果你的公司是百度,有个helloword的项目。那groupId应该为com.baidu.helloword

                如果你的helloword项目有很多模块,则按模块化分。com.baidu.helloword.模块名称

artifactId:定义了当前maven项目在组中的唯一ID,假如你的项目为myapp,groupId为com.baid.myapp。那你的artifactId可以按模块划分

                例如当前编写的是myapp项目中的工具类,则artifactId可以为myapp-utils

packaging:表示打包后项目的类型(默认是jar),web项目为war

version:指定了当前项目的版本,SNAPSHOT意为快照,表示正在开发中,不是稳定版本。

name:声明了一个对于用户更为友好的项目名称

url:


2.添加依赖

依赖需要在project元素中添加dependencies元素,通过在dependencies元素中添加一个或多个dependency元素来配置依赖关系。

例如:为项目添加junit测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
     < modelVersion >4.0.0</ modelVersion >
     < groupId >com.fz</ groupId >
     < artifactId >ShiroTest</ artifactId >
     < packaging >war</ packaging >
     < version >0.0.1-SNAPSHOT</ version >
     < name >ShiroTest Maven Webapp</ name >
     < url >http://maven.apache.org</ url >
 
     < dependencies >
         < dependency >
             < groupId >junit</ groupId >
             < artifactId >junit</ artifactId >
             < version >4.9</ version >
             < scope >test</ scope >
         </ dependency >    
     </ dependencies >
     
</ project >

dependencies元素中可以包含多个dependency元素,dependency元素声明了项目的依赖。

以上的配置能够让maven自动下载junit-4.9.jar文件

dependency中有个scope元素,该元素表示依赖范围。如果scope的范围是test,表示该依赖(junit)只对测试有效,如果在其他主代码中使用的话。

就会编译错误不声明依赖范围,默认是compile










转载于:https://my.oschina.net/u/1045699/blog/786588

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值