Maven基础

一、Maven的简介

下载
Maven是apache下的一个开源项目,是一个纯Java开发,并且只是用来管理Java项目的工具

二、Maven的功能

1、一键构建

编码、编译、测试、运行、打包、部署

tomcat:run
2、jar赖管理

就是对jar包的统一管理,不用将jar拖到项目中

3、分模块开发

可以将项目分为多个模块开发例如:
用户管理模块、订单管理模块、商品管理模块
Dao模块(jar)、Service模块(jar)、Web模块(war)、Parent(pom)

三、Maven配置

1、配置环境变量
MAVEN_HOME

变量:就是maven解压的目录列如:D:\User\Program\Maven\apache-maven-3.6.3-bin
测试:mvn-v
查看是否显示Maven版本和jdk版本

四,Maven仓库

1、三种仓库
①本地仓库 自己维护

在maven的conf目录的setting.xml下设置自己的仓库repository的路径列如:
D:\User\Program\Maven\repository\repository

②远程仓库(私服)公司维护
③中央仓库 maven团队维护

五、Maven常用命令

Clean:清理编译的文件
Compile:编译主目录的文件
Test:编译并且运行test目录的代码
Package:打包
Install:把项目发布到本地仓库
Tomcat:run 一键启动
deploy:把项目发布到私服

六、Maven的生命周期

①Clean生命周期

Clean

②Default生命周期

Compile、test 、package、install、deploy

③Site生命周期

Site

不同生命周期的命令可以同时进行

七、Maven相关配置

1、配置编译版本和tomcat

<build>
		<!-- 配置了很多插件 -->
		<plugins>
			<plugin>
				 <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- maven内置 的tomcat6插件 -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.1</version>
				<configuration>
					<!-- 可以灵活配置工程路径 -->
					<path>/ssh</path>
					<!-- 可以灵活配置端口号 -->
					<port>8080</port>
				</configuration>
			</plugin>
		</plugins>
	</build>

2、配置添加jar包

坐标GAV
<dependencies>
		<dependency>
			<groupId>组织名</groupId>
			<artifactId>包名</artifactId>
			<version>版本</version>
			<scope>依赖范围</scope>
		</dependency>
</dependencies>

八、依赖版本冲突的解决

1、第一声明优先原则

<dependencies>
  <!--   spring-beans-4.2.4 -->
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>4.2.4.RELEASE</version>
  	</dependency>
  
<!--   spring-beans-3.0.5 -->
  	<dependency>
  		<groupId>org.apache.struts</groupId>
  		<artifactId>struts2-spring-plugin</artifactId>
  		<version>2.3.24</version>
  	</dependency>

2、路径近者优先原则

自己添加jar包

<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-beans</artifactId>
  		<version>4.2.4.RELEASE</version>
  	</dependency>

3、排除原则

<dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-spring-plugin</artifactId>
	<version>2.3.24</version>
	<exclusions>
	  <exclusion>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-beans</artifactId>
	  </exclusion>
	</exclusions>
</dependency>

4、版本锁定原则

<properties>
		<spring.version>4.2.4.RELEASE</spring.version>
		<hibernate.version>5.0.7.Final</hibernate.version>
		<struts.version>2.3.24</struts.version>
	</properties>

	<!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
</dependencies>
</dependencyManagement>

九、依赖范围

Compile 如struts2-core

编译(compile) 是需要、测试时需要、运行是需要、打包时需要

Provided 如jsp-api.jar servlet-api.jar

编译(compile) 是需要、测试(Test)时需要、运行是不需要、打包时不需要

Runtime 如数据驱动包

编译是不需要、测试时需要、运行是需要、打包时需要

Test 如junit.jar

编译是不需要、测试时需要、运行是不需要、打包时不需要

十、分模块开发

依赖范围对依赖传递造成的影响

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值