Maven(2)pom.xml常用元素和依赖范围、传递、冲突
pom.xml常用元素
<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"> <!-- 指定当前pom的版本 --> <modelVersion>4.0.0</modelVersion> <!-- Maven的项目和实际项目不是一一对应的关系, Maven的项目体现的是一个模块化得概念 一个实际项目往往会被划分成很多个模块--> <!-- 反写的公司网址+项目名 --> <groupId>com.daoan.hi</groupId> <!-- 项目名+模块名 --> <artifactId>hi</artifactId> <!-- 第一个0表示大版本号 第二个0表示分支版本号 第三个0表示小版本号 --> <version>0.0.1-SNAPSHOT</version> <!-- 默认是jar包 war zip pom --> <packaging>jar</packaging> <!-- 项目描述名 --> <name>hi</name> <!-- 项目地址 --> <url>http://maven.apache.org</url> <!-- 项目描述 --> <description></description> <!-- 开发人员列表 --> <developers></developers> <!-- 许可证信息 --> <licenses></licenses> <!-- 组织信息 --> <organization></organization> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 依赖列表 下面可以包含多个依赖项目 --> <dependencies> <!-- 依赖项 --> <dependency> <!-- 确定依赖所在位置 --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <!-- 依赖范围 jar只在测试的依赖范围有用 --> <scope>test</scope> <!-- 设置依赖是否可选true false(默认) --> <optional></optional> <!-- 排除依赖传递列表 --> <exclusions> <!-- A依赖B,B依赖C,A和C是传递依赖,如果不需要该依赖! --> <exclusion></exclusion> </exclusions> </dependency> </dependencies> <!-- 依赖的管理 --> <dependencyManagement> <dependencies> <dependency> </dependency> </dependencies> </dependencyManagement> <build> <!-- 插件列表 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <!-- 通常用于在子模块中对父模块pom的一个继承 --> <parent></parent> <!-- 聚合运行多个的maven项 --> <modules> <module></module> </modules> </project>
依赖:
3种classpath范围 测试、编译、运行
依赖范围scope:
compile 编译依赖范围,默认对3种classpath都有效
test 测试依赖范围,仅对测试classpath有效
provided 已提供依赖范围,编译和测试classpath有效,运行时无效servlet-api,运行时由容器提供
依赖性传递 ( 陈浩南鸡哥案例 )
排除依赖
<dependencies> <!-- 依赖项 --> <dependency> <groupId>com.hongxing</groupId> <artifactId>hongxing-nange</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 排除依赖 --> <exclusions> <exclusion> <groupId>com.hongxing</groupId> <artifactId>hongxing-bge</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
依赖冲突
1.短路优先
A--> B --> C --> X (jar)
A--> D--> X(jar)
2.路径相同,先声明先优先
聚合和继承
聚合是指在maven中如果想将多个项目进行install,将其安装到本地仓库中!
在hongxing-aggreation的pom.xml文件写,将<packaging>pom</packaging>
<modules> <module>../hongxing-bge</module> <module>../hongxing-nange</module> <module>../hongxing-shanji</module> </modules>
install就可以同时将多个项目安装pom包到本地仓库
继承: 继承是指将各项目共同个性封装成一个父类,父类不需要src/main ,src/test目录
<packaging>pom</packaging>
hongxing-parent的pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>3.8.1</junit.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement>
hongxing-bge pom.xml继承父类
<!-- 通常用于在子模块中对父模块pom的一个继承 -->
<parent> <groupId>com.hongxing</groupId> <artifactId>hongxing-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<!-- 依赖列表 下面可以包含多个依赖项目 -->
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>