maven学习(2)

来源:互联网 发布:斗鱼卡卡淘宝店 编辑:程序博客网 时间:2024/06/02 03:18

1.依赖的配置

先来看一个pom.xml

[html] view plaincopy
  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.    
  4.   <modelVersion>4.0.0</modelVersion>  
  5.   <groupId>com.deppon.demo</groupId>  
  6.   <artifactId>test01</artifactId>  
  7.   <packaging>war</packaging>  
  8.   <version>0.0.1-SNAPSHOT</version>  
  9.   <name>test01 Maven Webapp</name>  
  10.   <url>http://maven.apache.org</url>  
  11.     
  12.   <!-- 属性配置 -->  
  13.   <properties>  
  14.       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   </properties>  
  16.   
  17.   <!-- 依赖配置 -->       
  18.   <dependencies>  
  19.     <!-- 添加JUnit -->  
  20.     <dependency>  
  21.       <groupId>junit</groupId>  
  22.       <artifactId>junit</artifactId>  
  23.       <version>3.8.1</version>  
  24.       <scope>test</scope>  
  25.     </dependency>  
  26.       
  27.     <!-- 添加Servlet -->  
  28.     <dependency>    
  29.         <groupId>javax.servlet</groupId>    
  30.         <artifactId>servlet-api</artifactId>    
  31.         <version>2.5</version>    
  32.         <scope>provided</scope>    
  33.     </dependency>    
  34.       
  35.   </dependencies>  
  36.     
  37.   <build>  
  38.     <finalName>test01</finalName>  
  39.   </build>  
  40.     
  41. </project>  

根元素project下的dependencies可以包含一个或者多个dependency元素,以声明一个或者多个项目依赖

每个依赖包含的元素有:

groupId,artifactId,version:依赖的基本坐标

type:依赖的类型。大部分情况下,该元素不必声明,默认是jar

scope:依赖的范围

optional:标记依赖是否可选

exclusions:用来排除传递性依赖


2.scope (依赖范围)

在Maven的世界中,有很多种classpath,编译classpath,测试classpath,运行classpath

依赖范围就是用来控制依赖与这三种classpath的关系

compile:编译依赖范围。如果没有指定,则默认使用该依赖范围。使用此范围的依赖,在编译,测试,运行着三种classpath都有效。

test:测试依赖范围。使用此范围的依赖,只对测试classpath有效,即只有在测试的代码中才可用。典型例子就是Junit

provided:已提供依赖范围。使用此范围的依赖对于编译和测试都有效,但在运行时无效。典型例子就是servlet-api

runtime:运行时依赖范围。使用此范围的依赖对于测试和运行classpath有效,但在编译主代码是无效。典型例子就是JDBC驱动实现

system:系统依赖范围。对于编译和测试有效,但在运行时无效。使用system范围时,必须通过systemPath元素显示的指定依赖文件的路径(慎用)

import:导入依赖范围

0 0
原创粉丝点击