![]() When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared. Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. To address this, Maven allows you to exclude specific dependencies. For example, a certain older jar may have security issues or be incompatible with the Java version you're using. Since Maven resolves dependencies transitively, it is possible for unwanted dependencies to be included in your project's classpath. Your project will have to include an explicit dependency on the specific driver for the one database it does use. X2 can declare these dependencies as optional, so that when your project declares X2 as a direct dependency in its POM, all the drivers supported by the X2 are not automatically included in your project's classpath. However your project only uses one specific database and doesn't need drivers for the others. All of these dependencies are needed at compile time to build X2. Each supported database requires an additional dependency on a driver jar. It supports many databases such as MySQL, PostgreSQL, and several versions of Oracle. ![]() Suppose there is a project named X2 that has similar functionality to Hibernate. You need to declare it directly in the POM of Project X for B to be included in X's classpath. Project-B is not included in the classpath of Project-X. When another project (Project-X) declares Project-A as a dependency in its POM, the optional nature of the dependency takes effect. How do I use the optional tag?Ī dependency is declared optional by setting the element to true in its dependency declaration: They prevent problematic jars that violate a license agreement or cause classpath issues from being bundled into a WAR, EAR, fat jar, or the like. Optional dependencies save space and memory. This is not the clearest way to handle this situation, but both optional dependencies and dependency exclusions are stop-gap solutions. ![]() If a user wants to use functionality related to an optional dependency, they have to redeclare that optional dependency in their own project. However, since the project cannot be split up (again, for whatever reason), these dependencies are declared optional. This new subproject would have only non-optional dependencies, since you'd need them all if you decided to use the subproject's functionality. Ideally, such a feature would be split into a sub-module that depends on the core functionality project. The idea is that some of the dependencies are only used for certain features in the project and will not be needed if that feature isn't used. Optional dependencies are used when it's not possible (for whatever reason) to split a project into sub-modules. It also explains why exclusions are made on a per dependency basis instead of at the POM level. This will help users to understand what they are and when and how to use them. This section discusses optional dependencies and dependency exclusions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |