Detailed explanation of powerful scope tags in maven

Detailed explanation of powerful scope tags in maven

Purpose of this article

  Continuing from the summary and understanding of the version number version of maven
  when I was packaging the toolkit , I found some dependencies that must be used in the tool code, such as the method jarused in the toolkit I made, then I springIn the tool project, you need to reference spring-webthe package.
  But because we are all springprojects, like this package will definitely be in the sub-project in the future, for example, through reference spring-boot-starter-web, the package will be automatically spring-webbrought in. Then there will be a reference conflict. As shown in the figure below:
insert image description here
   Generally, in this case, there will be no problem. The basic development tools and compilers will automatically give priority to one of them. (Depending on the mediation mechanism, I will write about this later)
   But as a qualified code farmer, although this kind of thing is not a big problem, messy references violate my rigorous original intention. Therefore, the protagonist of this article scopeis the best way to help us solve this problem.

text

1. what isscope

   For example, the following picture, look at scopethe position:

<dependency>
   <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
    <scope>runtime</scope>
</dependency>

2. scopeThe concept of

  First of all, our mavenproject dependencies are actually divided into three periods, 编译期, 测试期, 运行期, that is, when compiling the code, we need to use one set classpath; when testing, we need another set of dependencies classpath; and when the project is running, we need another set of dependencies. sets of dependencies classpath.

  scopeCommonly known as dependency range. Through reasonable configuration, it is possible to control the relationship of the current dependency taking effect at different 编译期times .测试期运行期

3. scopeClassification of

   scopeThere are compile, test, provided, runtime, system, importseveral kinds.

  • compile : called compile dependency scope. If the referenced dependency does not specify a scope, that dependency scope will be used by default. Using this dependency range means that it is valid for the above mentioned 编译期, 测试期, and 运行期three classpath.
  • test : Called the test dependency scope. Only 测试期valid for classpath, typically JUnitdependencies, only needed when compiling test code (src/test) and running tests.
  • provided : The dependency scope is provided. Valid for 编译期, 测试期classpath. Using this dependency range means that the current dependency will be referenced by the reference project by default in the future, so there is no need to reference it more. In the situation introduced at the beginning of this article, you can spring-webadd <scope>provided</scope>tags to the dependencies in the tool project.
  • runtime : runtime dependency scope. Valid for 测试期, 运行期classpath.
  • system : System dependent scope. This dependency providedis the same as the dependency scope, but when using this dependency, you need to specify systemPatha label and display jarthe path of the specified dependency. Using this dependency range means finding dependencies from the local disk, not mavenresolving them from the warehouse. Pay attention when using this dependency, because you are looking for dependencies from your own local disk, so if multiple people are developing together, it will be troublesome, and you have to copy that dependency jarto the same path on the other machine. Therefore it is not recommended to use this dependency scope.
  • import : import dependency scope. This dependency has no effect on the three periods mentioned above classpath. Dependencies of this scope are only dependencyManagementvalid for tags. Its function is to merge the dependencies in the tag into the current 目标pomtag for use in the referenced project. Due to the particularity of dependencies, generally when you see the scope, it will exist , that is, it points to the module whose packaging type is . For example, the following:dependencyManagementpomdependencyManagementimportimport<type>pom</type>pom
<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.4.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
</dependencyManagement>

4. Transitive dependencies

  When a project references mybatis-plus-boot-startera package, because dependencies are configured in its pom mybatis-plusand the scope is declared, compileafter I introduce the former, I will also introduce the latter into the project, as shown in the figure below, this is called 传递性依赖. ideaThe project dependencies I looked at:
insert image description here
  In addition, the dependencies for mybatis-plus-boot-starterand pomfor are as follows:   that is, it is declared , so for it, because it is declared , it means that it is valid in the three types of , and , and our project is referencing , the same is used . This will allow our project to have both .    If we modify different values, we will see different dependency effects. For specific effects, refer to the figure below in "Maven Combat":    Therefore, the scope of dependencies not only controls the relationship between the three dependencies, but also affects the dependency transmission.mybatis-plus
insert image description here
compilemybatis-plus-boot-startermybatis-pluscompilemybatis-plus-boot-starter编译期测试期运行期classpathmybatis-plus-boot-startercompilemybatis-plus
<scope>
insert image description here
scopeclasspath

5. scopeApplication Scenarios

  Having said so many scopescopes, general development may not care at all, but if you want to be more rigorous, you need to consider these to ensure that the dependency management of the project is more standardized.
   Because every company has an architect, the architect is responsible for the upgrade and transformation of the basic framework, and the things he does will be packaged into several dependent Jar packages in the future, put them on the company's private server, and be used directly by reference projects in the future.
   For example, it is agreed that the project adopts springboot + mybatis-plus + lombok + shiro + druidthese different versions, and these different versions should not be referenced by each project. The architect should agree on the version, make the basic configuration into several Jars, and set these related dependencies to (if not written, the default is <scope>compile</scope>compile ), so that each subproject does not need to reference these dependencies separately.
   For some individual packages, such as reference projects that will definitely be referenced in the future, the dependency jar package can be set to <scope>provided</scope>, for example, the package mentioned at the beginning spring-web.

Why is the structure of    the next maven project like this

Guess you like

Origin blog.csdn.net/wohaqiyi/article/details/119631500
Recommended