Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告,和文档的软件项目管理工具。
网址:https://maven.apache.org/download.cgi
windows下载这个包:
配置环境变量:
M2_HOME :
path: %M2_HOME%\bin;
修改配置文件中本地仓库位置:
打开setting.xml文件:
保存关闭即可。
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包到本地仓库
在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.0modelVersion>
<groupId>com.imooc.maven01groupId>
<artifactId>maven-testartifactId>
<version>1.0version>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.10version>
dependency>
dependencies>
project>
在maven-test目录下执行maven命令:
mvn compile
输出如下:
mvn test
mvn package
执行mvn clean,输出如下:
可以看到target文件夹已经不存在了:
可以在本地仓库中看到我们的jar包已经存在:
如果输出正确,代表安装及项目创建正确。
在maven-test目录执行: mvn install
并且在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.0modelVersion>
<groupId>com.imooc.maven02groupId>
<artifactId>maven-test-2artifactId>
<version>1.0version>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.10version>
dependency>
<dependency>
<groupId>com.imooc.maven01groupId>
<artifactId>maven-testartifactId>
<version>1.0version>
dependency>
dependencies>
project>
成功,表示依赖相关操作正确。基于此功能方便在其他原有项目上构建更大的项目。
在命令行输入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
然后一路回车即可完成创建
当前项目配置:
为新建项目配置默认maven:
最好再更改一下下面的设置:
<project>
<modelVersion>modelVersion>
<groupId>反写公司网址+项目名groupId>
<artifactId>项目名+模块名artifactId>
<version>version>
<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>
<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>
编译,测试,运行
compile:默认范围,编译测试运行都有效
provided:在测试和编译时有效
runtime:只在测试和运行时有效
test:只在测试时有效
system:只在测试和编译时有效,与本地环境关联,可移植性较差
import:导入的范围,只使用在dependencyManagement中,表示从其他的pom中导入dependecy的配置
bige<–nange<–shanji(默认依赖bige可以排除)
hongxing-bige的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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-bigeartifactId>
<version>1.0-SNAPSHOTversion>
<name>hongxing-bigename>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.0version>
dependency>
dependencies>
project>
hongxing-nange的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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-nangeartifactId>
<version>1.0-SNAPSHOTversion>
<name>hongxing-nangename>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-bigeartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.5version>
dependency>
dependencies>
project>
hongxing-shanji的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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-shanjiartifactId>
<version>1.0-SNAPSHOTversion>
<name>hongxing-shanjiname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-nangeartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
hongxing-shanji如果不想依赖hongxing-bige可以用下面的方式引入hongxing-nange
<dependency>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-nangeartifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-bigeartifactId>
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如下
<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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-allartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<name>hongxing-allname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<modules>
<module>../hongxingbigemodule>
<module>../hongxingnangemodule>
<module>../hongxingshanjimodule>
modules>
project>
运行该项目,会将三个module的pom.xml都运行
可以建立一个父maven项目来提供依赖给子项目继承,父类提供详细描述,子类只需填写groupId和artifactId即可引入依赖。
继承后各pom.xml(注意要先将父pom注册到仓库中)
hongxing-parent的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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-parentartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<name>hongxing-parentname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.11junit.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
dependencies>
dependencyManagement>
project>
hongxing-bige的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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-bigeartifactId>
<version>1.0-SNAPSHOTversion>
<name>hongxing-bigename>
<url>http://www.example.comurl>
<parent>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-parentartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.0version>
dependency>
dependencies>
project>
hongxing-nage的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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-nangeartifactId>
<version>1.0-SNAPSHOTversion>
<name>hongxing-nangename>
<url>http://www.example.comurl>
<parent>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-parentartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
<dependency>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-bigeartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.5version>
dependency>
dependencies>
project>
hongxing-shanji的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.0modelVersion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-shanjiartifactId>
<version>1.0-SNAPSHOTversion>
<name>hongxing-shanjiname>
<url>http://www.example.comurl>
<parent>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-parentartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
<dependency>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-nangeartifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>com.hongxinggroupId>
<artifactId>hongxing-bigeartifactId>
exclusion>
exclusions>
dependency>
dependencies>
project>