目录
Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念。
作用:完成项目的相关操作,如:编译,构建,单元测试,安装,网站生成和基于Maven部署项目。
1.1下载maven安装包,解压即可使用
http://maven.apache.org/download.cgi

在D盘建一个CDLMaven的文件夹,将下载的压缩包放进去

解压到当前文件夹
1.2 MAVEN_HOME
MAVEN_HOME:一定要点到下一层是bin再复制路径

1.3 修改path添加maven相关路径
配置方式跟jdk有些类似,环境变量MAVEN_HOME和M2_HOME的值为maven的根目录、
然后在PATH环境变量里加入“%MAVEN_HOME%\bin”即可
注意:不要多打符号

1.4 验证
doc窗口执行命令“mvn –version”

2.1仓库介绍:
仓库的作用就是用来存放jar包的
仓库的分类:
中央仓库
公司仓库(也叫私人仓库或私服)
本地仓库中央仓库(http://search.maven.org/,但一般使用另外一个网站:http://www.mvnrepository.com/)
公司仓库
本地仓库
公司里面还会有一个大仓库(本地)全公司使用
本地仓库程序员自己使用,私服全公司使用,中央仓库所有人用
2.2 maven的jar包下载流程

2.3配置
找到下载jar包的网址:http://www.mvnrepository.com
以MySQL为例


mysql依赖:
mysql
mysql-connector-java
5.1.44
修改“MAVEN_HOME\config”下的setting.xml文件,配置本地仓库。

注意是:“E:/”而非“E:\”

配置阿里云的仓库

引用部分①
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
central
alimaven
central
aliyun maven
http://maven.aliyun.com/nexus/content/repositories/central/
用引用部分① 替代上图红框外面部分

如图一步一步
3.1改变新工作区间的编码方式


3.2搭建环境


点击下拉框旁边的按钮



3.3 maven配置
配置图中画红线部分



如图


此时新建的之前那个文件夹 出现了新的东西

继续操作

加载完之后


注意:没有jar包的 是文件缺损 可以换个网络 重新配
解决项目报错问题:

①添加jdk的插件
buildPath

√去掉前

√去掉后

②解决jdk的问题
下载插件
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.8
1.8
UTF-8
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0modelVersion>
- <groupId>com.cdlgroupId>
- <artifactId>test_mavenartifactId>
- <packaging>warpackaging>
- <version>0.0.1-SNAPSHOTversion>
- <name>test_maven Maven Webappname>
- <url>http://maven.apache.orgurl>
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>3.8.1version>
- <scope>testscope>
- dependency>
- dependencies>
- <build>
- <finalName>test_mavenfinalName>
- <plugins>
-
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.7.0version>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- <encoding>UTF-8encoding>
- configuration>
- plugin>
-
-
- plugins>
-
-
- build>
- project>
配完 保存就开始下载了


web.xml
- <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 Applicationdisplay-name>
- web-app>
③


④
将依赖复制进来:
junit
junit
4.12
test
mysql
mysql-connector-java
5.1.44
javax.servlet
javax.servlet-api
4.0.1
provided
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0modelVersion>
- <groupId>com.cdlgroupId>
- <artifactId>test_mavenartifactId>
- <packaging>warpackaging>
- <version>0.0.1-SNAPSHOTversion>
- <name>test_maven Maven Webappname>
- <url>http://maven.apache.orgurl>
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.44version>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>4.0.1version>
- <scope>providedscope>
- dependency>
- dependencies>
- <build>
- <finalName>test_mavenfinalName>
- <plugins>
-
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.7.0version>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- <encoding>UTF-8encoding>
- configuration>
- plugin>
-
-
- plugins>
-
-
- build>
- project>
此时项目就没有报错了

建一个测试类:
- package com.cdl.demo;
-
- import java.io.IOException;
-
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
-
- @WebServlet("/demo")
- public class DemoServlet extends HttpServlet{
-
- @Override
- public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
- System.out.println("com in ...");
- }
-
- }
结果:

可以看到jar的来源
