项目管理工具Maven安装与配置
1.Maven简介
Maven 是 Apache 软件基金会的一个开源的项目管理工具,它用来帮助开发者管理项目中的 jar,以及 jar 之间的依赖关系、完成项目的编译、测试、打包和发布等工作。
2.Maven中的概念
2.1Pom(Project Object Model)项目对象模型
Maven 管理的项目的根目录下都有一个 pom.xml 文件。pom.xml 文件指示Maven 如何工作。
在 pom.xml 文件中配置项目基本信息以及项目构建信息等。比如:项目坐标、项目依赖的 jar、插件、编译选项等。
一旦在 pom.xml 文件中配置了所依赖的 jar,Maven 会自动从构件仓库中下载相应的构件。
2.2项目坐标
maven 给每个 jar 定义了唯一的标志,这个在 maven 中叫做项目的坐标,通过这个坐标可以找到你需要 用到的任何版本的 jar 包。
groupId、artifactId、packaging、version 的组合被称为项目的坐标,它们形 成了项目的唯一标识,Maven 通过坐标来精确定位构件。
其中 groupId、artifactId、version 是必须的,且这三项的值必须唯一,packaging 是可选的(默认为 jar)。
2.3仓库
中央仓库全球共享,先将jar从中央仓库下载到本地仓库,然后再项目中引用本地仓库的jar。
3.Maven开发环境搭建
前提:
1.安装jdk;
2.Maven工作时需要通过网络下载各种插件以完成任务,因此应保证网络的通畅;
3.1下载maven服务器
1.进入maven 官网:http:// maven.apache. org
2.点击左侧download
3.点击下载 apache-maven-3.6.3-bin.zip 并进行解压
3.2配置Maven
在系统环境中配置环境变量
在path中配置到Maven的bin目录
配置成功后在cmd中输入命令mvn -version 查看版本
表示 maven 环境配置成功。
配置本地仓库地址:
在本地磁盘中建立通过 maven 下载后 jar 文件所存储的文件夹,一般我们称为”仓库(repository)”;最终是如何将下载的 jar 文件存储到我们指定的仓库中呢?需要在 maven 的服务器解压的文件中找到 conf 文件夹下的settings.xml 文件进行修改,如下图所示:
配置阿里云 maven 镜像仓库,下载速度更快
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
3.3 在idea中设置maven
注意要修改图中红色框中的默认路径。
3.4创建maven工程
4.pom.xml配置
maven 仓库官网(http://mvnrepository.com/)
在此官网就可以下载自己所需开发 jar 包
示例:下载servlet jar包
在 Pom.xml 文件配置 jar 包信息,使用 maven 自动下载 jar 包
管理 jar 之间的依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
配置servlet开发环境
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
子元素功能解释:
,和:依赖项的基本坐标;
:依赖项的packaging类型,默认是jar;
:依赖项的作用范围,默认是compile。各个取值的含义如下:
compile:编译时和运行时依赖。如果你的项目最终打包为 war 包,则该依赖包会被拷贝到 war 包中的 lib 目录中。
provided:编译项目主代码和测试代码时使用此依赖项,运行时由 jre 或 web容器提供。如果你的项目最终打包为 war 包,则该 jar 包不会被打包。
5.maven命令
maven常用命令
- compile 编译
- clean删除
- test test case junit/testNG
- package 打包
- install 把项目 install 到本地仓库
常用jar坐标:
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--spring-mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- spring-boot web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- 配置springboot内置tomcat环境 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.22</version>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- 配置springboot集成mybatis环境 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>