maven入门

本文介绍了Maven的安装步骤,包括依赖于Java环境的安装,以及配置阿里云镜像以加速下载。此外,详细讲述了如何设置本地仓库和Java版本,并解析了Maven的依赖管理、项目构建功能,以及坐标、作用域、打包方式、依赖排除和项目继承的概念。
摘要由CSDN通过智能技术生成

maven的安装 

maven的安装依赖于java环境,首先安装java环境然后安装maven并且在系统中进行配置

打开apache-maven-3.8.1\conf\settings.xml文件进行如下配置 

配置阿里云镜像提高下载速度

这段代码要放在<mirrors></mirrors> 标签中

<mirror>
	<id>alimaven</id>
	<mirrorOf>central</mirrorOf>
	<name>aliyun maven</name>
	<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
   </mirror>

设置本地仓库 

<localRepository>D:\Maven\maven-repository</localRepository>

设置java版本

<profile>     
    <id>jdk-1.8</id>   
    <activation>        
          <activeByDefault>true</activeByDefault>    
          <jdk>1.8</jdk>      
     </activation>  
     <properties>  
          <maven.compiler.source>1.8</maven.compiler.source> 
          <maven.compiler.target>1.8</maven.compiler.target> 
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>   
     </properties>
     </profile>

maven的作用

1,依赖管理

2,项目构建(对项目的编译,测试,打包,部署...等等)

maven坐标含义 

<dependency>
    <!-- 组id 我们在创建maven项目时组id就是我们打包到本地仓库的位置-->
    <groupId>junit</groupId>
    <!-- 项目名 -->
    <artifactId>junit</artifactId>
    <!-- 版本-->
    <version>4.13.1</version>
    <!-- 作用域默认值为compile -->
    <scope>test</scope>
</dependency>

maven常见作用域及作用范围 

 provided表示这个jar包服务器已经提供了,在我们部署项目时jar包不会被部署到服务器上仅在本地起作用

打包方式

<packaging>xxx</packaging>

pom:当项目存在继承关系时父项目的打包方式为pom

jar: Java项目打包方式

war:web项目打包方式

依赖传递

依赖排除

A依赖希望排除C依赖则只需在引入B依赖的时候进行如下操作

<dependency>
   <exclusions>
       <exclusion>
           <groupId>xxx</groupId>
           <artifactId>C</artifactId>
        </exclusion>
    </exclusions>
    <groupId>xxx</groupId>
    <artifactId>B</artifactId>
    <version>1.0</version>
</dependency>

maven项目继承 

maven项目继承的主要目的是实现父项目对子项目的管理列如依赖管理

虽然子工程继承了父工程但是我们要在子工程中声明依赖坐标才能使用依赖

打包方式为pom的工程才可以作为父工程

 <!--    父工程的pom.xml文件          -->
<properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
<!--    创建自定义标签使用${标签名}来进行使用        -->
         <com.test>4.12</com.test>
    </properties>
<!--    父工程对子工程的依赖管理          -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${com.test}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

子工程的pom.xml文件

<!-- 通过<parent></parent>标签引入父工程  -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.7.10</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 在子工程的依赖中我们如果不配置版本号那么就使用父工程配置好的 
如果子工程配置的版本号和父工程不一样那么使用子工程的版本号 -->
<dependencies>
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
    </dependency>
</dependencies>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值