What is the best way to share common code (such as domain classes) between two or more projects?

后端 未结 7 1843
野的像风
野的像风 2021-02-02 11:33

We are developing a Web application consisting of two Eclipse projects. One project is an HTTP-based RESTful Web service; the other project is a Web site. Both

7条回答
  •  [愿得一人]
    2021-02-02 12:03

    there are several possible ways to do this, and depending on your specific situation and requirements, some might be better than other.

    in all cases, i would strongly recommend a system that understands version numbers.

    a classic way to do this would be to use maven. also, it is very well integrated in eclipse and you can structure your modules as part of one parent project, such as:

    project-parent
        project-website1
        project-website2
        project-controllers
        project-model
        [...]
    

    internally, these modules can then depend on each other. you can go as far as separating interfaces from the implementation:

    project-parent
        project-website1
        project-website2
        project-api
        project-api-impl
        [...]
    

    and then depending on the api module most of the time. maven also has a few mechanisms for dealing with WAR files.

    this (single-project) approach is probably ideal for a very small development team. the main drawback is that it is quite unpractical to release things separately - a bugfix in the implementation that only affects website2 would also require a release of website1.

    also, the separation tends to be less clear in this, making it potentially too easy to move stuff into the shared modules that shouldn't really be there.

    another pattern would be:

     project1-parent
          project1-webapp
          project1-specifics
          [...]
    
     project2-parent
          project2-webapp
          project2-specifics
          [...]
    
     common-parent
          common-api
          common-impl
          common-model
          [...]
    

    this makes the separation a bit clearer, and you can release things separately. also (although this is probably not recommended under normal circumstances), project1-webapp could depend on an older or newer version of the common module than project2-webapp. maven can be found here:

    https://maven.apache.org/
    

    another toolset that might help you deal with versioning is:

    https://gradle.org/
    

    to make the most of these, you might also want to look into using git with gitflow:

    http://git-scm.com/
    https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow/
    

    and understand how to use this to deal with versioning and releasing.

    personally, i found it VERY confusing when i first started out - but it all makes a lot of sense now.

提交回复
热议问题