pom.xml里面各个配置的含义以下:apache
<!-- 主项目标识,表示当前maven属于哪一个实际项目,与包是同样的 --> <groupId>反写的公司网址+项目名</groupId> <!-- 模块标识--> <artifactId>项目名+模块名</artifactId> <!-- 版本号,通常由三个数字组成 第一个0,表示大版本号, 第二个0表示分支版本号, 第三个0表示小版本号, snapshot 快照 beta 公测 alpha 内测 Release 稳定 GA正式发布 --> <version></version> <!-- 打包的方式,默认为jar,还能够打包成war,zip,pom --> <packaging></packaging> <!-- 项目描述名 --> <name></name> <!-- 项目的地址 --> <url></url> <!-- 项目的描述 --> <description></description> <!-- 开发人员信息 --> <developers></developers> <!-- 许可证信息 --> <licenses></licenses> <!-- 组织信息 --> <organization></organization> <!-- 依赖列表--> <dependencies> <!-- 依赖项 --> <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> <type></type> <!-- 依赖的范围 --> <scope></scope> <!-- 设置依赖是否可选,默认false。 --> <optional></optional> <!-- 排除依赖传递列表 --> <exclusions> <exclusion> </exclusion> </exclusions> </dependency> </dependencies> <!-- 依赖的管理 --> <dependencyManagement> <dependencies> <dependency></dependency> </dependencies> </dependencyManagement> <build> <!-- 插件列表 --> <plugins> <plugin> <groupId></groupId> <artifactId></artifactId> <version></version> </plugin> </plugins> </build> <!-- 用于子模块对父模块pom的继承 --> <parent></parent> <!-- 用来聚合多个maven项 --> <modules> <module></module> </modules>
例如上一节建立的以下:maven
<groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mavenDemo01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
三种classpath:ide
scop选项:测试
这里创建三个maven项目演示ui
demo02要依赖demo01,要想依赖,必须在本地仓库安装demo01的项目,url
首先对demo01进行以下操做:spa
1,右键demo01,使用maven方式运行,将其打包:插件
2,将mavendemo01安装到本地仓库中,一样执行maven方式运行,执行install命令。code
在demo02中加入demo01的依赖:xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo01</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
以下:
一样方式将demo02安装,为了省去操做,同时执行两个命令:clean install
在demo03中依赖demo02项目。
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo02</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
一样对demo03安装到本地仓库。
观察mavenDemo03的依赖包发现,demo03原本只想依赖demo02,可是连demo01也依赖了,
这代表依赖具备传递性。
要想不依赖demo01,使用exclusions,能够排除demo01的依赖。
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo02</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion><!--下面填入mavenDemo01的坐标--> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo01</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
保存后发现只剩demo02的依赖了。
解决依赖冲突的两条原则:
1,短路优先:A->B-C->X(jar)
A->D->X(jar)
因为第二条路比较短,会依赖第二条的方式。
2,先声明先优先:
若是路径长度相同,则谁先声明,先解析谁。
若是想要将多个项目进行install,安装到本地仓库中,必须对其依次进行install命令。
而聚合能够将多个项目放到一块儿运行,同时安装。
例如:将前面的三个项目聚合,一块儿安装。
新建一个mavenDemo04,在pom.xml里面配置以下:
首先把packaging改为pom
<groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo04</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>
而后添加聚合标签:分别加入要安装的三个项目
<modules> <module> ../mavenDemo01 </module> <module> ../mavenDemo02 </module> <module> ../mavenDemo03 </module> </modules>
而这里的dependency无所谓了,能够删除。
而后运行maven命令,执行 clean install命令。
此时就同时安装了三个项目到本地仓库。
例如对于以前的三个项目中,每一个项目都依赖了一个junit,其实这样重复了不少,可使用继承方式代替这种。
1,新建一个demo5,demo5中定义以下:
<groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo05</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>mavenDemo05</name> <url>http://maven.apache.org</url> <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>
一共关注三点:
1,将packaging改成pom
2,在properties中新增一共junit.version属性。而后能够在version标签中经过${属性名}的方式使用。
3,新增一个dependencyManagement标签,将dependencyies标签放进去。
假如在demo3中继承这里。
demo3中junit依赖定义以下:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency>
</dependencies>
修改,须要将上面的红色部分删除,而后添加一个parent标签。parent标签引入demo05的坐标。
<parent> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo05</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo02</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo01</artifactId> </exclusion> </exclusions> </dependency> </dependencies>