cmd查看maven版本_Maven知识一览

本文详细介绍了Maven的使用,包括环境搭建、命令行操作、项目目录结构、依赖管理、仓库注册、骨架生成以及在IDEA中的配置。通过实例演示了如何创建、打包、测试和安装项目,并探讨了依赖范围、类路径、聚合和继承等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maven

介绍和搭建

介绍

Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告,和文档的软件项目管理工具。

环境搭建

网址:https://maven.apache.org/download.cgi

windows下载这个包:

72b2b69db63d0b38a9e513a45bec0b77.png

配置环境变量:

M2_HOME :

9aff2c2238d8266b7e509f09cd2cc592.png

path: %M2_HOME%bin;

打开cmd验证:

d8593bf762678669f084d0597f52afd1.png

修改配置文件中本地仓库位置:

打开setting.xml文件:

1df3750168e60c8c53a107606ef4d78c.png

修改仓库为你自己的文件夹位置:

90e13aec9c96c14984c29228d1083158.png

保存关闭即可。

小案例

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目录并在其中建立如下目录结构

5f2654ddc46caefa50859015fa8b1f69.png

在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

输出如下:

e9fbbc1e561027979d6176b86ae58a55.png

mvn test

输出如下:

915e43515c6426a363772fcb72dfa86d.png

mvn package

输出如下:

2ca447df1fff0621ad7144828a358bfe.png

执行mvn clean,输出如下:

0378927898a6f4361fefbcba80f7acd6.png

可以看到target文件夹已经不存在了:

a59caaaacb7497e8d1b982339a1f0205.png

执行 mvn install,输出如下:

a9977e2dd50e7ded83d669c1eb838372.png

可以在本地仓库中看到我们的jar包已经存在:

687abee6fb20a3a35ddf3096c7c0b5cf.png

如果输出正确,代表安装及项目创建正确。

注册到仓库并被其他项目依赖

在maven-test目录执行: mvn install

成功后新建一个项目maven-test-2,目录如下:

99bd9d6ed860bf398efed124117b2c73.png

并且在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

949994cedc961578735c8558e1243b2b.png

成功,表示依赖相关操作正确。基于此功能方便在其他原有项目上构建更大的项目。

自动生成maven项目骨架

在命令行输入mvn archetype:generate,然后等待下载完相应插件根据提示输入groupId,artifactId,version然后就会自动帮你创建一个对应的maven骨架项目

3e49ea0092ea4b9109a8e7c0dd3d658a.png

结果如下:

f948c7d2842e4b48e38486b50bbe4adf.png

或者直接设置所有的属性:

mvn archetype:generate -DgroupId=com.imooc.maven04 -DartifactId=maven-test-4 -Dversion1.0

-Dpackage=com.imooc.maven04.demo

然后一路回车即可完成创建

IDEA中配置Maven

当前项目配置:

4f0c9767576adb00bbe882662adec9ea.png

de6a3b7f4c86d11cbe4adecf9c4b08f8.png

为新建项目配置默认maven:

ad0403748ab5e49ad1a560b3faaab6e8.png

637bb61d4f6dc04bf21a184145d9d701.png

最好再更改一下下面的设置:

a298d4427526665e40f5b6ba0b34aea5.png

0bea9ec18f0a7fb39659d4249dea05d1.png

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值