Maven最简单使用步骤
(一)Maven介绍
简单地说Maven是一个软件开发工具,主要的工作是依赖管理(管理项目需要的jar包)、项目构建。
(二)Maven的安装与仓库配置
1、下载Maven,解压,了解Maven的目录结构
bin目录 mvn.bat (以run方式运行项目)、 mvnDebug.bat(以debug方式运行项目 )
boot目录 maven运行需要类加载器
conf目录 settings.xml 整个maven工具核心配置文件
lib目录 maven运行依赖jar包
2、配置环境变量
(1)配置 MAVEN_HOME
(2)将 %MAVEN_HOME%/bin 加入环境变量 path
(3)打开命令行,通过 mvn -v命令检查 maven是否安装成功(如图安装成功)
3、配置Maven本地仓库(jar包所在仓库:repository压缩包,上网进行下载)
(1)解压下载的repository压缩包
(2)
在MAVE_HOME/conf/settings.xml文件中配置本地仓库位置:
(三)eclipse中使用Maven程序入门
1、指定Maven的安装目录:Windows--Preferences--Maven--Installation--Add进行添加
2、User Setting配置:Windows--Preferences--Maven--User Settings
配置settings.xml文件,要点击UPdate Settings更新设置。
3、Eclipse中浏览配置好的仓库:Windows--Show view--Maven--Maven Repositories.
4、找到Local respository本地仓库项,右键,点击Rebuild index重建索引
完成之后可以看到之前解压的时候看到的jar包仓库:
5、创建Maven工程File--new--Maven Project
6、坐标定义,项目出错,别着急,看下面
项目报错:建立web.xml文件
在src/webapp中添加WEB-INF/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>
7、项目默认的编译JDK版本是1.5,将他修改为我们自己的JDK版本。在pom.xml中加入:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加完毕以后,右键工程--Maven--Update Project,进行更新工程。
8、编写servlet进行测试
在src/main/java中创建ServletTest。和我们普通创建方式一致。创建好之后发现Servlet文报错,往下看:
9、添加servlet/jsp的jar包
编辑pom.xml:添加下面内容到pom.xml中
<!-- 添加servlet-api,jsp-api -->
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
10、将项目部署到服务器上,打开浏览器运行项目。