java基础-构建工具mvn-day20

发布时间 2023-10-23 20:50:29作者: jack-chen666

1. 初识mvn

mvn是一个项目构建工具 idea里面内置ant,已经基本被maven取代
官网:https://maven.apacher.org
核心文件pom.xml project object model
面向的是项目

idea内置mvn的位置
D:\02-soft_setup\01-idea\ideaIU-2019.2.3.win\plugins\maven\lib\maven3
将conf里面的setting 复制到user/.m2目录

setting/build_tools/maven

初次使用maven user/.m2目录不存在的解决

如何解决

环境变量PATH添加 maven的bin目录

cmd 输入
mvn help:system
注:可能运行较慢 可以等配置完镜像源 在运行 会快很多

什么是仓库


每个jar包都有唯一的坐标

远程仓:
中央仓默认是apache提供
访问一下中央仓
mavenreposity.com -->mybatis

私服仓库--本地局域网 公司内部搭建的仓库

本地仓库 --用户缓存远程下载的jar

如何配置本地仓

什么是镜像仓库?

远程的apache仓库 用国内的镜像代替 例如 aliyun镜像仓

如何配置 国内的镜像仓
mirrors标签添加

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

maven关于jdk的配置

    <profile>
      <id>jdk-1.8</id>
      <activation>
      <activeByDefault>true</activeByDefault>
      <jdk>1.8</jdk>
      </activation>
      <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
      </properties>
    </profile>

检查一下 是否完成以下配置

mvn工程的类型

2. 用maven创建工程

new project 不勾选模板

这三个参数其实就是maven里面的坐标
GroupId AtriFactId Version

初次创建的工程会下载jar包

创建好的工程:

各个文件夹的作用

idea右边的maven插件 双击 install

在本地仓respority目录里面查看是否有jar包打出


3. maven工程 之间的关系

依赖 继承 聚合

依赖关系 以如何 注入依赖?

pom.xml中增加坐标 自动依赖

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>
    </dependencies>

不同的版本 自动解决jar包冲突

依赖的传递

将demo工程
clean -- install 重新打jar包

创建项目2

将demo1的坐标直接复制到demo2的dependency

    <groupId>com.msb</groupId>
    <artifactId>MavenDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

点击自动导入

demo1 已经mybatis自动的依赖 添加

依赖的两个原则
最短路径原则 最先声明原则

如何排除掉demo1里面的mybatis

    <dependencies>
        <dependency>
            <groupId>com.msb</groupId>
            <artifactId>MavenDemo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

mybatis自动消失

依赖的范围:
compile provied runtime system

MavenDemo2是MavenDemo工程的子工程 如何关联

    <parent>
        <groupId>com.msb</groupId>
        <artifactId>MavenDemo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../MavenDemo</relativePath>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
    </dependencies>

4. 父子 mvn工程

父工程

<?xml version="1.0" encoding="UTF-8"?>
<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>

<!--定义为pom项目-->
    <packaging>pom</packaging>

    <groupId>com.msb</groupId>
    <artifactId>ParentDemoProject</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--版本定义-->
    <properties>
        <mybatis.version>3.5.4</mybatis.version>
        <spring-core.version>5.1.11.RELAESE</spring-core.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring-core.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring-core.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>


</project>

ChildProj自动增加dependency

5. mvn常见的插件

  1. 编译器
    修改项目的pom.xml 不同的子项目 用的JDK版本不一样 这个场景很少见

  2. 资源拷贝
    只有放在src/main/resource下面的文件
    打包后才会在target/class下面放着

如何把非resource 也打包到classes目录中

6. tomcat插件

new project MavenWarDemo01

点击自动导入

配置tomcat插件
注意不要放错了位置

<plugin>
        <!--tomcat插件-->
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8080</port>
          <path>/</path>
        </configuration>
      </plugin>

启动:

浏览器访问:http://localhost:8080/index.jsp

maven 命令 clean与package的区别