maven的作用
- maven的作用是完成项目的相关操作,
如:编译,构建,单元测试,安装,网站生成和基于Maven部署项目。 - 可以直接的将xx.jar导入工程中,不需要在去百度下载,加快了程序员来管理导jar包。
maven的配置
-
环境配置
M2_HOME配置maven的安装路径
- path添加maven相关路径bin下的文件
- 检查你的maven是否安装好了
- 配置仓库
- 修改安装文件路径“\config”下的setting.xml文件,仓库的作用就是用来存放jar包的
<localRepository>D:\app\maven</localRepository>
- 在eclipse配置下载的路径
- 配置私人服务器也在setting.xml文件中
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
Maven的使用
-
可以到http://www.mvnrepository.com找到自己要导的jar包的代码
-
在创建web时会出现缺少servlet的index.jsp.
-
可以用这个代码来处理缺少servlet的配置。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1<ersion>
<scope>provided</scope>
</dependency>
- 我们必须要把web.xml改为3.1版本,并且要把jdk配置成1.8版本才能进行web包创建。
- 把web.xml改为3.1版本。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
</web-app>
- 在pom.xml 中把1.5转化为1.8版本
<plugins>
<plugin>
<!--把jdk从1.5转化到1.8版本 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
- 配置调用mysql.jar包方法
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
- 创建web项目把webContent修改为src/main/webapp所要的包下
如下效果 :
执行测试代码
package com.maven;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/mavenServelt")
public class MavenServelt extends HttpServlet {
private static final long serialVersionUID = 4031045895309119879L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("应用成功!!");
}
}