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

后端 未结 7 1844
野的像风
野的像风 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:05

    Separation of concerns

    Actually creating a third project and adding project dependencies is the best way, because Separation of concerns isn't only a principle for classes but also for software modules. It creates some advantages:

    • Less code to read to learn the ropes of one project.
    • Better dependency control, because you could leave out some inter-project dependencies, so that using classes of the wrong module isn't possible.
    • Duplicating code is awful.

    Project Structure

    Make sure you're not creating one big "utility" project, but rather domain-specific projects, like user management or addressbook.

    In your case, it could be

    • user-api contains User transfer object
    • user-service provides CRUD operations
    • webapp (or user-client) calls user-service.

    Other Build Systems

    When moving to continuous integration you'll need to use a better build system than Eclipse, but the principles are the same. You'll create small modules with minimal dependencies.

    The most popular Build Systems for Java projects are Maven, Ant and Gradle. Each has its own way to define module dependencies.

    Project references in Eclipse

    To tell Eclipse about project dependencies, right click on the project, open the properties and switch to the project references. Here you could mark dependencies, so that code changes will take effect immediately without copying a JAR file manually.

    Project references menu

提交回复
热议问题