zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【maven】什么是坐标(依赖)继承与模块、web项目启动&访问

2023-06-13 09:17:14 时间

目录

2. Maven 基础

2.1 坐标

2.1.0 什么是坐标(依赖)

2.1.1 获得坐标

2.1.2 使用坐标

2.1.3 依赖范围

2.1.4 依赖传递

2.1.5 依赖冲突&调节原则

2.1.6 依赖排除

2.1.7 使用第三方jar包

2.2 继承与模块

2.2.1 概述

2.2.2 分析

2.2.3 实际操作

2.3 web项目启动&访问

2.3.1 打包

2.3.2 创建web项目

2.3.3 启动1:配置tomcat方式

2.3.4 启动2:tomcat插件方式

2.4 jar项目转war项目

2.4.1 需求

2.4.2 步骤

2.4.3 实现

2. Maven 基础

2.1 坐标

2.1.0 什么是坐标(依赖)

  • 坐标:在maven中每一个项目都一个唯一标识,这个标识称为坐标,也称为依赖 dependency 。
  • 坐标组成:组、标识、版本

2.1.1 获得坐标

  • 方式1:通过idea搜索本地仓库

   <dependencies>        <dependency>            <groupId>com.czxy</groupId>            <artifactId>itcast-tools</artifactId>            <version>1.5.7</version>        </dependency>    </dependencies>

  • 方式2:通过官方网站搜索,并让maven自动下载 https://mvnrepository.com/
  • 如果下载失败?
    • 先执行 cleanLastUpdated.bat,删除*.lastUpdated文件
    • 重新下载,idea pom.xml文件 剪切在粘贴

2.1.2 使用坐标

  • 通过坐标完成的使用
    • 在maven项目中,通过坐标可以导入对应的jar包。
    • 可以在本地仓库中,通过坐标获得jar包具体的位置。
  • 使用坐标
    • 情况1:直接使用    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.9</version>        </dependency>    </dependencies>
    • 情况2:先锁定版本,再使用    <!-- 锁定版本   -->    <dependencyManagement>        <dependencies>            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>4.9</version>            </dependency>        </dependencies>    </dependencyManagement> ​    <!--使用-->    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>        </dependency>    </dependencies> ​
    • 情况3:先定义版本,再锁定版本,最后使用 <!-- 版本号   -->    <properties>        <junit.version>4.9</junit.version>    </properties> ​    <!-- 锁定版本   -->    <dependencyManagement>        <dependencies>            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>${junit.version}</version>            </dependency>        </dependencies>    </dependencyManagement> ​    <!--使用-->    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>        </dependency>    </dependencies>

2.1.3 依赖范围

  • 依赖范围:坐标/依赖/jar包,在maven项目中,使用的范围。
    • 此范围包括3种时态:编译时、测试时、运行时。
  • 依赖范围种类:
    • compile ,默认值,在3个时态(编译时、测试时、运行时)中都可以使用。
    • test ,测试,仅在测试时有用,其他时没有此jar。例如:Junit
    • provided,仅在编译时测试时有用。例如:servlet、jsp相关(必须操作的。)
    • runtime,仅在测试时运行时有用。例如:jdbc驱动
    • system,maven仓库之外的jar包。(不建议)
      • 如果有仓库之外的jar包,建议先安装到本地仓库中。

      mvn install:install-file -DgroupId=com.czxy -DartifactId=itcasttools -Dversion=1.5.8 -Dpackaging=jar -Dfile=E:\develop\jars\itcast-tools-1.5.8.jar

    <!--默认值,3个时态都有效-->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>druid-spring-boot-starter</artifactId>            <version>1.1.10</version>            <scope>compile</scope>        </dependency> ​        <!--测试时,有效-->        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.9</version>            <scope>test</scope>        </dependency> ​        <!-- 编译时、测试时,2个时态有效   -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>            <version>2.5</version>            <scope>provided</scope>        </dependency> ​        <!-- 测试时、运行时,2个时态有效   -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.32</version>            <scope>runtime</scope>        </dependency>

2.1.4 依赖传递

  • Maven项目中的依赖关系:直接依赖、间接依赖
    • 直接依赖:A依赖B
    • 间接依赖:A依赖B,B依赖C,A间接依赖C,间接依赖,也称之为传递性依赖。
  • 前提:依赖范围必须是compile

2.1.5 依赖冲突&调节原则

  • 问题:如果两个jar,同时依赖与另外一个jar的不同版本,就可能导致jar冲突问题。这就是传递依赖的Jar版本冲突问题。
  • 依赖冲突的调节原则:
    • 原则1:路径最短优先原则 A --> B --> C 1.1 A --> C 1.0 采纳:C 1.0
    • 原则2:路径长度相同
      • 同一个pom.xml文件,后面声明的会覆盖前面的依赖。(不建议使用)

         <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.10</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.9</version>        </dependency>    </dependencies>

      • 不同pom.xml文件,先声明的依赖,会覆盖后面生命的依赖。

             <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>5.2.10.RELEASE</version>        </dependency> ​        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>5.2.12.RELEASE</version>        </dependency>

2.1.6 依赖排除

  • 如果依赖传递后的版本不是实际需要的版本,需要单独引入,通常将传递的依赖进行依赖排除
        <!-- druid-spring-boot-starter 依赖传递 druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
            <scope>compile</scope>
            <!--  依赖排除 -->
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- 手动移入druid,采用【最短路径原则】-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>

2.1.7 使用第三方jar包

  • maven中央仓库中没有jar包,需要先安装,再使用。
  • 安装 mvn install:install-file -DgroupId=com.czxy -DartifactId=itcast-tools -Dversion=1.5.9 -Dpackaging=jar -Dfile=E:\01_授课资源\02_JavaWeb\01_资料\01_jar\01_xml\itcast-tools-1.5.9.jar

2.2 继承与模块

2.2.1 概述

  • 在项目开发中,一个项目比较大,通常将项目进行拆分,方便项目维护、升级等操作。
  • 拆分方式:
    • 方式1:按照软件分层进行拆分。
      • 例如:common、domain、dao、service、web
    • 方式2:按照模块进行拆分。
      • 例如:common、domain、user、order、... 等
  • maven通过继承与模块对拆分进行支持
    • 创建父项目
    • 为父项目,创建多个子项目。
    • 每一个子项目,就是一个模块
    • 父项目和子项目,通过继承体现父子关系。

2.2.2 分析

  • 创建父项目:day17_maven_parent
  • 创建子项目:
    • day17_common
    • day17_domain
    • day17_dao
    • day17_service
    • day17_web
  • 总结:
    • 父项目的配置:
    • 子项目的配置:

2.2.3 实际操作

  • 创建父项目:day17_maven_parent
  • 创建子项目:
    • day17_common
    • day17_domain
    • day17_dao
    • day17_service
    • day17_web

总结:

  • 项目结构:
  • 父项目的配置:
  • 子项目的配置:
    • 通用配置,引用其他依赖(web --> service --> dao --> domain --> common)    <dependencies>        <dependency>            <groupId>com.czxy</groupId>            <artifactId>day17_service</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>    </dependencies>
    • 除web项目外,其他项目:
    • web项目:
      • 方式1:标准web开发,需要使用 jsp 等资源。需要将项目打包成war包。
      • 方式2:spring boot + RestFul风格,打包方式仍是jar包。

2.3 web项目启动&访问

2.3.1 打包

  • 打包之后
  • 如果我们将jar项目修改成war项目,没有其他操作时,需要在pom.xml文件中添加    <properties>        <!--声明不需要web.xml文件-->        <failOnMissingWebXml>false</failOnMissingWebXml>    </properties>

2.3.2 创建web项目

  • 创建web项目
  • 设置项目坐标

2.3.3 启动1:配置tomcat方式

  • 创建web项目工作目录 webapp,并创建首页 index.html
  • 配置tomcat
  • 部署web项目
  • 启动
  • 访问 http://localhost:8080/day17_web_war/

2.3.4 启动2:tomcat插件方式

  • 创建web项目工作目录 webapp,并创建首页 index.html (已有)
  • 如果没有配置 WEB-INF/web.xml 文件,启动有异常,可以禁用    <properties>        <!--声明不需要web.xml文件-->        <failOnMissingWebXml>false</failOnMissingWebXml>    </properties>
  • 给web项目 pom.xml文件配置tomcat插件

   <build>        <plugins>            <!-- tomcat7插件 -->            <plugin>                <groupId>org.apache.tomcat.maven</groupId>                <artifactId>tomcat7-maven-plugin</artifactId>                <version>2.1</version>                <configuration>                    <port>8080</port>                    <server>tomcat7</server>                </configuration>            </plugin>        </plugins>    </build>

  • 运行对应命令 tomcat7:run

启动问题:Could not find artifact

  • 错误的提示信息
  • 解决方案:将父项目安装到maven私有仓库中
  • 存在问题:代码更新不及时,需要手动频繁的安装

2.4 jar项目转war项目

2.4.1 需求

  • 默认maven创建的是jar项目,特点是该项目基础目录结构完整。
  • 如果进行war开发,是否可以将jar项目转换成war项目

2.4.2 步骤

  • 步骤1:创建maven项目
  • 步骤2:创建项目 maven_java
  • 步骤3:创建webapp/WEB-INF目录,并拷贝web.xml文件
  • 步骤4:添加 WEB模块 支持 File/Project Structure/Modules
  • 步骤5:创建成功后,修改配置项
  • 步骤6:缺失artifact,点击Fix进行修复。
  • 步骤7:配置pom.xml

2.4.3 实现

  • 步骤1:创建maven项目
  • 步骤2:创建项目 maven_java
  • 步骤3:创建webapp/WEB-INF目录,并拷贝web.xml文件
    • 注意:maven_java/web/WEB-INF/web.xml 由步骤4自动创建
  • 步骤4:添加 WEB模块 支持 File/Project Structure/Modules
  • 步骤5:创建成功后,修改配置项
  • 步骤6:缺失artifact,点击Fix进行修复。
  • 步骤7:配置pom.xml    <packaging>war</packaging> ​    <build>        <plugins>            <!-- tomcat7插件 -->            <plugin>                <groupId>org.apache.tomcat.maven</groupId>                <artifactId>tomcat7-maven-plugin</artifactId>                <version>2.1</version>                <configuration>                    <port>8080</port>                    <server>tomcat7</server>                </configuration>            </plugin>        </plugins>    </build>