Maven pom 文件一点简单基础的扩展知识

z4zr 2019-04-20 AM 2857℃ 0条

在玩Java、Kotlin、Scala的圈子里,gradle隐隐坐势,不过maven仍是主流,这里只简单谈下<dependencies>相关的东西,用简单的语言简单描述,简单的去理解即可。最基本的:

    <dependencies>
        <dependencie>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>xxx-xxx</artifactId>
            <version>xxxx</version>
        <dependencie/>
    <dependencies>

只是道这些是远远不够的,如何处理好依赖间的关系,如果避免在打包时引入无用的包,这些都需要考虑,写程序同样是艺术,每一个细节都很重要,一个个精良巧妙的设计才是艺术的根本,仅仅为了完成任务和功能而堆砌的东西,毫无意义。

optional

optional表示可选依赖,其值为boolean型,默认为false,如果设定为true,当其他项目引用当前项目时,显式引入xxx-xxx的依赖时才会依赖xxx-xxx,否则不会引入xxx-xxx。常常用于避免/规避license许可、避免冲突/兼容性问题、也可用于避免非必要的依赖传递减少jar包体积。

        <dependencie>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>xxx-xxx</artifactId>
            <version>vvvv</version>
            <optional>true</optional>
        <dependencie/>

scope

scope用于控制依赖使用的范围:

  • compile(默认)编译阶段有效,测试阶段有效(打包会带上)
  • provided 编译阶段有效,不会被打包,运行时不可用,仅当运行环境中提供时才被使用
  • runtime 编译阶段不需要,运行时需要(打包会带上)
  • test 测试编译阶段和测试运行阶段可用
  • system 显式提供一个本地文件系统中的jar文件路径,范围同compile(打包会带上)
  • import 用于<packaging>pom</packaging>而不是jar,只使用在<dependencyManagement>下的<dependencie/>中,且需声明<type>pom</type>

scopesystem时需提供<systemPath>指定jar包路径,一般不推荐使用,一些特殊来源jar包不放在仓库中,jar包名称与artifactId并不要求必须相同。

<type>不多解释:jar(默认)、war、ear、pom

        <dependencie>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>xxx-xxx</artifactId>
            <version>vvvv</version>
            <scope>system</scope>
            <systemPath>/opt/repo/crypto-xxx/****.jar</systemPath>
        <dependencie/>

classifier

classifier是可选的,它可以是任意的字符串,其内容会附加在版本号后面。下面的例子表示我们将依赖的是这个依赖关系的vvvv-aaaa版本,而不是vvvv版本。常见用于区分适配不同的JDK版本、底层依赖版本、不同功能的包(source、javadoc、core等)、内部版本等。

即实际依赖的是xxx\xxx\xxx\xxx-xxx\vvvv目录下的xxx-xxx-vvvv-aaaa.jar而不是xxx-xxx-vvvv.jar

        <dependencie>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>xxx-xxx</artifactId>
            <version>vvvv</version>
            <classifier>aaaa</classifier>
        <dependencie/>

通过maven-jar-plugin打包时也可使用

<packaging>jar<packaging>
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
        <!-- core额外的修饰,区分同一版本jar包的不同类型,用法很灵活 -->
                <classifier>core</classifier>
                <includes>
                  <include>**/com/cloud/</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

execution

execution用于排除当前依赖的项目中对某依赖项的依赖,多用于处理依赖冲突(如当前项目依赖的A、B依赖了不同版本的C,由于依赖路径长度的问题选择了一个较低的版本,导致A无法正常使用,此时需要对B进行依赖排除以试图解决依赖冲突问题)

    <dependency>
        <groupId>xxx.xxx.xxx</groupId>
        <artifactId>B</artifactId>
        <version>vvvv</version>
        <exclusions>
            <exclusion>
                <groupId>xxx.xxx.xxx</groupId>
                <artifactId>C</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

relativePath(parent标签中,跟dependencie无关,只是因为常用)

relativePath是可选的,如果relativePath指定了地址,maven会优先搜索这个地址查找该依赖(pom.xml),后搜索本地和远程仓库。
当relativePath设定为空时即<relativePath/>将始终从仓库获取,不从本地路径获取

    <parent>
        <groupId>xxx.xxx.xxx</groupId>
        <artifactId>xxx-xxx</artifactId>
        <version>xxxx</version>
        <relativePath>../parent/<relativePath>
    <parent/>

相关内容出处:pom.xml中的classifier标签有什么作用

标签: maven

非特殊说明,本博所有文章均为博主原创。

评论啦~