[Maven] Maven relies range of scope

One, to understand the role Maven scope dependent range

Maven when compiling the project's main code requires the use of a classspath. There are three classpath, respectively corresponding to the time the main Maven compile the project code, Maven build and test execution time, when actual running Maven project.

  • Compile: compile Ze rely scope if not specified, the default will depend on the scope. Use this dependence range Maven dependency to compile, test, Operation of Three classpath are valid. Such as spring-core dependency, when editing, testing, operation need to use this dependence.

test: testing relies range. Use this dependence range Maven dependency is only valid for the test classpath, not the use of such dependence when compiling the main code or run the project. Such as JUnit dependency, it is only required to compile and test code when running tests

  • provided: to provide a range dependent. Use this dependence range Maven dependency effective for compiling and testing classpath, but not in operation. For example servlet-api, compiling and testing programs need to use the time-dependent, but time is running the project, since the container has been provided, there is no need to introduce Maven repeated again.
  • Range dependent runtime: runtime. Use of this range dependent Maven dependence CLASSPATH effective testing and operation, but not in the main code when compiling. For example, JDBC drivers implementation, project main code compile time, you only need JDK JDBC interface provided you can, but in the implementation of the test or run programs only need to implement to achieve specific JDBC driver JDBC interface.
  • system: system relies range. Exactly the same relationship to the three classpath of the dependent, and dependent on the range provided. But, the use of system dependent range, specify systemPath display element specified file path dependence. Because of this dependency is not resolved Maven repository, and often with native binding systems, may cause irreversible ruins built, and is not recommended. systemPath elements can reference environment variables, such as:

<dependency>
  <groupId>javax.sql</groupId>
  <artifactId>jdbc-stdext</artifactId>
  <version>2.0</version>
  <scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>

二、依赖范围与classpath的关系

Dependent range (scope) Effective compilation classpath Effective test classpath classpath for running effective example
compile Y Y Y spring-core
test - Y - JUnit
provided Y Y - servlet-api
runtime - Y Y JDBC driver implementation
system Y Y - Library files outside the local, Maven repository

Guess you like

Origin www.cnblogs.com/756623607-zhang/p/11373916.html