Maven安装与使用

0. Maven的好处

  1. 节省磁盘空间
  2. 可以一键构建
  3. 可以跨平台
  4. 应用在大型项目时可以提高开发效率

1. Maven安装

  1. 去官网http://maven.apache.org/download.cgi下载最新版Maven的压缩文件,并解压

  2. 在环境变量中添加本机中Maven的路径
    在这里插入图片描述

  3. 打开cmd测试mvn -version会出现maven版本与本地jdk版本
    在这里插入图片描述

  4. 进入maven目录下的conf打开settings.xml进行配置

    1. settings下配置本地仓库maven的本地仓库目录<localRepository>C:\jarHouse\maven_repository</localRepository>,默认情况本地仓库目录在用户目录下
    2. 配置阿里云镜像,可以提高jar包的下载速度
    	<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-->
    	      <id>nexus-aliyun</id>
    	      <!--该镜像用来取代的远程仓库,central是中央仓库的id-->
    	      <mirrorOf>central</mirrorOf>
    	      <name>Nexus aliyun</name>
    	      <!--该镜像的仓库地址,这里是用的阿里的仓库-->
    	      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    	    </mirror>
    	</mirrors>
    
    1. 配置默认的jdk版本
    	<profile>
                <id>JDK1.8</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </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>
                    <encoding>UTF-8</encoding>
                </properties>
        </profile>
    
  5. 将maven中央仓库添加到书签,以后查找自己需要的jar包可以去中央仓库 https://mvnrepository.com/

2. idea集成maven

  1. 打开settings 将本机中maven的信息加入到 maven的相关配置中
    在这里插入图片描述

  2. 配置自动下载文档
    在这里插入图片描述

  3. 配置jre
    在这里插入图片描述

3. 创建maven项目

可以创建空项目或者webapp
在这里插入图片描述

4. 父子工程pom配置

工程与模块的区别:

  1. 工程不等于完整的项目,模块也不等于完整的项目,一个完整的项目看的是代码,代码完整,就可以说这是一个完整的项目,和此项目是什么没有关系
  2. 工程天生只能使用自己内部的资源,工程天生是独立的,后天可以和其他工程或者模块建立关联关系
  3. 模块天生不是独立的,模块天生是属于父工程的,模块一旦创建,所有父工程的资源都可以使用

父子工程之间: 子模块天生继承父工程,可以使用父工程的所有资源
子模块之间: 子模块之间天生是没有任何关系的,需要后天引入依赖

父子工程直接不用建立关系,继承关系是先天的,不需要手动建立
平级之间的引用叫依赖,依赖不是先天的,需要后天建立
作用域为test的junit从父工程传递到dao中是不可以使用的
在这里插入图片描述

4.1 父模块中可以看到其子模块

将父模块打包方式设置为pom

	<modules>
        <module>dao</module>
        <module>service</module>
        <module>web</module>
    </modules>
    <packaging>pom</packaging>
4.2 统一管理jar包版本
	<!-- 统一管理jar包版本 -->
    <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>
        <spring.version>5.0.2.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <shiro.version>1.2.3</shiro.version>
        <mysql.version>8.0.18</mysql.version>
        <mybatis.version>3.4.5</mybatis.version>
        <spring.security.version>5.0.1.RELEASE</spring.security.version>
    </properties>

4.3 锁定jar包版本
	<!-- 锁定jar包版本 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
        <dependencies>
    <dependencyManagement>
4.4 tomcat7插件

Maven中集成了tomcat7插件,在ssm阶段都是可以使用的,到SpringBoot阶段可以使用springboot集成的tomcat
启动tomcat命令为 mvn tomcat7:run

	<!-- tomcat7插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 访问项目的端口号 -->
                    <port>80</port>
                    <!-- 项目访问路径-->
                    <path>/mavenSplit</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
父子工程打包顺序(SSM)
  1. 执行clean命令
  2. 先分别将除web外的子模块(打包方式为jar)install到本地
  3. package web模块(打包方式为war)
  4. web模块下的的target中有已经打好的war
父子工程打包顺序(Spring Boot)
  1. 执行clean命令
  2. web模块中添加spring boot的web打包依赖
    	<build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
  3. packageweb模块
  4. web模块下的的target中有已经打好的jar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值