In pom4, the scope element is introduced into the dependency element, which is a very important attribute. In Maven projects, jar package conflicts and type conversion exceptions are caused by improper use of scope elements.
The role of the scope element: controls the scope of use of the dependency element. In general, it is to control the scope in which jar packages are loaded and used.
Scope contains five values, which are described as follows:
Compile (Compilation)
Meaning: Compile is the default value. If no scope value is specified, the default value of this element is compile. The dependent project must be involved in the compilation, testing, packaging, and running stages of the current project. Packages usually contain dependent projects.
Provided (provided)
Meaning: the dependent project can theoretically participate in the compilation, testing, running, and other stages, which is equivalent to compile, but exclude is performed in the repackaging stage.
Applicable scenarios: for example, if we are developing a web application, we need to rely on the servlet-api.jar at compilation, but we don't need the jar package at runtime, because this jar package has been provided by the application server, we need to use provided for range modification.
Runtime)
Meaning: The dependent project does not need to be involved in project compilation, but will participate in project testing and running. Compared with compile, the dependent project does not need to be involved in project compilation.
Applicable scenarios: for example, we do not need the jdbc api jar package during compilation, but we need the JDBC driver package during runtime.
Test)
Meaning: The dependent project only participates in the test-related work, including the compilation and execution of the test code.
Applicable scenarios: for example, JUnit testing.
System)
Meaning: The system element is similar to the provided element, but it is obtained from the local system instead of from the maven repository. The systempath element is used to specify the path of the JAR file in the local system. That is to say, the application server provides the jar package at runtime by using the jar package specified by the systempath element during compilation.
Applicable scenario: This is used when a custom basic component service exists in the project. For example:
<dependency> <groupId>org.open</groupId> <artifactId>open-core</artifactId> <version>1.5</version> <scope>system</scope> <systemPath>${basedir}/WebContent/WEB-INF/lib/open-core.jar</systemPath></dependency>
Source Article 77646263
Maven: analyze the meaning of scope in dependency relationships