Maven文件配置解析和常见内容

我们先看一个简单的例子:


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <!-- maven model version -->  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <!-- project group id & artifact id -->  
  6.     <groupId>com.freesoft.mvn-webapp</groupId>  
  7.     <artifactId>mvnwebapp</artifactId>  
  8.     <!-- packing type -->  
  9.     <packaging>war</packaging>  
  10.     <!-- version -->  
  11.     <version>1.0-SNAPSHOT</version>  
  12.     <name>mvnwebapp Maven Webapp</name>  
  13.     <url>http://maven.apache.org</url>  
  14.   
  15.   
  16.     <dependencies>  
  17.   
  18.         <!-- JUnit -->  
  19.         <dependency>  
  20.             <groupId>junit</groupId>  
  21.             <artifactId>junit</artifactId>  
  22.             <version>4.11</version>  
  23.             <scope>test</scope>  
  24.         </dependency>  
  25.   
  26.     </dependencies>  
  27.   
  28.   
  29.     <build>  
  30.         <finalName>mvnwebapp</finalName>  
  31.         <pluginManagement>  
  32.             <plugins>  
  33.                 <plugin>  
  34.                     <groupId>org.apache.tomcat.maven</groupId>  
  35.                     <artifactId>tomcat7-maven-plugin</artifactId>  
  36.                     <version>2.1</version>  
  37.                     <configuration>  
  38.                         <tomcat-url>http://localhost:8080/manager/html</tomcat-url>  
  39.                         <server>tomcat_localtest</server>  
  40.                     </configuration>  
  41.                 </plugin>  
  42.             </plugins>  
  43.         </pluginManagement>  
  44.     </build>  
  45.   
  46.     <properties>  
  47.         <struts.version>2.3.15</struts.version>  
  48.         <mysql.version>5.1.29</mysql.version>  
  49.         <hibernate.version>4.3.1.Final</hibernate.version>  
  50.     </properties>  
  51. </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中。
exclusions 排除项目中的依赖冲突时使用。

2.1 关于排除依赖冲突

我们可能经常会遇到这样一个问题:我们的项目有两个依赖项:A & B,而且A和B同时依赖了C,但不是同一个版本。那么我们怎么办呢?


2.1.1 添加检查插件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <reporting>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <groupId>org.apache.maven.plugins</groupId>  
  5.             <artifactId>maven-project-info-reports-plugin</artifactId>  
  6.         </plugin>  
  7.     </plugins>  
  8. </reporting>  

然后运行:mvn project-info-reports:dependencies,来查看依赖项报告。


2.1.2 去除依赖项

如果我们需要在某个dependency中去除某个依赖项,直接这样即可:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!-- Struts2 -->  
  2. <dependency>  
  3.     <groupId>org.apache.struts</groupId>  
  4.     <artifactId>struts2-core</artifactId>  
  5.     <version>${struts.version}</version>  
  6.     <exclusions>  
  7.         <exclusion>  
  8.             <groupId>org.freemarker</groupId>  
  9.             <artifactId>freemarker</artifactId>  
  10.         </exclusion>  
  11.     </exclusions>  
  12. </dependency>  

3. 继承

我的repository下面有个例子就直接拿来用了:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <modelVersion>4.0.0</modelVersion>  
  2. <parent>  
  3.   <groupId>com.thoughtworks.xstream</groupId>  
  4.   <artifactId>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值