maven入门(一)

如今基本上不少项目开始使用maven进行构建项目,已经不是直接在lib文件夹下统一放置jar包,因此仍是有必要学习掌握下maven的。html

针对maven。这篇文章主要介绍一下几点,你们若是都明白了,就能够参照这个思惟导图,再复习下,毕竟知识长时间不用,会忘记的。 enter image description herejava

###1、 maven 是什么web

Maven的Apache公司开源项目,是项目构建工具,也用来依赖管理。apache

2、maven的好处

一、因为maven构建的项目是没有jar包的,因此项目的大小上,确定是maven的项目比较小。 二、jar包统一交给maven管理。 三、maven一样能够进行项目构建。tomcat

maven主要就是 项目构建和依赖(jar包)管理app

3、maven安装

maven程序安装前提:maven程序java开发,它的运行依赖jdk。eclipse

一、 首先去Maven官网,下载Maven的包,地址为http://maven.apache.org/download.cgi 二、下载完解压,而后配置一下环境变量,和JDK的环境变量配置相似(如图) enter image description herewebapp

enter image description here 三、查询maven版本信息 enter image description here 四、 配置本地仓库 找到 解压目录下的 config/setting.xml,个人就是 E:\app\apache-maven-3.5.3\conf\setting.xml 主要修改2个地方 4.1 修改本地仓库路径 enter image description here 4.2 修改为阿里云镜像jsp

<mirror>
		<id>nexus-aliyun</id>
		<mirrorOf>*</mirrorOf>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public</url>
	</mirror>

enter image description here

4.4 复制刚刚设置好的setting.xml 到你设置的本地仓库路径 个人是E:\dev_maven enter image description heremaven

5.仓库类型有哪些 enter image description here

4、 使用maven构建项目

这里我是用eclipse 进行建立的

一、eclipse配置

1.1 配置maven程序 enter image description here 1.2 配置userSetting ,知道仓库位置 enter image description here 1.3 构建索引,方便查找jar包(Window->show view ->maven Repository) enter image description here

二、开始建立项目 2.1 这里选择普通项目- Maven Project ,点击next http://p7zk4x9pv.bkt.clouddn.com/maven/TIM%E6%88%AA%E5%9B%BE20180430165750.png

enter image description here

2.2 打包方式选择war ,完成。 enter image description here

2.3 web.xml缺失报错

此时,会报错,须要在src-main-webapp 下面建立 WEB-INF/web.xml

目录结构 enter image description here

web.xml 内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

</web-app>

同时建立一个index.xml_(src\main\webapp\index.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello maven</h1>
</body>
</html>

设置jdk编译版本为1.8.默认为1.5 修改pom文件

<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.zhb</groupId>
  <artifactId>maven_hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  
  	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>  
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

更新maven enter image description here

  1. 运行项目

enter image description here

tomcat:run

enter image description here

4.大功告成 enter image description here enter image description here

5、maven 经常使用命令

clean:清理

将项目根目录下target目录清理掉

compile:编译

将项目中.java文件编译为.class文件

test:单元测试

单元测试类名有要求:XxxxTest.java 将项目根目录下src/test/java目录下的单元测试类都会执行。

package:打包

web project --- war包 java project --- jar 包 将项目打包,打包项目根目录下taget目录。

install:安装

本地多个项目公用一个jar包。 打包到本地仓库

这里你们本身尝试一下,进入工程目录里面 如打包 则执行 mvn package enter image description here enter image description here

2018年5月20日更新

我如今发现 我先在使用idea开发,你们能够在本身电脑的c盘的,m2 文件夹下将setting.xml 考到里面,这样本地仓库就是你设置的了

好了,写了很久。终于弄完了。玩的开心。

相关文章
相关标签/搜索