Maven 基本的认识

Maven 基本的认识

1. 什么是Maven?

在平时开发中,经常遇到某个jar包,我在代码层已经Import 和@Automation了,编译器还是提醒你某个jar包找不到,往往这时来个mvn install 问题就解决了。可想而知,Maven 是帮助我们开发者管理Jar包的工具

2. Maven基本命令

2.1 代码的编译

 mvn compile

同样的,这里把项目jar代码编译成.class文件到项目的target文件夹

2.2 安装包

mvn install 

编译好的.class文件,maven 自动地安装到本地Repository,即maven 的仓库。为什么要安装到仓库,这样的作用是什么?

  • 方便Maven工具快速找到相关的jar 包,引入项目里
  • 集中管理项目的jar包

2.3 清理目标文件

mvn clean 

maven 只对项目中的target 中的文件清理,原先的安装的文件不受影响。

3. Maven POM

3.1 什么是POM?

POM代表项目对象模型。它是 Maven 中工作的基本单位,这是一个 XML 文件。它始终保存在该项目基本目录中的 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.lollipop.api</groupId>  
 <artifactId>ali</artifactId>  
 <version>1.0-SNAPSHOT</version>  
</project>

3.2 POM中装包类型

  • 装包的属性
	 <packaging></packaging>
  • 装包类型
  1. pom 类型,所有的父级项目的packaging都为pom
  2. jar 类型,默认是jar类型,如果不作配置,maven会将该项目打成jar包,作为内部调用或者是作服务使用
  3. war 类型,如果是需要部署的项目,则需要打包成war类型
    每个pom.xml 文件必需有project 、groupId、artifactId、version。
    | 元素 | 说明 |
    |:-------😐:----😐
    | project | 项目根目录 |
    | groupId | 公司或组织域倒序+项目 |
    | artifactId | 模块名称 |
    | version | 模块版本号 |

4. Maven 依赖

4.1依赖的范围

4.1.1 范围标签
<dependencies>
   <dependency>
      <groupId>xxx.xx.x</groupId>
      <artifactId>xxx</artifactId>
      <version>4.0</version>
      <scop>xxx</scop> #范围标签
   </dependency>
</dependencies>
4.1.2 范围标签作用域值
  1. compile 范围依赖
    • 对程序编译和打包期,该作用域有效
  2. test 范围依赖
    • 对测试程序编译和运行期,该作用域有效
  3. procided 范围依赖
    • 除打包和部署期无效后,其他期都有效

4.2 依赖的继承

在项目里,如果多个子模块需要用到同个依赖包,我们可以提取它们到父 POM,子POM仅仅简单的引用
父pom.xml

<project>
   ...
  <dependencyManagement>
   <dependencies>
    <dependency>
	  <groupId>group-a</groupId>
	  <artifactId>artifact-a</artifactId>
	  <version>1.0</version>

	  <exclusions>
	  <exclusion>
	  <groupId>group-c</groupId>
	  <artifactId>excluded-artifact</artifactId>
	  </exclusion>
	  </exclusions>

  </dependency>

  <dependency>
	  <groupId>group-c</groupId>
	  <artifactId>artifact-b</artifactId>
	  <version>1.0</version>
	  <type>war</type>
	  <scope>runtime</scope>
  </dependency>

  <dependency>
	  <groupId>group-a</groupId>
	  <artifactId>artifact-b</artifactId>
	  <version>1.0</version>
	  <type>bar</type>
	  <scope>runtime</scope>
  </dependency>
  </dependencies>
 </dependencyManagement>
</project>

子pom.xml

  <project>
     ...
  <dependencies>
	  <dependency>
		  <groupId>group-c</groupId>
		  <artifactId>artifact-b</artifactId>
		  <!-- This is not a jar dependency, so we must specify type. -->
		  <type>war</type>
	  </dependency>

	  <dependency>
		  <groupId>group-a</groupId>
		  <artifactId>artifact-b</artifactId>
		  <!-- This is not a jar dependency, so we must specify type. -->
		  <type>bar</type>
	  </dependency>
  </dependencies>
  </project>

4.3 规范依赖版号

这里就不详细说明了。主要在父级pom里头加入版本号,子级pom简单引入就可以了

有很多地方说的不好,可以去Maven官网看文档, 建议多动手熟悉Maven的知识

posted @ 2019-01-11 17:43  Lollipop.Hsu  阅读(274)  评论(0编辑  收藏  举报