Where Are Maven Scopes Defined? (Explained with Examples)

Maven Scopes: Where They Come From and Why They Matter

Maven scopes are a powerful tool for organizing and managing dependencies in your Maven projects. They allow you to specify which dependencies are required for your project’s compile, test, and runtime phases, and they can also be used to control the version of a dependency that is used.

In this article, we’ll take a closer look at Maven scopes, and we’ll see how you can use them to improve the organization and maintainability of your Maven projects. We’ll also discuss some of the common pitfalls that you should avoid when using Maven scopes.

So, if you’re interested in learning more about Maven scopes, then read on!

Scope Description Example
compile The compile scope includes all dependencies that are required to compile the project’s source code. compile ‘org.springframework:spring-core:5.0.5.RELEASE’
runtime The runtime scope includes all dependencies that are required to run the project, but not to compile it. runtime ‘org.springframework:spring-web:5.0.5.RELEASE’
test The test scope includes all dependencies that are required to run the project’s tests. test ‘org.mockito:mockito-core:2.18.3’

What is a Maven scope?

A Maven scope defines the visibility of a dependency within a project. Maven scopes are used to control which artifacts are available for use by different parts of a project.

There are six different Maven scopes:

  • compile: The compile scope is the default scope. Dependencies with the compile scope are available for use by all the compilation units of a project.
  • provided: Dependencies with the provided scope are available for use by the compilation units of a project, but are not required to be packaged with the project’s output. This scope is typically used for dependencies that are provided by the runtime environment, such as the Java Runtime Environment (JRE).
  • runtime: Dependencies with the runtime scope are available for use by the runtime of a project, but are not required to be available for compilation. This scope is typically used for dependencies that are only required at runtime, such as database drivers.
  • test: Dependencies with the test scope are only available for use by the test units of a project. This scope is typically used for dependencies that are only required for testing, such as testing frameworks.
  • system: Dependencies with the system scope are available for use by all the compilation units of a project, but are not managed by Maven. This scope is typically used for dependencies that are not available in a Maven repository, such as a local file system or a network share.
  • optional: Dependencies with the optional scope are not required by a project, but may be used by it. This scope is typically used for dependencies that are not essential to the project, but may be useful in some cases.

Where are Maven scopes defined?

Maven scopes are defined in the `` section of a Maven POM file. Each dependency has a `scope` attribute that specifies its scope. For example, the following dependency has the `compile` scope:

“`xml

org.apache.maven
maven-core
3.8.1
compile

“`

Maven scopes can also be defined using the `-Dscope` command-line argument. For example, the following command will install the `maven-core` artifact with the `provided` scope:

“`
mvn install -Dscope=provided
“`

Maven scopes are a powerful tool for controlling the visibility and dependencies of a project. By understanding Maven scopes, you can ensure that your projects are properly configured and that your dependencies are used correctly.

Where Are Maven Scopes Made?

Maven scopes are defined in the pom.xml file. To define a scope, add the scope element to a dependency element. For example, the following dependency element defines a dependency with the compile scope:

“`xml

org.apache.maven
maven-core
3.6.3
compile

“`

The scope element can have one of the following values:

  • compile: The dependency is required for compilation.
  • provided: The dependency is provided by the runtime environment.
  • runtime: The dependency is required for running the application.
  • test: The dependency is required for testing the application.
  • system: The dependency is a system library that is not managed by Maven.
  • optional: The dependency is optional and can be excluded from the build.

How do Maven scopes work?

Maven scopes control how dependencies are included in the build. When a project is built, Maven will only include dependencies that are required for the current build phase. For example, if a project is being built in the compile phase, Maven will only include dependencies that have the compile scope.

The following table shows how Maven scopes are used in different build phases:

| Build Phase | Scope |
|—|—|
| Compile | compile |
| Test | compile, test |
| Package | compile, runtime |
| Install | compile, runtime, provided |
| Deploy | compile, runtime, provided, system |

What are the different types of Maven scopes?

Maven defines six different scopes:

  • compile: The dependency is required for compilation.
  • provided: The dependency is provided by the runtime environment.
  • runtime: The dependency is required for running the application.
  • test: The dependency is required for testing the application.
  • system: The dependency is a system library that is not managed by Maven.
  • optional: The dependency is optional and can be excluded from the build.

The following table provides a more detailed description of each scope:

| Scope | Description |
|—|—|
| compile | The dependency is required for compilation. This is the default scope. |
| provided | The dependency is provided by the runtime environment. This scope is typically used for dependencies that are included in the JDK or the operating system. |
| runtime | The dependency is required for running the application. This scope is typically used for dependencies that are not included in the JDK or the operating system. |
| test | The dependency is required for testing the application. This scope is typically used for dependencies that are only used for testing. |
| system | The dependency is a system library that is not managed by Maven. This scope is typically used for dependencies that are not available in a Maven repository. |
| optional | The dependency is optional and can be excluded from the build. This scope is typically used for dependencies that are not required for all builds. |

Maven scopes are a powerful tool for controlling how dependencies are included in the build. By understanding how Maven scopes work, you can ensure that your projects are built with the correct dependencies.

Q: Where are Maven scopes made?

A: Maven scopes are defined in the pom.xml file. The scope element is used to specify the visibility of a dependency to other projects in the build. The following scopes are available:

  • compile: The dependency is available to all projects in the build.
  • provided: The dependency is available to all projects in the build, but it is not required.
  • runtime: The dependency is only available to projects that are actually running the code.
  • test: The dependency is only available to projects that are running tests.
  • system: The dependency is not managed by Maven. You must manually install the dependency on the system.
  • external: The dependency is not managed by Maven. You must manually add the dependency to the project’s classpath.

Q: How do I change the scope of a dependency?

A: To change the scope of a dependency, you can edit the scope element in the pom.xml file. For example, the following code changes the scope of the junit dependency to test:

“`xml

junit
junit
4.12
test

“`

Q: What is the default scope for a dependency?

A: The default scope for a dependency is compile. This means that the dependency is available to all projects in the build.

Q: Can I use multiple scopes for a dependency?

A: Yes, you can use multiple scopes for a dependency. However, it is important to understand the implications of each scope. For example, if you use the compile and runtime scopes for a dependency, the dependency will be available to all projects in the build, but it will only be required for projects that are actually running the code.

Q: What are the best practices for using Maven scopes?

A: The following are some best practices for using Maven scopes:

  • Use the compile scope for dependencies that are required by all projects in the build.
  • Use the provided scope for dependencies that are required by all projects in the build, but that are not available on the target runtime environment.
  • Use the runtime scope for dependencies that are only required by projects that are actually running the code.
  • Use the test scope for dependencies that are only required by projects that are running tests.
  • Use the system scope for dependencies that are not managed by Maven.
  • Use the external scope for dependencies that are not managed by Maven.

    In this blog post, we have discussed where Maven scopes are made. We have seen that Maven scopes are defined in the element of the tag. We have also seen that there are six different scopes: compile, provided, runtime, test, system, and optional. We have discussed the purpose of each scope and when to use it. Finally, we have seen how to override the default scope of a dependency.

We hope that this blog post has been helpful in understanding Maven scopes. If you have any further questions, please feel free to leave a comment below.

Similar Posts