Maven项目管理工具

一 Maven简介

Maven是一个项目管理工具。它可以帮助程序员构建工程,管理jar包,编译代码,完成测试,项目打包等等。

1.1 Maven作用

一键构建

我们的项目往往都要经历编译、测试、运行、打包、安装,部署等一系列过程,这些过程称之为构建。通过Maven工具,可以使用简单的命令轻松完成构建工作。

依赖管理

1.2 Maven的下载与配置

maven下载

进入https://maven.apache.org/download.cgi即可完成下载

安装目录

bin:存放maven的命令
boot:存放maven本身的引导程序,如类加载器等
conf:存放maven的配置文件
lib:存放maven本身运行所需的jar包

配置环境变量

1.新建系统变量MAVEN_HOME
变量值是Maven安装目录
2.编辑系统变量Path,添加变量值%MAVEN_HOME%\bin
3.验证安装是否成功,win+R运行cmd,输入mvn -v.

1.3 Maven工程的类型与结构

Maven工程分三种;Pom,Jar,War三种.
Pom工程是逻辑工程,Maven并不会对该类型工程做打包处理,这些工程不包括具体的业务,而是用来整合其他工程。
Jar工程: 普通的java工程,打包时会将项目打成jar包.
War工程;javaWeb工程打包时会将项目打包成war包.

Maven的生命周期

Maven完成项目的构建构成要经历编译、测试、运行、打包、安装,部署等过程,Maven将这些过程规范为项目构建的生命周期。

Maven的常用命令

mvn clean 清除编译的class文件,即删除target目录。
mvn validate 验证项目是否正确
mvn compile 编译maven项目mvn test编译maven项目及运行测试文件
mvn package 编译maven项目及运行测试文件,并打包
mvn install 编译maven项目及运行测试文件并打包,并发布到本地仓库
mvn deploy 部署项目到远程仓库
mvn tomcat7:run 使用tomcat运行项目

1.4 Maven的配置文件

本地仓库的默认位置是${user.dir}/.m2/repository,
${user.dir}表示windows用户目录,我们可以通过修改
${MAVEN_HOME}\conf\settings.xml,修改本地仓库的位置。

配置本地仓库

<settings>中添加如下标签
<--本地仓库路径>
注意斜杠
<localRepository>E:/liulanqi/maven/repository</localRepository>

配置镜像

由于中央仓库访问速度较慢,可以配置镜像代理中央仓库的下载请求。在下的中添加如下标签即可配置镜像:

<mirrors>
  <mirror>      
  <id>nexus-aliyun</id>    
  <name>nexus-aliyun</name>  
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>    
  <mirrorOf>central</mirrorOf>      
</mirror> 

<mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>

</mirrors>

1.5 idea配置maven

一 pom配置文件

放jar包坐标:https://mvnrepository.com 在这里查询jar包坐标也就不用再导入jar包了
pom.XML里导入jar包servlet和jsp的jar包,

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.2.1</version>
      <scope>provided</scope>
    </dependency>

tomcat的依赖 在标签下

 <!-- TOMCAT7-->
        <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.1</version>
          <configuration>
            <port>8888</port>
            <path>/</path>
            <uriEncoding>UTF-8</uriEncoding>
            <server>tomcat7</server>
          </configuration>
        </plugin>

二 Maven测试开发

2.1 测试概述

黑盒测试:不需要写代码,给输入值,看程序能不能输出期望的值
白盒测试:需要写代码 关注程序具体执行流程。
Junit是白盒测试

2.2 Junit使用步骤

引入坐标

   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

在这里插入图片描述
Junit测试方法没有返回值和参数,需要注解@Test
断言 参数是预期结果和实际参数
Assert.assertEquals(3,add);

2.3 @Before @After

@Before 测试方法之前自动执行,我们用来获取资源。
@After测试方法之后自动执行,我们用来释放资源。

三 依赖冲突调节

依赖冲突的产生

依赖a引路依赖c,依赖b引路依赖c ,如果不调节会引路两个依赖c,maven如何调节依赖呢?

3.1最短路径优先原则

路径最短的版本会被使用:假设jar包之间的关系是a》b》c》d(2.0)和e》f》d(1.0)同时引入a和e那么D1.0会被使用因为e到d路径最短.

3.2 最先声明原则

依赖路径相等的前提下,maven调节依赖的原则是顺序靠前的会被使用

3.3排除依赖,锁定依赖

手动调节依赖:

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.12.RELEASE</version>
      <exclusions>  调节依赖
        <exclusion>
          <groupId></groupId>
          <artifactId></artifactId>
        </exclusion>
      </exclusions>
    </dependency>

四 聚合开发

聚合关系

在这里插入图片描述

继承关系

Maven中的继承是针对于父工程和子工程。父工程定义的依赖和插件子工程可以直接使用。注意父工程类型一定为POM类型工程。

五 聚合案例

5.1 搭建父工程

不用选web模板搭建 夫工程就是放一些依赖

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--JDBC驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!--Jstl -->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
        <!-- tomcat插件-->
        <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
            <port>8080</port>
            <path>/</path>
            <uriEncoding>UTF-8</uriEncoding>
            <server>tomcat7</server>
            <systemProperties>
                <java.util.logging.SimpleFormatter.format> %1$tH:%1$tM:%1$tS %2$s%n%4$s: %5$s%6$s%n</java.util.logging.SimpleFormatter.format>
        </systemProperties>
        </configuration>
    </plugin>
</plugins>

    </build>

5.2 搭建dao模块

在Maven_demo里面创建dao模块domain包是写实体类(get和set方法还有构造方法),dao成是写查询
test是测试
在这里插入图片描述

public class StudentDao {
    //查询所有学生
    public List<Student> findAll() throws Exception {
    //读取配置文件
        Properties properties = new Properties();
        //获取类加载器
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("db.properties");
        properties.load(is);

        String url=properties.getProperty("jdbc.url");
        String username=properties.getProperty("jdbc.username");
        String password=properties.getProperty("jdbc.password");

        //查询数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select  * from student ");
        //处理结果集合
        List<Student> students = new ArrayList<>();
        while (resultSet.next()) {
            int id =resultSet.getInt("id");
           String name =resultSet.getString("name");
           String sex =resultSet.getString("sex");
           String saddress =resultSet.getString("address" );
            Student student = new Student(id, name, sex, saddress);
            students.add(student);

        }

        //释放资源
        resultSet.close();
        statement.close();
        connection.close();
        return students;
  }

test测试

public class StudentDaoTest {
    @Test
    public void testFinAll() throws Exception {
        StudentDao studentDao = new StudentDao();
        List<Student> all = studentDao.findAll();
        all.forEach(System.out::println);
    }
}

5.3 搭建service模块

在这里插入图片描述
先在pom。xml里面加入jar包,可以使用Maven_demo_dao方法

   <!--依赖Dao -->
    <dependencies>
        <dependency>
            <groupId>com.itbaizhan</groupId>
            <artifactId>Maven_demo_dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

src/java

public class StudentService {
    private StudentDao studentDao = new StudentDao();
    //查询所有学生
    public List<Student> findAllStudent() throws Exception {
        return studentDao.findAll();
    }
}

src/test

public class StudentServiceTest {
    @Test
    public void testFindAllStudent() throws Exception {
        StudentService studentService = new StudentService();
        List<Student> allStudent = studentService.findAllStudent();
       allStudent.forEach(System.out::println);
    }
}

5.4 搭建web模块

package com.itbaizhan.servlet;

import com.itbaizhan.domain.Student;
import com.itbaizhan.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/allStudent")
public class FindAllStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService studentService = new StudentService();
        try {
            List<Student> allStudent = studentService.findAllStudent();
            req.setAttribute("allStudent",allStudent);
        } catch (Exception e) {
            e.printStackTrace();
        }
      req.getRequestDispatcher("allStudent.jsp").forward(req,resp);
    }
}

JSP页面


<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>所有学生</title>
</head>
<body>
<table width="500px" align="center" border="1" cellspacing="0" cellpadding="0">

    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>性别</th>
        <th>地址</th>
    </tr>
    <c:forEach items="${allStudent}" var="student">
        <tr>
            <td>${student.id}</td>
            <td>${student.name}</td>
            <td>${student.sex}</td>
            <td>${student.address}</td>
        </tr>
            </c:forEach>


</table>

</body>
</html>

5…5 运行项目

5.6 依赖传递失效以及解决方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值