maven笔记

maven常用命令:

  mvn    -v  查看maven版本

      -compile    编译(要进入到项目名下E:/maven/maven01)

      -test    (要进入到项目名下E:/maven/maven01)

      -package    打包(要进入到项目名下E:/maven/maven01)

      -clean   删除生成的target目录(要进入到项目名下E:/maven/maven01)

      -install   将该项目jar包到本地仓库 中

      

maven的层级结构

  src

    -main

      -java

        -package(com.xiaojiang...)

    -test

      -java

        -packge

  resource(用到才有)

 

用maven的插件自动生成架构

  1.mvn archetype:generate     然后根据提示填写

  2  mvn archetype:generate -DgroupId=组织名,公司网址的反写+项目名(包名)  -DartifactId=项目名-模块名   -Dversion=版本号(1.1.0-SNAPSHOT,快照,还在书写测试阶段)

                -Dpackge=生成的包名

 

 maven的仓库

  本地仓库,在settings.xml中设置<localRepository>G:\m2\repository</localRepository>更改仓库位置

  远程中央仓库,本地仓库中找不到依赖时,就到中央仓库去找

  镜像仓库,可以设置访问镜像仓库,而不访问默认的远程中央仓库。<mirror></mirror>

 

Maven 有以下三个标准的生命周期:

  • clean
  • default(or build)(如compile,test,package)当一个阶段通过 Maven 命令调用时,例如 mvn test,只有该阶段之前以及包括该阶段在内的所有阶段会被执行。
  • site

 

pom.xml中的常用标签

<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>

  <groupId>反向的公司网址+项目名</groupId>
  <artifactId>项目名+模块名</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 第一个0表示大版本号
          第二个0表示分支版本号
          第三个0表示小版本号
          snapshot  快照
          alpha 内部测试
          beta公测
          release稳定
          GA正式发布
   -->
   <!-- 默认jar -->
  <packaging>jar</packaging>
    <!-- 项目描述名 -->
  <name>maven01-hi</name>
  <!-- 项目地址 -->
  <url>http://maven.apache.org</url>
  <!-- 项目描述 -->
    <description></description>
    <!--  许可证-->
    <licenses></licenses>
    <developers></developers>
    <organization></organization>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <!-- 依赖范围 -->
      <scope>test</scope>
      <!-- 设置依赖是否可选 -->
      <optional></optional>
      <!-- 排除依赖列表 -->
      <exclusions>
          <exclusion></exclusion>
      </exclusions>
    </dependency>
  </dependencies>
  <build>
  <!-- 插件列表 -->
      <plugins>
          <plugin>
              <groupId></groupId>
              <artifactId></artifactId>
              <version></version>
          </plugin>
      </plugins>
  </build>
</project>

 

依赖范围<scope>:

  compile   默认,编译、测试、运行都有效

  provide  编译和测试有效

  runtime 测试和运行时有效

  test   测试时有效

  

排除依赖:<exclutions>

  B依赖A,C依赖B,则C也会依赖A。如果不想C依赖A,则在写C依赖B的<dependency>时加上<exclutions>,排除依赖A。

解决依赖冲突

  1.短路优先: C->B->A->X1(jar) C->B->X2(jar) 【C依赖B,B依赖A,A和B都包含同一个不同版本的Jar,则取B的依赖版本x2。(c的pom.xml中不必注明jar坐标)】

  2.先声明先优先 如果路径相同长度相同,则谁先声明,先取谁。 【C依赖A和B,A和B都包含同一个不同版本的Jar,谁依赖在前取谁的依赖版本。】

 

聚合和继承

  聚合:是同时将多个项目进行操作

    创建一个项目,pom文件如下,注意打包方式为pom

    然后clean install,会将三个项目依次打包

<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>

  <groupId>com.hongxing.aggregation</groupId>
  <artifactId>hongxing-agg</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>hongxing-agg</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <modules>
      <module>../hongxing-bge</module>
      <module>../hongxing-nange</module>
      <module>../hongxing-shanji</module>
  </modules>
</project>

 

依赖:

 父项目的pom文件如下(文件中打包方式为pom),

[html] view plain copy
 
  1. <properties>  
  2.         <target.version>2.5.6</target.version>  
  3.     </properties>  
  4.   
  5.     <dependencyManagement>  
  6.         <dependencies>  
  7.             <dependency>  
  8.                 <groupId>your groupId</groupId>  
  9.                 <artifactId>your artifactId</artifactId>  
  10.                 <version>${target.version}</version>  
  11.             </dependency>  
  12.         </dependencies>  
  13.     </dependencyManagement>  


子模块的POM继承这些配置:子模块继承这些配置的时候,仍然要声明groupId和artifactId,表示当前配置是继承于父POM的,从而直接使用父POM的版本对应的资源

 

 子项目的pom

<parent>
    <groupId>com.hongxing.parent</groupId>
  <artifactId>hongxing-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <relativePath>../hongxing-parent/pom.xml</relativePath>
</parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>

    </dependency>
  </dependencies>

 

 

这个可以有效的避免多个子模块使用依赖版本不一致的情况,有助于降低依赖冲突的几率。注:只有子模块配置了继承的元素,才会真正的有效,否则maven是不会加载父模块中声明的元素。

posted on 2018-04-08 16:40  夜的第八章  阅读(153)  评论(0编辑  收藏  举报

导航