java-web3-IDEA使用MAVEN项目管理工具

5.MAVEN

作用:自动导入和配置项目所需jar包的项目管理工具

5.1 MAVEN项目管理工具
  • 核心思想:约定>配置,有约束别违反
  • 规定如何编写java,提现在目录结构上,按此规范编写
5.2 MAVEN下载安装配置

在这里插入图片描述

  • 测试是否安装成功mvn -v
    在这里插入图片描述

  • 配置镜像:加速下载

  • 国内建议使用阿里云镜像

  • D:\maven\apache-maven-3.8.1\conf\settings.xml

<mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
	<mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
  </mirrors>
  • 建议url将http改为https,解决后面springboot下载包不完整的问题

配置本地仓库:

<localRepository>E:\m2\repository</localRepository>
5.3 IDEA中使用MAVEN
  • 启动IDEA

  • 创建一个MavenWeb项目,使用原型,普通的项目什么也不用勾选
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 等待项目初始化完毕,自动导入包
    在这里插入图片描述

  • 项目构建成功
    在这里插入图片描述

  • 观察Maven仓库中的多了的jar包

  • idea配置Maven。注意:经常会出现项目自动创建完后,这个mavenhome会使用默认的仓库,需要手动改为自定义的仓库路径
    在这里插入图片描述

  • 可以✓,但一般不✓
    在这里插入图片描述
    在这里插入图片描述

  • idea使用配置Maven结束

5.4 使用MAVEN创建普通项目
  • 不用✓原型
    在这里插入图片描述
    在这里插入图片描述
  • idea将文件夹标记为属性目录,如将:java标记为sources root 才能创建class
  • 标记方式1:
    在这里插入图片描述
  • 标记方式2:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
5.6 pom.xml文件
  • pom.xml时maven的核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--maven版本和头文件-->
<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>
<!--项目的GAV配置-->
  <groupId>com.zk</groupId>
  <artifactId>web02</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--打包方式:
  java应用:jar包
  javaweb应用:war包-->
  <packaging>war</packaging>

  <name>web02 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
<!--配置-->
  <properties>
    <!--项目的构建编码-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--项目的编译版本-->
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
<!--项目依赖的jar-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
<!--项目构建用的插件-->
  <build>
    <finalName>web02</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
  • pom.xml配置后的结果显示
    在这里插入图片描述

  • maven的高级之处在于,帮你导入这个jar包依赖的其他jar包

  • 不建议直接用maven创建web工程(直接创建web项目不干净),用普通工程转换为web工程

  • **问题:**由于maven中约定大于配置问题,我们可能会遇到自己写的资源文件无法被导出问题,以至于在运行之后target文件里没有我们自己写的的配置文件。

<build>
<resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>
  • 修改编辑器内容颜色
    在这里插入图片描述
5.7 idea操作项目jar包联系目录树
  • 体现了jar包的联系关联图
    在这里插入图片描述
5.8 遇到的问题
  • maven 3.6.2无法导入jar包,具体原因时jdk和maven版本不兼容,先将jdk环境问题,将jdk12->低版本,将maven 3.6.2新版本降级

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p58kz4wo-1653489918274)(…/…/typora-user-images/image-20220509232358617.png)]

  • 显示日志
    在这里插入图片描述在这里插入图片描述
  • tomcat闪退,闪退时jdk或jre环境变量设置的不对.或者在startup.bat文件中加pause命令
  • idea每次要重复配置maven。需要在idea的全局默认配置中进行配置。
  • idea2020版本idea在file->New Project Setting在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • maven项目中tomcat无法配置
  • idea中maven默认web项目中的web.xml版本问题
    在这里插入图片描述
  • 替换为tomcat下webapps中默认项目的root的web.xml版本,使环境达到最优
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值