Maven
介绍和搭建
介绍
Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告,和文档的软件项目管理工具。
环境搭建
网址:https://maven.apache.org/download.cgi
windows下载这个包:
配置环境变量:
M2_HOME :
path: %M2_HOME%in;
打开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>