Maven scope attribute
In Maven,scope
Attributes are used to define the behavior of dependencies during different life cycle stages.
scope
This 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.
understandscope
The settings are very important for optimizing the build process and managing dependencies.
Common values and meanings of scope
Maven supports the following commonscope
value:
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 to
compile
, 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 to
provided
, 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:
-
groupId
yes, represents the organization or library to which it depends.
-
artifactId
yesflink-clients
, indicates the library or module that depends on. -
version
yes${}
, represents the dependency version, a property is used here${}
, this property needs to be defined elsewhere in the POM file. -
scope
yesprovided
, 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 to
provided
。 - If the application you are developing is running in an environment that already contains the Flink client, such as the Flink cluster itself, you can
flink-clients
Set asprovided
。
Things to note
- use
provided
When 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 use
provided
, and usecompile
orruntime
。 -
provided
Applicable 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.