一、maven的概述
1、什么是maven
maven是一个项目管理工具,包含项目管理,插件以及目标的逻辑等。maven为我们提供了一系列的执行流程:

2、maven的下载安装
maven的官方网址:http://maven.apache.org/
maven的历史版本下载:https://archive.apache.org/dist/maven/maven-3/
注意:maven是一个使用java写的软件,所以必须依赖java环境。maven下载出来进行解压就可以使用。
解压之后的文件夹目录:
- Bin:存放二进制执行脚本文件
- Boot:类加载器,加载我们的maven进行启动
- Conf:配置文件所在的路径
- Lib:maven必须依赖的一些jar包
3、maven的配置
maven的配置有两步:配置环境变量和修改配置文件


在maven的解压文件夹中,conf目录下有一个settings.xml文件,我们需要在这个文件中进行配置,大家可以直接复制下面的内容到文件中:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.home}/conf/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<!-- 这里的路径是本地仓库的路径 -->
<localRepository>F:\java\jar</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<!-- 这里的URL是阿里云仓库的地址 -->
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
</profiles>
</settings>
4、maven的常用命令
- mvn clean:清理target目录,这个目录包含编译的结果(class文件)
- mvn test:运行测试代码
- mvn compile:编译src/main/java下面的java代码,然后把编译结果放到target目录中
- mvn package:对项目进行打包,包中的内容没有src/test/java下面的java测试代码
5、利用maven工具创建出来的java项目的目录结构
simple
src
main
java:这个目录存放主要的java代码
resources:存放配置文件,这些配置文件只能被src/main/java中的代码使用
test
java:这个目录存放测试的java代码
resources:存放配置文件,这些配置文件只能被src/test/java中的代码使用
pom.xml:配置当前项目的坐标、jar包(依赖)的坐标、maven的一些插件
target:编译、测试、打包等操作的结果存放的地方
利用maven工具创建出来的web项目的目录结构,绝大部分和maven的java项目结构一样,只不过多了一个webapp目录。
二、maven与eclipse的集成
第一步:验证eclipse是否安装有maven的插件
我们需要打开eclipse的window选项中的preferences,搜索maven,如果存在maven选项,就证明已经安装好maven插件了;如果没有maven插件,证明没有安装好maven插件,需要手动安装

第二步:安装eclipse的maven插件
下载maven插件,链接: https://pan.baidu.com/s/1Bu4yRrMyRxMHwF5ukZh6Mw 密码: 9k5b,将连个文件夹放入eclipse的安装目录下,如果提示文件夹已经存在,直接合并就可以了,然后重启eclipse,重复第一步检查maven插件是否安装成功。

第三步:配置maven
首先配置maven的安装路径

然后配置maven的settings.xml的文件路径

三、在eclipse中创建maven项目
1、创建java工程



注意:如果本地仓库中没有这个骨架就会联网到阿里云仓库中进行下载,一定要联网。


2、创建web工程


我们创建完成后,会发现项目中存在一些错误,这是由于maven的包路径错误导致的,我们需要修改存在的问题。

首先在项目上右键选中Properties;






修改完包路径后,可能还存在问题,我们在问题窗口中可能会看到下面的结果,这说明我们还少缺少一个东西。



上面所有的步骤完成后,我们需要更新我们的项目:在项目名上右键 --> Mavne --> Update Project
3、跳过骨架选择创建java项目
我们看到上面的方法步骤都过于麻烦,我们可以选择使用下面的方法进行创建项目。
注意,这时一定要联网,但是如果阿里云的仓库中也没骨架的话,我们需要到中央仓库中进行下载。


4、跳过骨架选择创建web项目


我们创建好项目后,会发现又报错了,查看问题发现,我们web项目中没有web.xml文件,我们需要进行添加,然后将web.xml中的内容添加进去。


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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>01_helloWorld</display-name>
<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>
然后我们web项目就创建好了。
网友评论