Maven学习

依赖有效性

  依赖的范围、依赖的有效性
  compile(默认) test provided 

依赖原则:为了防止冲突
  a.路径最短优先原则
  b.路径长度相同:
    i.在同一个pom.xml文件中有2个相同的依赖(覆盖):后面声明的依赖 会覆盖前面声明的依赖 (严禁使用本情况,严禁在同一个pom中声明2个版本不同的依赖)
    ii.如果是不同的 pom.xml中有2个相同的依赖(优先):则先声明的依赖 ,会覆盖后声明的依赖

 

compile

test

provided

编译

×

测试

部署运行

×

×

maven部署web   

    

  通过maven直接部署运行web项目:
    a.配置cargo
    b. maven命令:deploy

    作用: 不用打成war后  再复制war包到tomcat下

 

    <build>    <finalName>打成war包的名字</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.4.9</version>
                <configuration>
                    <container>
                        <containerId>tomcat8-5-30</containerId>
                        <home>本地tomcat地址</home>
                    </container>
                    <configuration>
                        <type>existing</type>
                        <home>本地tomcat地址</home>
                        <!-- 默认值8080 -->
                        <properties>
                            <cargo.servlet.port>8088</cargo.servlet.port>
                        </properties>
                    </configuration>
                </configuration>
                <executions>
                    <execution>
                        <id>cargo-run</id>
                        <phase>install</phase>
                      <!-- 运行 -->
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>                

maven 的父目录 打包方式pom

在父工程的pom.xml中编写依赖:

  <dependencyManagement>
    <dependencies>
      <dependency>

子类:
<!-- 给当前工程 继承一个父工程:1加入父工程坐标gav 2当前工程的Pom.xml到父工程的Pom.xml之间的 相对路径 -->
  <parent>
    <!-- 1加入父工程坐标gav -->
      <groupId>com.zhao.maven</groupId>
      <artifactId>A</artifactId>
      <version>0.0.1-SNAPSHOT</version>
     <!-- 2当前工程的Pom.xml到父工程的Pom.xml之间的 相对路径 -->
    <relativePath>../A/pom.xml</relativePath>
  </parent>

在子类中 需要声明 :使用那些父类的依赖

    <dependency>
    <!-- 声明:需要使用到父类的junit (只需要ga) -->
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>

聚合:

Maven项目能够识别的: 自身包含、本地仓库中的

Maven2依赖于Maven1,则在执行时:必须先将Maven1加入到本地仓库(install),之后才能执行Maven2
以上 前置工程的install操作,可以交由“聚合” 一次性搞定。。。

聚合的使用:

在一个总工程中配置聚合: (聚合的配置 只能配置在(打包方式为pom)的Maven工程中)
  modules


  <modules>
  <!--项目的根路径 -->
  <module>../Maven1</module>
  <module>../Maven2</module>
  </modules>


配置完聚合之后,以后只要操作总工程,则会自动操作 改聚合中配置过的工程  注:<module>的顺序没有要求

注意:clean命令 是删除 target目录,并不是清理install存放入的本地仓库

maven常见命令: 
编译: ( Maven基础组件 ,基础Jar)
  mvn compile --只编译main目录中的java文件
  mvn test 测试
  mvn package 打成jar/war
  mvn install 将开发的模块 放入本地仓库,供其他模块使用 (放入的位置 是通过gav决定)

  mvn clean 删除target目录(删除编译文件的目录)
  运行mvn命令,必须在pom.xml文件所在目录

maven生命周期:

 


  

生命周期和构建的关系:
  生命周期中的顺序:a b c d e
  当我们执行c命令,则实际执行的是 a b c

  生命周期包含的阶段:3个阶段
  clean lifecycle :清理
  pre-clean clean post-clearn

  default lifecycle :默认(常用)

  site lifecycle:站点
  pre-site site post-site site-deploy

再次强调:在pom.xml中增加完依赖后 需要maven - update project

 

通过maven统一jdk版本:
<profiles>
<profile>
  <id>jdk-18</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>
</profiles>

 参考 :https://www.bilibili.com/video/av24885060

posted @ 2020-02-11 22:12  Angry-rookie  阅读(103)  评论(0)    收藏  举报