您的位置 首页 java

我是如何理解并使用maven的

前言

一直想写一篇关于Maven的文章,但是不知如何下笔,如果说能使用,会使用Maven的话,一、两个小时足矣,不需要搞懂各种概念。那么给大家来分享下我是如何理解并使用maven的。

什么是Maven?

Maven是一个用于项目构建的工具,通过它便捷的管理项目的生命周期。即项目的jar包依赖,开发,测试,发布打包,主要管理工作是:依赖管理,项目一键构建。

为什么要使用Maven

  • 使用maven构建的项目,整个项目的体积小

  • maven项目不需要手动导入jar包,通过在 pom .xml中添加依赖,引用依赖会自动从maven仓库下载jar包,方便快捷。

  • 项目一键构建:使用maven可以快速地对项目进行编译–测试–运行–打包–安装

  • maven支持跨平台操作,可在window、linux、mac上使用

  • maven遵循规范开发有利于提高大型团队的开发效率,降低项目的维护成本,属于主流技术,一般公司都会使用maven来构建项目

maven仓库的配置

下载项目引用需要jar包的时候存放的本地路径

仓库的分类

  • 本地仓库

  • 私服(公司的仓库)

  • 中央仓库

三个仓库之间的关系

三者之间的关系是,当我们在项目中依赖一个jar包时,Maven程序会先去本地仓库中找,如果没找到就回去私服找,如果还是没有,最后就回去中央仓库找。其过程如下图:

本地仓库的配置

找到已安装的maven路径,如:apache-maven-3.3.9\conf 目录下settings.xml 文件并用notepad++打开,ctrl+F找到localRepository标签,将路径设置为D:/repository,如下图:

说明:什么是本地仓库? 就是是由个人将常用到的jar包放入一个仓库中,已备自己在项目中使用,可从别人配置好的jar包仓库拷到自己本地目录,因为仓库一般很大,首次下载需要很长一段时间。

配置私服地址

实际工作中,很多项目都会用到maven私服仓库,一般公司都有统一的maven私服仓库,由于公司都是统一化管理,这时候我们就要配置统一的私服仓库,举例如下:

<mirrors> 

<mirror>
<!--This is used to direct the public snapshots repo in the
profile below over to a different nexus group -->
<id>nexus-public-snapshots</id>
<mirrorOf>public-snapshots</mirrorOf>
<url>
</mirror>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>
</mirror>
</mirrors>

pom文件说明

pom.xml文件,一般描述了maven项目的基本信息,比如groupId,artifactId,version等,一个最简单的pom.xml文件至少需要包含四个元素:modelVersion, groupId, artiffactId和version。

比如一个基本的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns=""
xmlns:xsi=""
xsi:schemaLocation=" ">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId> //当前项目的信息
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>//SNAPSHOT(快照)表示该项目还在开发中。
</project>

其中主要的标签含义如下:

project:pom.xml 文件中的顶层元素;

modelVersion:指明 POM 使用的对象模型的版本。这个值很少改动。

groupId:指明创建项目的组织或者小组的唯一标识。

GroupId 是项目的关键标识,典型的,此标识以组织的完全限定名来定义。比如,org.apache.maven.plugins 是所有 Maven 插件项目指定的 groupId。

artifactId:指明此项目产生的主要产品的基本名称。项目的主要产品通常为一个 JAR 文件。第二,象源代码包通常使用 artifactId 作为最后名称的一部分。典型的产品名称使用这个格式:

version:项目产品的版本号。Maven 帮助你管理版本,可以经常看到 SNAPSHOT 这个版本,表明项目处于开发阶段。

在项目中添加插件,以及对插件的配置

 <build> 
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>//编译插件
<version>2.4.3</version>//插件的版本号
<configuration>//对插件进行配置
<source>1.7</source>//源代码编译版本
<target>1.7</target>//目标平台编译版本;
<encoding>UTF-8</encoding>//设置插件或资源文件的编码方式。
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>//执行测试用例的插件
<version>2.17</version>//插件的版本号
<configuration>//对插件进行配置
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>//测试套件执行路径
</suiteXmlFiles>

</configuration>
</plugin>
</plugins>
</build>

如何在pom文件中添加依赖jar包

实际开发中需要引用jar包后,再进行开发,那么在pom中添加依赖呢?

1、比如我想添加testng.jar包,那么可以通过访问网址 ,然后在搜索框中输入testng,回车

2、点击testng,选择对应版本如6.14.3

3、复制红框中内容,放到dependencies标签内。

在pom中引用完成,并自动下载依赖jar包。

 <dependencies> 
<!-- -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>

实际效果:

编写一个测试类

package com.test.demo; 

import org.testng.Assert;
import org.testng.annotations.Test;

public class HellowWorld {

@Test
public void test {
Assert.assertEquals("HellowWorld","HellowWorld");
}
}

maven项目的目录结构

F:\mavendemo>tree

卷 新加卷 的文件夹 PATH 列表

卷序列号为 5C5B-6DDB

F:.

├─.idea

└─ src

├─ main

│ ├─ java

│ │ └─com

│ │ └─test

│ │ └─demo

│ └─resources

└─ test

└─java

└─com

└─test

└─demo

注意上面带红色的目录名,maven项目采用“ 约定优于配置” 的原则, src/main/java 约定用于存放源代码, src/main/test 用于存放单元测试代码, src/target 用于存放编译、打包后的输出文件。这是全世界maven项目的通用约定,请记住这些固定的目录结构。

编译和测试

在项目根目录下运行 mvn clean compile 命令。执行输出如下所示:

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-demo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-demo ---
[INFO] Deleting F:\mavendemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-demo ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\mavendemo\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.336 s
[INFO] Finished at: 2019-06-23T17:33:29+08:00
[INFO] Final Memory: 13M/309M
[INFO] ------------------------------------------------------------------------


clean: 清理输出目录target下生成jar包

compile: 编译项目主代码

编译完成后,我们一般都会运行测试代码进行单元测试,虽然很多情况下,我们并没有这么做,但是我还是建议大家通过Maven做一些自动化的单元测试。

测试用例编写完毕之后就可以调用Maven执行测试,运行 mvn clean test 命令,输出如下:

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-demo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-demo ---
[INFO] Deleting F:\mavendemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-demo ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\mavendemo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\mavendemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ maven-demo ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\mavendemo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ maven-demo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.032 s
[INFO] Finished at: 2019-06-23T17:27:28+08:00
[INFO] Final Memory: 15M/309M
[INFO] ------------------------------------------------------------------------

打包和运行

打包就是将我们编写的应用打成JAR包或者WAR包,我们执行 mvn clean package 命令就可以完成打包。 mvn clean package 命令的输出如下:

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-demo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-demo ---
[INFO] Deleting F:\mavendemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-demo ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\mavendemo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\mavendemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ maven-demo ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\mavendemo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ maven-demo ---
[INFO] Surefire report directory: F:\mavendemo\test-output

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.test.demo.TestHellowWorld
Configuring TestNG with: TestNG652Configurator
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.498 sec - in com.test.demo.TestHellowWorld
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-demo ---
[INFO] Building jar: F:\mavendemo\target\maven-demo-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.356 s
[INFO] Finished at: 2019-06-23T17:46:00+08:00
[INFO] Final Memory: 17M/311M
[INFO] ------------------------------------------------------------------------

运行完后,会在target目录下生成jar包

如果别的项目要引用这个JAR包时,我们将这个JAR包复制到其它项目的classpath中就OK了。

但是这样拷贝就违背了我们当初想要自动解决依赖的问题,所以如何才能让其它的Maven项目直接引用这个JAR包呢?我们需要执行 mvn clean install 命令。

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-demo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-demo ---
[INFO] Deleting F:\mavendemo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-demo ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\mavendemo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\mavendemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ maven-demo ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\mavendemo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ maven-demo ---
[INFO] Surefire report directory: F:\mavendemo\test-output

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.test.demo.TestHellowWorld
Configuring TestNG with: TestNG652Configurator
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.611 sec - in com.test.demo.TestHellowWorld
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-demo ---
[INFO] Building jar: F:\mavendemo\target\maven-demo-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-demo ---
[INFO] Installing F:\mavendemo\target\maven-demo-1.0-SNAPSHOT.jar to E:\repository\com\test\maven-demo\1.0-SNAPSHOT\maven-demo-1.0-SNAPSHOT.jar
[INFO] Installing F:\mavendemo\pom.xml to E:\repository\com\test\maven-demo\1.0-SNAPSHOT\maven-demo-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.643 s
[INFO] Finished at: 2019-06-23T20:39:34+08:00
[INFO] Final Memory: 16M/211M
[INFO] ------------------------------------------------------------------------

使用Archetype生成项目骨架

下面通过用命名行创建一个最基本的maven项目

mvn archetype:generate

先创建项目的根目录,从盘符开始,命令行窗口下输入

mkdir demotest

cd demotest

mvn archetype:generate

首次运行时,mvn会从远程”中央仓库”下载一些必需的文件到”本地仓库” –

如果你有兴趣,可以在等待下载过程中,观察一下”C:\Users\当前用户名\.m2\repository”目录下是不是多了很多文件。

下载完成后,会自动进入交互模式,会让你输入一些基本信息,类似下面这样:

F:\demotest>mvn archetype:generate 
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.0:generate (default-cli) @ standalone-pom ---
Downloading:
Downloaded: (8 KB at 5.4 KB/sec)
Downloading:
Downloaded: (5 KB at 10.1 KB/sec)
Downloading:
Downloaded: (5 KB at 11.2 KB/sec)
Downloading:
Downloaded: (2 KB at 3.8 KB/sec)
Downloading:
Downloaded: (19 KB at 41.0 KB/sec)
Downloading:
Downloaded: (32 KB at 26.9 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (4 KB at 8.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloading:
Downloading:
Downloading:
Downloading:
Downloaded: (19 KB at 39.7 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (26 KB at 26.5 KB/sec)
Downloading:
Downloaded: (24 KB at 20.6 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloading:
Downloaded: (33 KB at 20.2 KB/sec)
Downloading:
Downloaded: (22 KB at 10.6 KB/sec)
Downloading:
Downloaded: (43 KB at 19.6 KB/sec)
Downloading:
Downloaded: (12 KB at 4.7 KB/sec)
Downloading:
Downloaded: (30 KB at 8.3 KB/sec)
Downloading:
Downloaded: (240 KB at 56.1 KB/sec)
Downloading:
Downloaded: (121 KB at 27.4 KB/sec)
Downloading:
Downloaded: (0 B at 0.0 KB/sec)
Downloaded: (21 KB at 4.0 KB/sec)
Downloaded: (56 KB at 10.6 KB/sec)
Downloaded: (324 KB at 51.1 KB/sec)
Downloaded: (5394 KB at 19.0 KB/sec)
[INFO] Generating project in Interactive mode
.............................................................................

执行这个命令后,后看到很多输出,然后再按照提示一步步操作,一个Maven项目就创建成功了。

总结

到此,关于maven的入门基础知识总结完毕,文章知识点相对繁琐、复杂,还请读者多次阅读和实践,如有错误之处,烦请多指正!

文章来源:智云一二三科技

文章标题:我是如何理解并使用maven的

文章地址:https://www.zhihuclub.com/190189.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图