Maven - Maven External Dependencies - maven tutorial
What is External Dependencies in Maven?
- External Dependencies template is used to capture external dependencies, which are tasks, events, or deliverables that are outside of the Project Manager's control (performed by someone not on the project team) that must happen before the project can progress.
- If dependency is not available in any of remote repositories and central repository. Maven provides answer for such scenario using concept of External Dependency.
learn maven tutorial - maven dependency - maven example
Example for Creating Project section in Maven.
- Add lib folder to src folder
- Copy any jar into the lib folder. We've used ldapjdk.jar, which is a helper library for LDAP operations.
Now our project structure should look like following:
learn maven tutorial - maven dependency - maven example
- Here you are having your own library specific to project, which is very usual case and it can contain jars which may not be available in any repository for maven to download from.
- If your code is using this library with Maven then Maven build will fail because it cannot download or refer to this library during compilation phase.
- To handle the situation, let's add this external dependency to maven pom.xml using following way.
maven tutorial tags : apache maven , maven repository , maven central
pom.xml
maven tutorial tags : apache maven , maven repository , maven central
Managing External Dependencies in Maven
- All the basic list of dependencies in maven is handled by the maven repository at which a project downloads the required dependencies. But, there will be certain scenario at which some particular dependencies may not be available in the maven remote and central repositories. Maven still answers this scenario by providing the feature of external dependency management.
- An external dependency can be such as sqljdbc.jar or log4j. In maven, any external dependencies can be easily configurable as other dependencies in the pom.xml file.
- So, let us see a simple example to add sqljdbc.jar as an external dependency into a maven project.
- Create a folder called lib in the maven project.
- Download the sqljdbc.jar and put it inside the lib folder.
- Now, open the pom.xml of the project and add a new dependency as shown below:
- Specify groupId as name of the jar file
- Specify artifactId as name of the jar file
- Specify the scope of the system
- Specify the relative path of the jar file to project location.
Dependency Scope
- Dependency scope in maven is classified into 5 different types. Each of these scope controls the dependencies that are available in the class path and dependencies that are included in the application.
Dependency Scope | Description |
---|---|
compile | This is the default scope for any external dependency in maven. These dependencies are made available in the classpath and they are packaged well. |
provided | These dependencies are expected to be provided by the JDK or a container. The best example is the servlet api. These dependencies are made available on the classpath but not at runtime. Also, they are not packaged. |
runtime | These are the dependencies which are not required for the compilation, but required to execute and test the system. E.g. JDBC API |
test | These dependencies are required only during the test compilation and execution phase. |
system | This scope is similar to “provided”. The only thing is that developer need to explicitly provide the path of the jar file on the local file system. |