Detailed explanation of scope in Maven dependencies

One of Maven's philosophy is Convention Over Configuration. In Maven's default dependency configuration item, the default value of scope is compile, which is often indistinguishable in the project, so it is directly defaulted. Today, I will sort out the scope of maven.

Classification of scopes

compile

The default is compile, and nothing configured means compile. compile indicates that the dependent project needs to participate in the compilation of the current project. Of course, the subsequent testing and the running cycle are also involved, which is a relatively strong dependency. It usually needs to be included when packing.

test

The scope of test indicates that the dependent project only participates in the work related to the test, including the compilation and execution of the test code. More typical such as junit.

runntime

Runntime means that the dependent project does not need to participate in the compilation of the project, but it needs to participate in the later testing and running cycle. Compared with compile, it just skips compilation. To be honest, in terminal projects (non-open source, enterprise internal systems), it is not very different from compile. For the more common implementations such as JSR×××, the corresponding API jar is compiled, and the specific implementation is runtime. Compile only needs to know the interface is enough. The oracle jdbc driver shelf package is a good example, and the general scope is runntime. In addition, runtime dependencies are usually used in conjunction with optional, and optional is true. I can do it with A, and I can do it with B.

provided

Provided means that it is not necessary to include it when packaging, and other facilities (Web Container) will provide it. In fact, the dependency can theoretically participate in the compilation, testing, running and other cycles. Equivalent to compile, but the action of exclude is done in the packaging phase.

system

In terms of participation, it is also the same as provided, but the dependencies will not be captured from the maven repository, but from the local file system, which must be used with the systemPath attribute.

Dependency transfer of scope

A–>B–>C. The current project is A, A depends on B, and B depends on C. Knowing the scope of B in project A, how do you know the scope of C in A? The answer is: 
when C is test or provided, C is discarded directly, and A does not depend on C; 
otherwise, A depends on C, and the scope of C inherits the scope of B.

 

Below is a picture drawn by nexus. 
Scope calculation when dependencies are passed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326140826&siteId=291194637