简介
Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具
下载
下载地址: http://maven.apache.org/download.cgi
![](https://img.haomeiwen.com/i2627843/87b57fee00aac681.png)
目录 | 说明 |
---|---|
bin | 包含maven的运行脚本 |
boot | 包含一个类加载器的框架 |
conf | 配置文件 |
lib | 包含maven自身及第三方內库 |
配置环境变量
将解压后的bin
目录路径加到系统环境变量中
![](https://img.haomeiwen.com/i2627843/5cbde495bdc1393d.png)
配置成功
运行maven案例
目录结构
![](https://img.haomeiwen.com/i2627843/96838fb793694eeb.png)
HelloWorld.java
package com.chase.maven01.model;
public class HelloWorld
{
public String sayHello()
{
return "Hello Chase!";
}
}
HelloTest.java
package com.chase.maven01.model;
import org.junit.*;
import org.junit.Assert;
public class HelloTest
{
@Test
public void testHello()
{
Assert.assertEquals("Hello Chase!", new HelloWorld().sayHello());
}
}
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.chase.maven01</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>
# [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project hello: Compilation failure: Compilation failure:
# [ERROR] 不再支持源选项 5。请使用 6 或更高版本。
# [ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
# 加上这段版本号
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-rc-2</version>
</dependency>
</dependencies>
</project>
执行
mvn compile
.
.
.
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Administrator\Java\Hello\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Administrator\Java\Hello\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.913 s
[INFO] Finished at: 2019-12-12T10:33:52+08:00
[INFO] ------------------------------------------------------------------------
mvn package
.
.
.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello ---
[INFO] Building jar: C:\Users\Administrator\Java\Hello\target\hello-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.600 s
[INFO] Finished at: 2019-12-12T10:45:11+08:00
[INFO] ------------------------------------------------------------------------
maven 常用命令
mvn 命令 |
说明 |
---|---|
-v | 查看版本号 |
comile | 编译 |
test | 测试 |
package | 打包 |
clean | 删除target |
install | 安装jar包到本地仓库中 |
自动构建目录骨架
- 按照提示进行选择
mvn archetype:generate
- 一次性设置全部
mvn archetype:generate -DgroupId=组织名(公司网址的反写+项目名称) -DartifactId=项目名-模块名 -Dversion=版本号 - Dpackage=代码所存在的包名
maven仓库
全球仓库地址:https://repo.maven.apache.org/maven2/
修改配置文件conf/setting.xml
- 修改镜像地址
.
.
.
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>maven.net.cn</id>
<mirrorOf>central</mirrorOf>
<name>central mirror in china</name>
<url>http://maven.net.cn/content/group/public</url>
</mirror>
</mirrors>
.
.
.
- 修改仓库地址
.
.
.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>自己喜欢存放的目录</localRepository>
.
.
.
maven生命周期
- clean 清理项目
- pre-clean 执行清理前的工作
- clean 清理上一次构建生产的所有文件
- post-clean 执行清理后的文件
- default 构建项目
- compile
- test
- package
- install
- site 生成项目站点
- pre-site 在生成项目站点前要完成的工作
- site 生成项目的站点文档
- post-site 在生成项目站点后要完成的工作
- site-deploy 发布生成的站点到服务器上
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">
# 指定了当前pom的版本
<modelVersion>4.0.0</modelVersion>
<groupId>反写的公司网址+项目名</groupId>
<artifactId>项目名+模块名</artifactId>
# 第一个数字1表示大版本号
# 第二个数字0表示分支版本号
# 第三个数字0表示小版本号
# snapshot 快照,alpha 内部测试,beta 公测,release 稳定,GA 正式发布
<version>1.0.0snapshot</version>
# 默认是jar,其他为:war 、zip、pom
<packaging></packaging>
# 项目描述名
<name></name>
# 项目地址
<url></url>
# 项目描述信息
<description></description>
# 开发人员列表
<developers></developers>
# 许可信息
<licenses></licenses>
# 依赖包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-rc-2</version>
<type>1</type>
<scope>依赖范围</scope>
<!-- 设置依赖是否可选:默认false-->
<optional>false</optional>
<!--排除依赖列表-->
<exclusions></exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency></dependency>
</dependencies>
</dependencyManagement>
<build>
<!--插件列表-->
<plugins>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</plugins>
</build>
<parent></parent>
<modules></modules>
</project>
依赖
依赖范围:
- compile :默认,编译测试运行都有效
- provided 在编译和测试时有效
- runtime 在测试和运行时有效
- test 只在测试时有效
- system 编译和测试时有效,与本机系统相关联,可移植性差
- import 导入的范围,它只使用在dependencyManagement中,表示从其他的pom中导入dependecy的配置
网友评论