Maven: analyze the meaning of scope in dependency relationships

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.