Maven的scope总结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Maven的scope总结

野生开发者   2022-06-19 我要评论

Maven中的scope总结

Maven中的scope主要有以下6种

接下来分别介绍下这几种scope:

  • compile

不声明scope元素的情况下的默认值;compile表示被依赖包需要参与当前项目的编译,包括后续的测试,运行周期也参与其中,是一个比较强的依赖;打包的时候通常需要包含进去。

  • provided

provided 类型的scope只会在项目的编译、测试阶段起作用;可以认为在目标容器中已经提供了这个依赖,无需在提供,但是在编写代码或者编译时可能会用到这个依赖;依赖不会被打入到项目jar包中。

说到provided,这里就要说到<dependency>下的子标签<optional> ,如果一个依赖的<optional> 设置为true,则该依赖在打包的时候不会被打进jar包,同时不会通过依赖传递传递到依赖该项目的工程;例如:x

依赖B,B由依赖于A(x->B->A),则A中设置<optional> 为true的依赖不会被传递到x中。

这两者的区别在于:

1、<optional>为true 表示某个依赖可选,该依赖是否使用都不会影响服务运行;

2、provided的<scope>在目标容器中已经提供了这个依赖,无需在提供

  • runtime

runtime与compile比较相似,区别在于runtime 跳过了编译阶段,打包的时候通常需要包含进去。

  • test

在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用,不会被打包到项目jar包中,同时如果项目A依赖于项目B,项目B中的test作用域下的依赖不会被继承。

  • system

表示使用本地系统路径下的jar包,需要和一个systemPath一起使用,如下:

<!--引用-->
        <dependency>
            <groupId>xxxx</groupId>
            <artifactId>xxx</artifactId>
            <systemPath>${basedir}/lib/xxxxx.jar</systemPath>
            <scope>system</scope>
            <version>1.4.12</version>
        </dependency>
  • import

import 只能在pom文件的<dependencyManagement>中使用,从而引入其他的pom文件中引入依赖,如:在Spring boot 项目的POM文件中,我们可以通过在POM文件中继承 Spring-boot-starter-parent来引

用Srping boot默认依赖的jar包,如下:

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.BUILD-SNAPSHOT</version>
</parent>

但是,通过上面的parent继承的方法,只能继承一个 spring-boot-start-parent。实际开发中,用户很可能需要继承自己公司的标准parent配置,这个时候可以使用 scope=import 来实现多继承。代码如下:

     <dependencyManagement>
         <dependencies>
             <dependency>
                 <!-- Import dependency management from Spring Boot -->
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-dependencies</artifactId>
                 <version>2.0.1.BUILD-SNAPSHOT</version>
                 <type>pom</type>
                 <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

通过上面方式,就可以获取spring-boot-dependencies.2.0.1.BUILD-SNAPSHOT.pom文件中dependencyManagement配置的jar包依赖。如果要继承多个,可以在dependencyManagement中添加,如:

     <dependencyManagement>
         <dependencies>
             <!-- Override Spring Data release train provided by Spring Boot -->
             <dependency>
                 <groupId>org.springframework.data</groupId>
                 <artifactId>spring-data-releasetrain</artifactId>
                 <version>Fowler-SR2</version>
                 <type>pom</type>
                 <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.1.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Maven中<scope>参数</scope>配置

参数名称具体功能
<scope>compile</scope>默认值,表示当前依赖包要参与当前项目的编译后续测试运行时打包
<scope>provided</scope>当前包只在编译和测试的时候使用,而不再后续的运行和打包的时候不会打包进来
<scope>test</scope>表示当前依赖包只参与测试工作
<scope>runtime</scope>表示当前依赖包只参与运行周期,其他跳过
<scope>system</scope>从参与度和provided一致,不过被依赖项不会从maven远程仓库下载,而是从本地的系统拿。需要systemPath属性来定义路径

解决maven项目中无法打包生成空文件夹的问题

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <excludes>
                                    <exclude>org.apache.flink:force-shading</exclude>
                                    <exclude>com.google.code.findbugs:jsr305</exclude>
                                    <exclude>org.slf4j:*</exclude>
                                    <exclude>org.apache.logging.log4j:*</exclude>
                                </excludes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.lkr.flink.StreamingJob</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们