(六)Maven之pom.xml文件简单说明

通过前面几部分知识,我们对maven已经有了初步的印象,就像Make的Makefile、Ant的build.xml一样,Maven项目的核心是pom.xml。POM(Project Object Model,项目对象模型)定义了项目的基本信息,用于描述项目如何构建,声明依赖,等等。我们来看看maven中pom.xml文件主要标签的意思及其用法,来看一下pom.xml文件的结构:

 

  1 <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">
  2     <modelVersion>4.0.0</modelVersion>
  3 
  4     <groupId>com.uidp</groupId>
  5     <artifactId>UidpParent</artifactId>
  6     <version>0.0.1-SNAPSHOT</version>
  7     <packaging>pom</packaging>
  8 
  9     <!--当前项目的全称(名称)-->
 10     <name>UidpParent</name>
 11     <!--当前项目的主页的URL-->
 12     <url>http://maven.apache.org</url>
 13 
 14     <!--配置常用属性或定义一系列版本号-->
 15     <properties>
 16         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 17 
 18         <repository-url>http://192.168.0.70:8081/content/groups/public/</repository-url>
 19 
 20         <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
 21         <maven-war-plugin.version>2.4</maven-war-plugin.version>
 22         <maven-javadoc-plugin.version>2.9.1</maven-javadoc-plugin.version>
 23         <maven-release-plugin.version>2.4.1</maven-release-plugin.version>
 24         <maven-deploy-plugin.version>2.7</maven-deploy-plugin.version>
 25 
 26 
 27 
 28         <junit.version>4.11</junit.version>
 29         <oracle.version>10.2.0.4</oracle.version>
 30         <springframework.version>3.2.8.RELEASE</springframework.version>
 31         <mybatis.version>3.2.2</mybatis.version>
 32         <mybatis-spring.version>1.2.0</mybatis-spring.version>
 33         <mysql-driver.version>5.1.25</mysql-driver.version>
 34         <aspectjweaver.version>1.7.3</aspectjweaver.version>
 35 
 36         <commons-dbcp.version>1.4</commons-dbcp.version>
 37         <commons-pool.version>1.5.5</commons-pool.version>
 38         <commons-fileupload.version>1.2.2</commons-fileupload.version>
 39 
 40         <log4j.version>1.2.17</log4j.version>
 41         <slf4j-api.version>1.7.5</slf4j-api.version>
 42         <slf4j-log4j12.version>1.7.5</slf4j-log4j12.version>
 43 
 44         <freemarker.version>2.3.19</freemarker.version>
 45 
 46         <jackson-core.version>2.5.0</jackson-core.version>
 47         <jackson-mapper-asl.version>1.9.7</jackson-mapper-asl.version>
 48 
 49         <javax.servlet-api.version>3.0.1</javax.servlet-api.version>
 50         <jsp-api.version>2.2</jsp-api.version>
 51         <kryo.version>1.04</kryo.version>
 52         <snakeyaml.version>1.8</snakeyaml.version>
 53         <jedis.version>2.0.0</jedis.version>
 54         <commons-lang.version>2.6</commons-lang.version>
 55 
 56 
 57         <mockito-core.version>1.8.5</mockito-core.version>
 58         <powermock-core.version>1.4.9</powermock-core.version>
 59         <powermock-api-mockito.version>1.4.9</powermock-api-mockito.version>
 60         <powermock-module-junit4.version>1.4.9</powermock-module-junit4.version>
 61 
 62 
 63     </properties>
 64 
 65     <!--依赖管理-->
 66     <dependencyManagement>
 67         <dependencies>
 68 
 69             <dependency>
 70                 <groupId>junit</groupId>
 71                 <artifactId>junit</artifactId>
 72                 <version>${junit.version}</version>
 73                 <scope>test</scope>
 74             </dependency>
 75 
 76             <!-- spring jar begin -->
 77             <dependency>
 78                 <groupId>org.springframework</groupId>
 79                 <artifactId>spring-web</artifactId>
 80                 <version>${springframework.version}</version>
 81             </dependency>
 82 
 83             <dependency>
 84                 <groupId>org.springframework</groupId>
 85                 <artifactId>spring-webmvc</artifactId>
 86                 <version>${springframework.version}</version>
 87             </dependency>
 88 
 89             <dependency>
 90                 <groupId>org.springframework</groupId>
 91                 <artifactId>spring-beans</artifactId>
 92                 <version>${springframework.version}</version>
 93             </dependency>
 94 
 95             <dependency>
 96                 <groupId>org.springframework</groupId>
 97                 <artifactId>spring-context</artifactId>
 98                 <version>${springframework.version}</version>
 99             </dependency>
100 
101             <dependency>
102                 <groupId>org.springframework</groupId>
103                 <artifactId>spring-context-support</artifactId>
104                 <version>${springframework.version}</version>
105             </dependency>
106 
107             <dependency>
108                 <groupId>org.springframework</groupId>
109                 <artifactId>spring-core</artifactId>
110                 <version>${springframework.version}</version>
111             </dependency>
112 
113             <dependency>
114                 <groupId>org.springframework</groupId>
115                 <artifactId>spring-jdbc</artifactId>
116                 <version>${springframework.version}</version>
117             </dependency>
118 
119             <dependency>
120                 <groupId>org.springframework</groupId>
121                 <artifactId>spring-tx</artifactId>
122                 <version>${springframework.version}</version>
123             </dependency>
124 
125             <dependency>
126                 <groupId>org.springframework</groupId>
127                 <artifactId>spring-test</artifactId>
128                 <version>${springframework.version}</version>
129             </dependency>
130 
131             <dependency>
132                 <groupId>org.springframework</groupId>
133                 <artifactId>spring-expression</artifactId>
134                 <version>${springframework.version}</version>
135             </dependency>
136 
137             <dependency>
138                 <groupId>org.springframework</groupId>
139                 <artifactId>spring-aop</artifactId>
140                 <version>${springframework.version}</version>
141             </dependency>
142             <!-- spring jar end -->
143 
144             <dependency>
145                 <groupId>org.mybatis</groupId>
146                 <artifactId>mybatis</artifactId>
147                 <version>${mybatis.version}</version>
148             </dependency>
149 
150             <dependency>
151                 <groupId>org.mybatis</groupId>
152                 <artifactId>mybatis-spring</artifactId>
153                 <version>${mybatis-spring.version}</version>
154             </dependency>
155 
156             <dependency>
157                 <groupId>mysql</groupId>
158                 <artifactId>mysql-connector-java</artifactId>
159                 <version>${mysql-driver.version}</version>
160             </dependency>
161 
162             <dependency>
163                 <groupId>com.oracle</groupId>
164                 <artifactId>ojdbc14</artifactId>
165                 <version>${oracle.version}</version>
166             </dependency>
167 
168             <dependency>
169                 <groupId>org.aspectj</groupId>
170                 <artifactId>aspectjweaver</artifactId>
171                 <version>${aspectjweaver.version}</version>
172             </dependency>
173 
174 
175             <dependency>
176                 <groupId>commons-dbcp</groupId>
177                 <artifactId>commons-dbcp</artifactId>
178                 <version>${commons-dbcp.version}</version>
179             </dependency>
180             <dependency>
181                 <groupId>commons-pool</groupId>
182                 <artifactId>commons-pool</artifactId>
183                 <version>${commons-pool.version}</version>
184             </dependency>
185             <dependency>
186                 <groupId>commons-fileupload</groupId>
187                 <artifactId>commons-fileupload</artifactId>
188                 <version>${commons-fileupload.version}</version>
189             </dependency>
190 
191 
192             <!-- log jar -->
193             <dependency>
194                 <groupId>log4j</groupId>
195                 <artifactId>log4j</artifactId>
196                 <version>${log4j.version}</version>
197             </dependency>
198             <dependency>
199                 <groupId>org.slf4j</groupId>
200                 <artifactId>slf4j-api</artifactId>
201                 <version>${slf4j-api.version}</version>
202             </dependency>
203             <dependency>
204                 <groupId>org.slf4j</groupId>
205                 <artifactId>slf4j-log4j12</artifactId>
206                 <version>${slf4j-log4j12.version}</version>
207             </dependency>
208 
209             <!-- freemarker -->
210             <dependency>
211                 <groupId>org.freemarker</groupId>
212                 <artifactId>freemarker</artifactId>
213                 <version>${freemarker.version}</version>
214             </dependency>
215 
216 
217             <!-- jackson -->
218             <dependency>
219                 <groupId>com.fasterxml.jackson.core</groupId>
220                 <artifactId>jackson-core</artifactId>
221                 <version>${jackson-core.version}</version>
222             </dependency>
223             <dependency>
224                 <groupId>org.codehaus.jackson</groupId>
225                 <artifactId>jackson-mapper-asl</artifactId>
226                 <version>${jackson-mapper-asl.version}</version>
227             </dependency>
228 
229             <dependency>
230                 <groupId>javax.servlet</groupId>
231                 <artifactId>javax.servlet-api</artifactId>
232                 <version>${javax.servlet-api.version}</version>
233                 <scope>provided</scope>
234             </dependency>
235 
236             <dependency>
237                 <groupId>javax.servlet.jsp</groupId>
238                 <artifactId>jsp-api</artifactId>
239                 <version>${jsp-api.version}</version>
240                 <scope>provided</scope>
241             </dependency>
242 
243             <dependency>
244                 <groupId>com.googlecode</groupId>
245                 <artifactId>kryo</artifactId>
246                 <version>${kryo.version}</version>
247             </dependency>
248 
249             <dependency>
250                 <groupId>org.yaml</groupId>
251                 <artifactId>snakeyaml</artifactId>
252                 <version>${snakeyaml.version}</version>
253             </dependency>
254 
255             <dependency>
256                 <groupId>redis.clients</groupId>
257                 <artifactId>jedis</artifactId>
258                 <version>${jedis.version}</version>
259             </dependency>
260 
261             <dependency>
262                 <groupId>commons-lang</groupId>
263                 <artifactId>commons-lang</artifactId>
264                 <version>${commons-lang.version}</version>
265             </dependency>
266 
267 
268             <dependency>
269                 <groupId>org.mockito</groupId>
270                 <artifactId>mockito-core</artifactId>
271                 <version>${mockito-core.version}</version>
272                 <scope>test</scope>
273             </dependency>
274 
275             <dependency>
276                 <groupId>org.powermock</groupId>
277                 <artifactId>powermock-core</artifactId>
278                 <version>${powermock-core.version}</version>
279                 <scope>test</scope>
280             </dependency>
281 
282             <dependency>
283                 <groupId>org.powermock</groupId>
284                 <artifactId>powermock-api-mockito</artifactId>
285                 <version>${powermock-api-mockito.version}</version>
286                 <scope>test</scope>
287             </dependency>
288 
289             <dependency>
290                 <groupId>org.powermock</groupId>
291                 <artifactId>powermock-module-junit4</artifactId>
292                 <version>${powermock-module-junit4.version}</version>
293                 <scope>test</scope>
294             </dependency>
295 
296 
297         </dependencies>
298     </dependencyManagement>
299     
300     <distributionManagement>
301         <repository>
302             <id>releases</id>
303             <name>public</name>
304             <url>http://59.50.95.66:8081/nexus/content/repositories/releases</url>
305         </repository>
306         <snapshotRepository>
307             <id>snapshots</id>
308             <name>Snapshots</name>
309             <url>http://59.50.95.66:8081/nexus/content/repositories/snapshots</url>
310         </snapshotRepository>
311     </distributionManagement>
312     
313     
314     <!--构建管理-->
315     <build>
316         <plugins>
317 
318             <plugin>
319                 <groupId>org.apache.maven.plugins</groupId>
320                 <artifactId>maven-compiler-plugin</artifactId>
321                 <version>${maven-compiler-plugin.version}</version>
322                 <configuration>
323                     <source>1.7</source> <!-- 源代码使用的开发版本 -->
324                     <target>1.7</target> <!-- 需要生成的目标class文件的编译版本 -->
325                 </configuration>
326             </plugin>
327 
328             <plugin>
329                 <groupId>org.apache.maven.plugins</groupId>
330                 <artifactId>maven-javadoc-plugin</artifactId>
331                 <version>${maven-javadoc-plugin.version}</version>
332             </plugin>
333 
334 
335             <plugin>
336                 <groupId>org.apache.maven.plugins</groupId>
337                 <artifactId>maven-release-plugin</artifactId>
338                 <version>${maven-release-plugin.version}</version>
339             </plugin>
340 
341             <plugin>
342                 <groupId>org.apache.maven.plugins</groupId>
343                 <artifactId>maven-deploy-plugin</artifactId>
344                 <version>${maven-deploy-plugin.version}</version>
345                 <configuration>
346                     <updateReleaseInfo>true</updateReleaseInfo>
347                 </configuration>
348             </plugin>
349 
350         </plugins>
351     </build>
352 
353 
354     <pluginRepositories>
355         <pluginRepository>
356             <id>nexus</id>
357             <name>nexus</name>
358             <url>${repository-url}</url>
359             <releases>
360                 <enabled>true</enabled>
361             </releases>
362             <snapshots>
363                 <enabled>true</enabled>
364             </snapshots>
365         </pluginRepository>
366     </pluginRepositories>
367 
368     
369 </project>

 

我们可以看到maven的pom.xml文件结构非常清晰,把项目创建好后,我们基本上是在dependencies元素下添加一些子元素及plugins元素下添加一些插件,下面我们来介绍一下各个元素的含义。

1) project是所有pom.xml的根元素,并且在里面定义了命名空间和xsd元素。

2) modelVersion 当前pom模型的版本。

3) groupId定义当前maven项目隶属的实际项目,并会根据这给项目建立包结构。

4) artifactId定义项目中的某个模块名称,如果只有一个模块那就是项目的名称。

5) version 定义maven项目当前所处的版本号,默认0.0.1-SNAPSHOT为快照版本。

6) packaging定义maven项目的打包方式,可以是jar包、war包、pom。

7) dependencies元素底下就是加入依赖包的地方,那么我们从哪里查询依赖包呢,可以查询的地方比较多,我给出一个大家用得比较多的仓库:http://mvnrepository.com

8) 每个dependency都是一个依赖包,依赖包也就是在dependency里面定义各个依赖包的坐标,这样maven就会从网上下载依赖包到你本地仓库中,有所不同的是dependency元素包含了一个子元素,这个就是对maven生命周期的一个说明,当然除了上面四个子元素外,还包含几个其他的元素。

   (1)type说明依赖的类型

   (2)optional标记依赖是否可选

   (3)exclusions 用来排斥传递依赖

   我们具体来看看这个结构:

   <dependency>

     <groupId>实际项目</groupId>

     <artifactId>模块</artifactId>

     <version>版本</version>

     <type>依赖类型</type>

     <scope>依赖范围</scope>

     <optional>依赖是否可选</optional>

     <!—主要用于排除传递性依赖-->

     <exclusions>

       <exclusion>

         <groupId>…</groupId>

         <artifactId>…</artifactId>

       </exclusion>

     </exclusions>

   </dependency>

 

Maven是通过groupId、artifactId、version这三类似于坐标的元素来确定唯一性的,因此这三个元素对于每个依赖大多是必须的,后面会详细介绍依赖、聚合、继承等知识点。

 

没有任何实际的Java代码,我们能够定义一个Maven项目的POM,这体现了Maven的一大优点,它能让项目对象模型最大程度地与实际代码相独立,我们可以称之为解耦。这在很大程度上避免了Java代码和POM代码的相互影响。只要我们定义的POM稳定后,日常的Java代码开发工作中基本不会涉及到POM的修改。

 

可爱博主:AlanLee

博客地址:http://www.cnblogs.com/AlanLee

本文出自博客园,欢迎大家加入博客园。

 

posted @ 2016-12-07 09:45  AlanLee-Java  阅读(3261)  评论(0编辑  收藏  举报