SoFunction
Updated on 2025-04-10

Maven  Usage of scope attribute

Maven scope attribute

In Maven,scopeAttributes are used to define the behavior of dependencies during different life cycle stages.

scopeThis affects what stages of the dependency will be downloaded and used during the build process, and whether it will be passed to other projects that depend on the current project.

understandscopeThe settings are very important for optimizing the build process and managing dependencies.

Common values ​​and meanings of scope

Maven supports the following commonscopevalue:

compile(default)

  • Corresponding to the entire build life cycle, it means that this dependency is available for compilation, testing, and running.
  • Will be included in the final package.
  • Will be passed to other projects that depend on the current project.

provided

  • Similar tocompile, but it assumes that at runtime it will be provided by other ways in the container or classpath.
  • Generally used for class libraries provided by containers, such as JAR files provided by Servlet containers.
  • Not included in the final package.
  • Not passed to other projects that depend on the current project.

runtime

  • Indicates that this dependency is required during the run and test phases, but not during the compile phase.
  • Will be included in the final package.
  • Will be passed to other projects that depend on the current project.

test

  • Indicates that this dependency is only required during the test compilation and test run phases.
  • Not included in the final package.
  • Not passed to other projects that depend on the current project.

system

  • Similar toprovided, but requires explicitly providing a local path to the JAR file.
  • It is already rarely used and is usually not recommended.

Example explanation

<dependency>
    <groupId></groupId>
    <artifactId>flink-clients</artifactId>
    <version>${}</version>
    <scope>provided</scope>
</dependency>

In this example:

  • groupIdyes, represents the organization or library to which it depends.
  • artifactIdyesflink-clients, indicates the library or module that depends on.
  • versionyes${}, represents the dependency version, a property is used here${}, this property needs to be defined elsewhere in the POM file.
  • scopeyesprovided, means that this dependency is assumed to be provided by a container or other means at runtime.

Use scenarios

  • When the application you develop is running in a container environment (such as Tomcat, Jetty, etc.) and the container already provides certain class libraries (such as JSP API, Servlet API, etc.), you can set the dependencies of these class libraries toprovided
  • If the application you are developing is running in an environment that already contains the Flink client, such as the Flink cluster itself, you canflink-clientsSet asprovided

Things to note

  • useprovidedWhen you are in the runtime, you need to make sure that the corresponding class library is available, otherwise the application will throw it because the necessary class cannot be found.ClassNotFoundException
  • If you are not sure if the runtime environment will provide these class libraries, or if these class libraries may be inconsistent in different environments, it is best not to useprovided, and usecompileorruntime
  • providedApplicable to those class libraries provided by the runtime environment, it reduces the size of the final package while also reducing the risk of transitive dependencies.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.