• Post Reply Bookmark Topic Watch Topic
  • New Topic

The end of Maven's optional dependencies?

 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I understood at Devoxx, modules will have two settings for dependencies - either it's a dependency and it's required, or it's not a dependency and the code cannot access it.

Maven on the other hand has optional dependencies. These dependencies are available at compile time, but may be omitted at runtime. This can be used for example if you only need part of an API, and not the part that has the dependency. The module system however needs all of its dependencies available at runtime. As far as I can tell, this means that optional dependencies are no longer possible. The result is either libraries that are split into more than one module, or libraries that require all dependencies to be met and therefore will require more modules on the module path (which in turn leads to larger applications).

Am I correct in this observation, or did I miss anything?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35744
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,
That's not my understanding. maven has different levels of dependencies:

  • compile - these are needed to compile and packaged with the artifact. Modules fit logically here
  • test - these are needed to test, but not compile the code that is packaged as the main artifact. Are these optional? You need them to compile. And they'd be modules for the test command.
  • provided - these are needed to compile but not packaged with the artifact. I think modules still work here. You compile with the modules in the path. And when you deploy, you specify different equivalent modules.


  • There are others of course, but I think these three are enough for discussing this point.
     
    Rob Spoor
    Sheriff
    Posts: 20822
    68
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For compile and provided, modules should work just fine. For test there will be some techniques that build tools will need to apply to append to the module path at runtime (Mark Reinhold has discussed this a bit at Devoxx). But what I'm talking about is the <optional>true</optional> flag that Maven supports. It's a bit like provided scope but the application can run without the dependency, as long as the classes and methods that use the dependency aren't used.
     
    Stephan van Hulst
    Bartender
    Posts: 6587
    86
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't think the two are related.

    Maven translates dependencies to hard classpath inclusions at the time they're needed. The compile scope means that maven will put a dependency on the classpath during compilation, and also when running the application. Making a dependency optional means that if A depends on B, and B optionally depends on C, Maven will not put C on the classpath when compiling/running A, except when A redeclares C as a dependency.

    To the javac and java tools, including such a module still looks like a hard yes/no question, except Maven handles this for you.
     
    Rob Spoor
    Sheriff
    Posts: 20822
    68
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But if A doesn't use the functionality of B that uses C, it's currently just fine to not include the dependency. And I think that's what will change with the module system; at runtime, B cannot exist with C any more, while currently it can, as long as those parts of B that use C are not used.
     
    Stephan van Hulst
    Bartender
    Posts: 6587
    86
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see, assuming you meant B cannot exist without C any more.

    I'm going to have to look at Jigsaw a little bit closer, it sounds this might have some interesting consequences.
     
    Rob Spoor
    Sheriff
    Posts: 20822
    68
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're right of course.
     
    Mohamed Sanaulla
    Bartender
    Posts: 3166
    34
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There has been a proposal for optional dependency in modules - http://mail.openjdk.java.net/pipermail/jpms-spec-experts/2016-June/000306.html
     
    Rob Spoor
    Sheriff
    Posts: 20822
    68
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Very interesting!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!