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 jar
used in the toolkit I made, then I spring
In the tool project, you need to reference spring-web
the package.
But because we are all spring
projects, 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-web
brought in. Then there will be a reference conflict. As shown in the figure below:
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 scope
is the best way to help us solve this problem.
text
1. what isscope
For example, the following picture, look at scope
the position:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>
2. scope
The concept of
First of all, our maven
project 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
.
scope
Commonly known as dependency range. Through reasonable configuration, it is possible to control the relationship of the current dependency taking effect at different 编译期
times .测试期
运行期
3. scope
Classification of
scope
There are compile
, test
, provided
, runtime
, system
, import
several 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运行期
threeclasspath
. - test : Called the test dependency scope. Only
测试期
valid for classpath, typicallyJUnit
dependencies, 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 canspring-web
add<scope>provided</scope>
tags to the dependencies in the tool project. - runtime : runtime dependency scope. Valid for
测试期
,运行期
classpath. - system : System dependent scope. This dependency
provided
is the same as the dependency scope, but when using this dependency, you need to specifysystemPath
a label and displayjar
the path of the specified dependency. Using this dependency range means finding dependencies from the local disk, notmaven
resolving 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 dependencyjar
to 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 onlydependencyManagement
valid for tags. Its function is to merge the dependencies in the tag into the current目标pom
tag 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:dependencyManagement
pom
dependencyManagement
import
import
<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-starter
a package, because dependencies are configured in its pom mybatis-plus
and the scope is declared, compile
after I introduce the former, I will also introduce the latter into the project, as shown in the figure below, this is called 传递性依赖
. idea
The project dependencies I looked at:
In addition, the dependencies for mybatis-plus-boot-starter
and pom
for 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
compile
mybatis-plus-boot-starter
mybatis-plus
compile
mybatis-plus-boot-starter
编译期
测试期
运行期
classpath
mybatis-plus-boot-starter
compile
mybatis-plus
<scope>
scope
classpath
5. scope
Application Scenarios
Having said so many scope
scopes, 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 + druid
these 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