maven入门(一)

现在基本上很多项目开始使用maven进行构建项目,已经不是直接在lib文件夹下统一放置jar包,所以还是有必要学习掌握下maven的。

针对maven。这篇文章主要介绍一下几点,大家如果都明白了,就可以参照这个思维导图,再复习下,毕竟知识长时间不用,会忘记的。
enter image description here

一、 maven 是什么

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

二、maven的好处

1、由于maven构建的项目是没有jar包的,所以项目的大小上,肯定是maven的项目比较小。
2、jar包统一交给maven管理。
3、maven同样可以进行项目构建。

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

三、maven安装

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

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

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

    <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 here

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

四、 使用maven构建项目

这里我是用eclipse 进行创建的

1、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、开始创建项目
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

五、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

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

猜你喜欢

转载自www.cnblogs.com/zhenghengbin/p/8974605.html