Should I rely on transitive dependencies in Maven if they come from other sub-module of my parent?

前端 未结 3 1999
半阙折子戏
半阙折子戏 2021-01-19 16:41

Suppose we are working on mortgage sub-module, and we are directly using the Google Guava classes in module code, but the dependcy for the

3条回答
  •  渐次进展
    2021-01-19 17:18

    1. Yes, declare the dep. It's not a duplication!!! That compile dependencies are transitive is not intended by the maven developer, it's forced by the java-language. Because features like class-inheritance forces this behavior. Your already mentioned "pro" is the important fact. See the (*) note in the transitive-scope-table

    2. Yes, always declare needed third party lib-versions in your reactor parent with dependencyManagement. It's a pain to find errors from different lib-versions at runtime. Avoid declaring versions of third-party libs in sub-modules of large reactors, always use a depMngs in parent.

    3. No, i would use "provided" only for dependencies provided from the runtime, in your example tomcat/jboss/wildfly/.. for things like servlet-api/cdi-api/. But not for third party libraries. Declare the "provided" scope as late as possible (i.e. your deployment(s) module war/ear) not in your business modules. This makes it easier to write tests. For example:

      • investment (depends on guava scope:=provided)
      • mortgage (depends on investment, but don't need guava himself)

    --> mortgage classpath doesn't contain guava. If you write a unit-test for mortgage where classes involved from investment it will not work -> you need to declare at least guava with scope=test/runtime to run these tests...

提交回复
热议问题