Maven is a project or dependency management tool. Mostly freshers download each and every jar(s) manually and place it into their respective project. Such things can be overcome using Maven to automate the build and deployment process.
Developers just need to set up POM.xml (Project Object Model), Maven needs the internet to download all mentioned dependencies under the POM XML file (a one-time process until the developer wants to download new jars). You can even write the logic for executing your requirement-specific plugin goals at defined phases in a life cycle.
Using Maven, Multi-level project management made so simple.
If you created a simple basic java project and want to use Maven, you can follow the below steps.
Right Click on the Project → Configure → Convert to maven project.
Maven dependency scope attribute is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime, etc). We will go through it with examples and their explanation.
Maven provides below six scopes, i.e.,
- Compile Scope
- Provided Scope
- Runtime Scope
- Test Scope
- System Scope
- Import Scope
- Transitivity Resolution
Cover Image Credits: Prepared by Author on MS Powerpoint
Maven dependency scope — compile
This is maven default scope. Dependencies with compile
scope is needed to build, test, and run the project.
Scope compile
is to be required in most of the cases to resolve the import statements into your java classes sourcecode.
Gist By Rakshit Shah
Maven dependency scope — system
Dependencies system
are similar to ones with scope provided
. The only difference is system
dependencies are not retrieved from the remote repository. They are present under the project’s subdirectory and are referred from there.
Gist by Rakshit Shah
Maven dependency scope — runtime
Dependencies with maven dependency scope runtime
are not needed to build, but are part of the classpath to test and run the project.
Gist by Rakshit Shah
Maven dependency scope — test
Dependencies with maven dependency scope tests are not needed to build and run the project. They are needed to compile and run the unit tests.
Gist By Rakshit Shah
Maven dependency scope — import
import
scope is only supported on a dependency of type pom
in the dependencyManagement
section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s dependencyManagement
section.
Gist by Rakshit Shah
Maven dependency scope — provided
Maven dependency scope provided is used during build and test the project. They are also required to run, but should not be exported, because the dependency will be provided by the runtime, for instance, by servlet container or application server.
Gist by Rakshit Shah
Maven dependency transitivity resolution
When you include a maven dependency and it has its own other dependencies (i.e. transitive dependencies) then you may want to be clear about the scope of these transitive dependencies as well.
Let’s understand about maven transitive dependencies with a simple table. In this table, if a dependency is set to the scope in the left column, transitive dependencies at the top row will result in a dependency on the scope listed at their intersection.
Hope this information will be helpful to you. Now make a POM.xml file with enough confidence.
Thanks for reading.
Discover more from 9Mood
Subscribe to get the latest posts sent to your email.
0 Comments