maven总结二: 常用标签及属性

本文为博主原创,未经允许不得转载

 目录:

  1. maven 依赖属性:groupId、artifactId、version

  2.插件执行: execution,id ,phase,goals,configuration

  3.聚合: <modules>

  4.可选依赖:<optional>

  5.排除依赖:<exclusions>

  6.依赖范围 : <scope>, <systemPath>

  7.依赖管理:<dependencyManagement> 

  8.项目属性: <properties>

  9.maven 依赖引用属性:type

  10.maven 默认的属性

1. maven 依赖属性:groupId、artifactId、version

  插件与普通jar 包一样包含 一组件坐标定位属性即:

  groupId、artifactId、version,当使用该插件时会从本地仓库中搜索,如果没有即从远程仓库下载

<!-- 唯一定位到dependency 插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>

2.插件执行: execution,id ,phase,goals,configuration

    execution 配置包含一组指示插件如何执行的属性:

    id 执行器命名

    phase:在什么阶段执行?

    goals:执行一组什么目标或功能?

    configuration:执行目标所需的配置文件?

    # 将插件依赖拷贝到指定目录

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>              
          <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <excludeTransitive>true</excludeTransitive> </configuration> </execution> </executions> </plugin>

3.聚合: <modules>

    指将多个模块整合在一起,统一构建,避免一个一个的构建。聚合需要个父工程,然后使用 <modules> 进行配置其中对应的是子工程的相对路径

<modules>
    <module>tuling-client</module>
    <module>tuling-server</module>
</modules>

4.可选依赖:<optional>

    可选依赖表示这个依赖不是必须的。通过在 <dependency> 添  <optional>true</optional> 表示,默认是不可选的。可选依赖不会被传递。

5.排除依赖:<exclusions>

    即排除指定的间接依赖。通过配置 <exclusions> 配置排除指定组件

<!-- 排除指定项目 -->
<exclusions>
    <exclusion>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
    </exclusion>
</exclusions>

6.依赖范围 : <scope>, <systemPath>

    junit 这个组件 我们只有在运行测试用例的时候去要用到,这就没有必要在打包的时候把junit.jar 包过构建进去,可以通过Mave 的依赖范围配置<scope>来达到这种目的。maven 总共支持以下四种依赖范围:

    compile(默认): 编译范围,编译和打包都会依赖。

    provided:提供范围,编译时依赖,但不会打包进去。如:servlet-api.jar

    runtime:运行时范围,打包时依赖,编译不会。如:mysql-connector-java.jar

    test:测试范围,编译运行测试用例依赖,不会打包进去。如:junit.jar

    system表示由系统中CLASSPATH指定。编译时依赖,不会打包进去。配合<systemPath> 一起使用。

    system 除了可以用于引入系统classpath 中包,也可以用于引入系统非maven  收录的第三方Jar,做法是将第三方Jar放置在 项目的 lib 目录下,然后配置 相对路径,但因system 不会打包进去所以需要配合 maven-dependency-plugin 插件配合使用。   

<!-- system 的通常使用方式-->
<dependency>
          <groupId>com.sun</groupId>
          <artifactId>tools</artifactId>
          <version>${java.version}</version>
          <scope>system</scope>
          <optional>true</optional>
          <systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>

<!-- system 另外使用方式 ,将工程内的jar直接引入 -->
<dependency>
    <groupId>jsr</groupId>
    <artifactId>jsr</artifactId>
    <version>3.5</version>
    <scope>system</scope>
    <optional>true</optional>
    <systemPath>${basedir}/lib/jsr305.jar</systemPath>
</dependency>
<!-- 通过插件 将system 的jar 打包进去。 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependencies</id> <phase>compile</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory> <includeScope>system</includeScope> <excludeGroupIds>com.sun</excludeGroupIds> </configuration> </execution> </executions> </plugin>

7.依赖管理:<dependencyManagement> 

    通过继承的特性,子工程是可以间接依赖父工程的依赖,但多个子工程依赖有时并不一至,这时就可以在父工程中加入 <dependencyManagement> 声明该功程需要的JAR包,然后在子工程中引入。子工程中引入时可省略 <version> 的标签版本

    通过 <dependencyManagement> 可实现在在外面的父pom 中统一管理所有依赖的版本。

<!-- 父工程中声明 junit 4.12 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<!-- 子工程中引入 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
</dependency>

8.项目属性<properties>:

    通过 <properties> 配置 属性参数,可以简化配置。

    示例:

   <properties>
        <java.version>1.8</java.version>
        <com.alibaba.version>1.8</com.alibaba.version>
    </properties>

  <!-- s{}引用 propertys 中定义的属性 -->  
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${com.alibaba.version}</version>
        </dependency>
    </dependencies>

9.maven 依赖引用属性:type

  maven 引入依赖,默认使用的 type 属性为 jar 类型。当需要引用很多依赖并进行统一管理时,则需要在 dependencyManagement 中声明引用的依赖版本。maven 提供了 引用类型type 为 pom 类型的依赖,这种依赖可以则可以直接使用该依赖中声明的所有jar的依赖管理。如spring-boot,spring-cloud都提供了这样的依赖支持:

  示例:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

  子pom 使用父pom 依赖,则可以直接使用,因为其在上面的pom 内部已经声明了引用的版本等信息:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

10.maven 默认的属性

${basedir}               项目根目录  
${version}              表示项目版本;  
${project.basedir}          同${basedir};  
${project.version}          表示项目版本,与${version}相同;  
${project.build.directory}       构建目录,缺省为target  
${project.build.sourceEncoding}    表示主源码的编码格式;  
${project.build.sourceDirectory}    表示主源码路径;  
${project.build.finalName}        表示输出文件名称;  
${project.build.outputDirectory}     构建过程输出目录,缺省为target/classes 

  idea 中配置maven编译打包忽略Test扫描的命令 : mvn clean install -DskipTests -f pom.xml

原文地址:https://www.cnblogs.com/zjdxr-up/p/15225540.html