Maven

介绍和搭建

介绍

Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告,和文档的软件项目管理工具。

环境搭建

网址:https://maven.apache.org/download.cgi

windows下载这个包:

配置环境变量:

M2_HOME :

path: %M2_HOME%\bin;

打开cmd验证:

修改配置文件中本地仓库位置:

打开setting.xml文件:

修改仓库为你自己的文件夹位置:

保存关闭即可。

小案例

Maven约定目录结构

mvn-project

​ src

​ -main

​ -java

​ -package

​ -test

​ -java

​ -package

​ resources

​ target

​ pom.xml

小测试

常用maven命令

mvn -v 查看maven版本

mvn compile 编译

mvn test 测试

mvn package 打包

mvn clean 删除target

mvn install 安装jar包到本地仓库

建立maven-test目录并在其中建立如下目录结构

在main最后目录下建立一个Hallo.java文件,内容如下:

package com.imooc.maven01.model;

public class Hello{
public String sayHello(){
return "Hello";
}
}

在test文件夹最后目录下建立一个TestHello.java文件,内容如下:

package com.imooc.maven01.model;

import org.junit.*;
import org.junit.Assert.*;
public class HelloTest{
@Test
public void testSayHello(){
Assert.assertEquals("Hello",new Hello().sayHello());
}
}

在maven-test文件夹下建立一个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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc.maven01</groupId>
<artifactId>maven-test</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>

在maven-test目录下执行maven命令:

mvn compile

输出如下:

mvn test

输出如下:

mvn package

输出如下:

执行mvn clean,输出如下:

可以看到target文件夹已经不存在了:

执行 mvn install,输出如下:

可以在本地仓库中看到我们的jar包已经存在:

如果输出正确,代表安装及项目创建正确。

注册到仓库并被其他项目依赖

在maven-test目录执行: mvn install

成功后新建一个项目maven-test-2,目录如下:

并且在main最后一个目录下建立一个Speak.java文件,内容如下:

package com.imooc.maven02.util;
import com.imooc.maven01.model.*; public class Speak{
public String useSayHallo(){
return new Hello().sayHello();
}
}

在test最后一个文件夹下建立一个TestSpeak.java文件,内容如下:

package com.imooc.maven02.util;

import org.junit.*;
import org.junit.Assert.*;
public class SpeakTest{
@Test
public void testSayHello(){
Assert.assertEquals("Hello",new Speak().useSayHallo());
}
}

pom.xml添加junit和maven-test依赖:

<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc.maven02</groupId>
<artifactId>maven-test-2</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>com.imooc.maven01</groupId>
<artifactId>maven-test</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>

执行maven test

成功,表示依赖相关操作正确。基于此功能方便在其他原有项目上构建更大的项目。

自动生成maven项目骨架

在命令行输入mvn archetype:generate,然后等待下载完相应插件根据提示输入groupId,artifactId,version然后就会自动帮你创建一个对应的maven骨架项目

结果如下:

或者直接设置所有的属性:

mvn archetype:generate -DgroupId=com.imooc.maven04 -DartifactId=maven-test-4 -Dversion1.0

-Dpackage=com.imooc.maven04.demo

然后一路回车即可完成创建

IDEA中配置Maven

当前项目配置:

为新建项目配置默认maven:

最好再更改一下下面的设置:

pom.xml解析

<project>

​	<modelVersion></modelVersion>

​	<groupId>反写公司网址+项目名</groupId>

​	<artifactId>项目名+模块名</artifactId>

​	<!--大版本.分支版本.小版本 snapshot快照,alpha内测,beta公测,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>

​	<build>

​		<!--插件列表-->

​		<plugin>

​				<groupId></groupId>

​				<artifactId></artifactId>

​				<version></version>

​		</plugin>

​	</build>

<!--继承父模块-->

<parent></parent>

<!--聚合多个模块编译-->

<modules></modules>

</dependencyManagement>

</project>

依赖范围

三种类路径

编译,测试,运行

scope

compile:默认范围,编译测试运行都有效

provided:在测试和编译时有效

runtime:只在测试和运行时有效

test:只在测试时有效

system:只在测试和编译时有效,与本地环境关联,可移植性较差

import:导入的范围,只使用在dependencyManagement中,表示从其他的pom中导入dependecy的配置

依赖传递

bige<--nange<--shanji(默认依赖bige可以排除)

hongxing-bige的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bige</artifactId>
<version>1.0-SNAPSHOT</version> <name>hongxing-bige</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0</version>
</dependency>
</dependencies> </project>

hongxing-nange的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>1.0-SNAPSHOT</version> <name>hongxing-nange</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bige</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies> </project>

hongxing-shanji的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId>
<artifactId>hongxing-shanji</artifactId>
<version>1.0-SNAPSHOT</version> <name>hongxing-shanji</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </project>

hongxing-shanji如果不想依赖hongxing-bige可以用下面的方式引入hongxing-nange

<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bige</artifactId>
</exclusion>
</exclusions>
</dependency>

依赖冲突

短路优先

A-->B-->C-->X(jar)

A-->D-->X(jar)

会优先解析短的路径

上面hongxing-shanji默认的commons-io的版本会是hongxing-nange中的版本

路径长度相同,谁的dependacy先声明,谁先解析

A-->B--X(jar)

A-->D-->X(jar)

如果在A中先声明D就用D中的X.jar版本

聚合

可以建立一个聚合maven项目来管理hongxing-bige,hongxing-nange,hongxing-shanji

建立一个hongxing-allmaven项目,pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId>
<artifactId>hongxing-all</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <name>hongxing-all</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <modules>
<module>../hongxingbige</module>
<module>../hongxingnange</module>
<module>../hongxingshanji</module>
</modules>
</project>

运行该项目,会将三个module的pom.xml都运行

继承

可以建立一个父maven项目来提供依赖给子项目继承,父类提供详细描述,子类只需填写groupId和artifactId即可引入依赖。

继承后各pom.xml(注意要先将父pom注册到仓库中)

hongxing-parent的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>hongxing-parent</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.11</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement> </project>

hongxing-bige的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bige</artifactId>
<version>1.0-SNAPSHOT</version> <name>hongxing-bige</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0</version>
</dependency>
</dependencies> </project>

hongxing-nage的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>1.0-SNAPSHOT</version> <name>hongxing-nange</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bige</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies> </project>

hongxing-shanji的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
<modelVersion>4.0.0</modelVersion> <groupId>com.hongxing</groupId>
<artifactId>hongxing-shanji</artifactId>
<version>1.0-SNAPSHOT</version> <name>hongxing-shanji</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bige</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </project>

最新文章

  1. wParam和lParam两个参数到底是什么意思?
  2. C#----我对坐标系的理解和图形转动
  3. c#中的linq二
  4. 创建透明的UIToolbar
  5. 用JavaScript探测页面上的广告是否被AdBlock屏蔽了的方法
  6. 《C程序设计语言现代方法》第5章 选择语句
  7. ibatis.net调用oracle存储过返回游标SYS_REFCURSOR结果集
  8. linux 6.4平台利用rman迁移oracle 11g r2数据库
  9. 阅读UML类图和时序图
  10. MongoDB导入导出以及数据库备份
  11. Jerry 2017年的五一小长假:8种经典排序算法的ABAP实现
  12. macbook 入门
  13. 【js高程学习笔记】Object类型
  14. [C#]中获取当前程序运行路径的方法
  15. vuex的module的简单实用方法
  16. 对话框 AlterDialog
  17. 菜单栏--Dom选择器
  18. Chrome 如何知道网站启用了SPDY 协议?
  19. 设置mysql group_concat长度
  20. ARKit从入门到精通(11)-ARKit开发常见问题及解决方案

热门文章

  1. 前端开发面试题 — html篇
  2. 【观隅】数据集管理与可视化平台-NABCD分析
  3. Qt开发技术:图形视图框架(一)基本介绍
  4. 又一起.NET程序挂死, 用 Windbg 抽丝剥茧式的真实案例分析
  5. 【CTF】2019湖湘杯 miscmisc writeup
  6. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之文件配置-08
  7. TP6学习笔记一:安装与基本配置
  8. 熟知Mysql基本操作
  9. OO第二单元总结——电梯
  10. 5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows