Reputation: 9710
I have a library project where i'm considering adding some dependencies as optional, since they will be required only for projects compiling and running on JDK9+.
However, the documentation about optional dependencies does not clarify whether it is possible to optionally depend on specific version, i.e., if i can put in my library pom file something like
<dependency>
<groupId>com.foo</groupId>
<artifactId>dependency-a</artifactId>
<version>2.0</version>
<optional>true</optional>
</dependency>
And somehow make sure that projects that depend on my library and also want to include dependency-a
will depend on version 2.0.
As far as I understand, setting dependency-a
as optional mean that projects that depend on my library will not transitively depend on it, but they may explicitly add it in their own dependencies if they need some additional (optional) features in my library.
Is there a way to make sure that if they want to add dependency-a
they depend on a specific version?
What would happen if a project depending on my library had
<dependency>
<groupId>com.foo</groupId>
<artifactId>dependency-a</artifactId>
<version>1.0</version>
</dependency>
And version 1.0 was not compatible with version 2.0?
And if it isn't possible to enforce the version on optional dependencies, what's the point in setting the version for an optional dependency?
I have also been looking at the maven enforcer plugin as it seems to be able to handle cases like this, but I was wondering if there is a better solution.
Upvotes: 4
Views: 1428
Reputation: 2013
I think that it would be more useful to add an exclusion of the library that is optional. Example: Project A has an optional dependency O Project B includes project A in its pom file. In case B wants to use O, nothing is changed. Otherwise, an inclusions tag is added in B's pom file inside A dependency to exclude O:
<dependency>
<groupId>project.a.group</groupId>
<artifactId>project.a.artifact</artifactId>
<version>1.4</version>
<exclusions>
<exclusion>
<groupId>o.group</groupId>
<artifactId>o.artifact</artifactId>
</exclusion>
</exclusions>
</dependency>
I still dont understand the benefit of using the optional tag, in case we will be obliged to specify the version vs removing the optional dependency from the first project and simply adding it when needed to the second project.
Upvotes: 1
Reputation: 35853
My understanding of optional
is the following:
If you declare a dependency as optional, it stays on your compile classpath (so you need a version), but is not transitively visible for users of your library. So users of your library would need to add the dependency themselves (with a sensible version) to their POM.
I also do not see how you want to use the enforcer plugins because users of your library will not "see" the plugins you have in your POM.
For ease of use, I would recommend to have two different jars for the different Java versions, either separated by classifier or by version (like 1.2.3-JDK8, 1.2.3-JDK9).
Upvotes: 1