1、pom.xml文件介绍
Maven项目的核心是pom.xml,pom(Project Object Model项目对象模型)
pom.xml文件定义了项目的基本信息,项目构建,项目依赖等。
1
2
3
4
5
6
7
8
9
|
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
<
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
|
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
<
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
>
<
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