Maven scope scope details

1. Introduction to scope

One of Maven's philosophy is that convention is greater than configuration, so in maven, many contents have default values, and the default value of scope is compile.

The role of the scope element: Control the scope of use of the dependency element. In layman's terms, it is to control the scope in which the Jar package is loaded and used. This scope includes, compiling, testing, running, and optionally whether to be included in the package.

2. Scope classification

The scope of Scope includes compile, test, provided, runtime, system, and import.

1. compile

This scope is the default dependency scope, which is a relatively strong dependency and applies to all stages.

These dependencie (dependent) jars will be passed to other dependent upper-level projects.

Compile-scoped dependencies are packaged and distributed with the project as required by the runtime.

Scope: Compile, test, run (deploy) all work.

2. test

Indicates that the dependent project only participates in test-related work, and is not required during compilation and runtime. The dependency can only be used when compiling test code or running test code. More typical such as junit.

Test-scoped dependencies are not packaged since they are not required at runtime.

Scope: Only test works

3. provided

provided dependencies are required at compile and test time, but not at runtime.

Because the runtime is invalid, it can prevent conflicts with the jar under tomcat, because the servlet api is provided by the tomcat container, and there is no need to re-introduce maven.

Provided scoped dependencies are not packaged since they are not required at runtime.

Scope: Compilation and testing are valid.

4. runtime

Runtime dependencies are required at runtime and test time, but not at compile time. Compared with compile, the dependent project does not need to participate in the compilation of the project.

For example, in the driver package of jdbc, the compilation of the main code of the project only requires the JDBC interface provided by the JDK, and the specific JDBC driver that implements the above interface is only required when executing the test or running the project.

Runtime-scoped dependencies are packaged as required by the runtime.

Scope: Test, run valid.

5. system

System-wide dependencies are similar to provided, but you must explicitly provide a path to the JAR file in the local system. You need to specify the systemPath disk path. System dependencies are not recommended.
insert image description here
Scope: Test, run valid

3. Summary

The order of dependency scope from strong to weak is: compile>provided>runtime>test

Not recommended due to system dependencies. The following sorting will not be included, if the sorting and provided are side by side

Practical application: In the SpringBoot project, we can extract some common functions to customize a starter, such as cry-oss-spring-boot-starter, which can support Alibaba Cloud, Amozon, minio, etc., in order to avoid components that are too heavy , you can define his <scope> as private, and then introduce specific packages according to requirements in practical applications

The source of the original text, this article only records the knowledge points, invaded and deleted.
https://baijiahao.baidu.com/s?id=1719114849179396100&wfr=spider&for=pc

Guess you like

Origin blog.csdn.net/haohaoxuexiyai/article/details/123823242