狂啃maven

一、maven的本地安装

1、maven下载网址建议大家下载3.5.2的版本
2、解压后打开目录****\apache-maven-3.5.2\conf,然后找到settings.xml文件,可以用记事本编辑修改里面的部分内容,找到如下内容这是我的settings.xml文件
然后在这一段的下一行添加一行代码,成为这个模样
添加的这一段代码就是注释里面的最后一行,将他复制然后然后修改中间的路径为自己本地仓库文件夹的路径,当然在这之前得事先创建好这个文件夹,只需创建好文件夹即可。我的本地仓库文件夹就是在D盘下my_maven_local_repository1文件夹。只要大家稍微会点英语就能看明白注释里面的内容是什么意思,是可以修改本地仓库的位置,因为默认的位置是在C盘用户目录下的.m2文件夹下,如果不是很懂英语的话就不要妄加猜测了,以免迷失心智,走火入魔
3、然后还是要修改settings.xml里面的内容,找到这
然后修改为
道理是和第二部一样的,注意添加代码的位置。这一步是为了从阿里云的maven镜像里下载所需的依赖,原来的实在太慢。

二、创建一个maven项目

IDE:idea 2019.1 java:jdk 12
版本没有大影响,现在很多人在用jdk 8,我感觉没啥影响,8是一个长期维护版本,并且可以满足日常程序开发的需求,而且比较稳定,但是12也不是不行,大家可以都下载,到时候修改环境变量就可以。

生成项目之后,大家就会看到pom.xml文件,这个文件是maven项目的核心,但是先不着急说。

三、配置maven环境变量,了解mvn命令

简单说就是添加环境变量MAVEN_HOME 为你maven的路径,然后再path里面添加%MAVEN_HOME%\bin!
然后cmd输入mvn -v,配置成功的话是下面这样
然后进入打开刚才创建的maven项目的,找到存放pom.xml的文件,在该目录空白处按住shift键同时点击鼠标右键进入powershell。然后就可以运行mvn命令了。
在这里插入图片描述

四、maven的生命周期

可以先看一下下面的这张图

大家看一看执行命令mvn clean install之后,maven都做了哪些工作
第一步就是clean,他就是在构建项目之前会查找target文件,就是把之前编译的文件删除重新编译,可以看到在clean插件下面显示删除了该项目中的target文件,clean生命周期里面包括了三个阶段。

下一个是compile,就是编译阶段,在编译之前会先执行resources插件就是把整个src目录下的非java文件复制到target文件里,然后执行编译插件讲java文件编译到target里面的classes文件里。

在下面就是测试文件夹里的编译,原理和上面的一样,不过我没有test文件夹所以就没有变化。

在下面有个war插件,就是把项目给打包。

在下面是install插件就是把war包上传到本地仓库里。如果执行的是mvn clean package命令的话就是完成打包工作不会上传到本地仓库,如果执行的是mvn clean deploy命令就是打包后不但会上传到本地仓库,而且会上传到远程仓库。可以自己操作去验证一下。

五、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.apple</groupId>
    <artifactId>apple_one</artifactId>
    <version>1.0-SNAPSHOT</version>
    //配置打包类型,常见的就是jar,war,pom,jar类型就是java文件,war类型就是web项目,pom类型用在父模块
    <packaging>pom</packaging>
    //常规的数据配置,可以在xml文件中引用
    <properties>
        <junit.version>4.11</junit.version>
        <spring.version>4.3.6.RELEASE</spring.version>
        <mybatis.version>3.4.2</mybatis.version>
        <mybaits-spring.version>1.3.1</mybaits-spring.version>
    </properties>
    //父模块用来管理依赖会用dependencyManagement标签
    <dependencyManagement>
    //普通的添加依赖
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybaits-spring.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    //项目的构建
    <build>
    <finalName>${project.artifactId}</finalName>
    //插件
        <plugins>
    <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                 <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        //父模块的插件管理类似于依赖管理
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

maven项目有着继承和聚合的特性
简单来说继承就是子项目可以继承父项目的一些配置,比如常用的依赖就可以用父模块来管理,子模块直接引用即可,还有很多标签都可以继承
聚合就是将多个多个模块集合在一个模板模块中。接下来会进行实战在maven中聚合多模块ssm框架。

六、maven多模块聚合

下一篇博客献上在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值