Declare dependency in section even if dependency not used everywhere?

后端 未结 6 458
醉酒成梦
醉酒成梦 2021-01-31 10:23

We\'re using maven 2.1.0. I have multiple modules that are completely separate, but still have many common dependencies. Like log4J, but some modules don\'t need it. I am wond

6条回答
  •  遥遥无期
    2021-01-31 11:09

    If you have a parent project, you can declare all dependencies and their versions in the dependencyManagement section of the parent pom. This doesn't mean that all projects will use all those dependencies, it means that if a project does declare the dependency, it will inherit the configuration, so it only need declare the groupId and artifactId of the dependency. You can even declare your child projects in the parent's dependencyManagement without introducing a cycle.

    Note you can also do similar with plugins by declaring them in the pluginManagement section. This means any child declaring the plugin will inherit the configuration.

    For example, if you have 4 projects, parent, core, ui and utils, you could declare all the external dependences and the internal project versions in the parent. The child projects then inherit that configuration for any dependencies they declare. If all modules are to have the same version, these can be even be declared as properties in the parent.

    An example parent is as follows:

    
      4.0.0
      name.seller.rich
      parent
      1.0.0
      pom
      
        
          
            commons-io
            commons-io
            1.4
          
          
            name.seller.rich
            ui
          ${project.version}
          
          
            name.seller.rich
            core
            ${project.version}
          
          
            name.seller.rich
            utils
            ${project.version}
          
        
      
      
        utils
        core
        ui
      
    
    

    And the utils, core, and ui projects inherit all the relevant versions. utils:

    
      4.0.0
      name.seller.rich
      utils
      
      
        parent
        name.seller.rich
        1.0.0
      
      
        
          commons-io
          commons-io
        
      
    
    

    core:

    
    4.0.0
    name.seller.rich
    core
    
      parent
      name.seller.rich
      1.0.0
    
    
      
        name.seller.rich
        utils
      
    
    

    ui:

    
      4.0.0
      name.seller.rich
      ui
      
        parent
        name.seller.rich
        1.0.0
      
      
        
          name.seller.rich
          core
        
      
    
    

提交回复
热议问题