在两个或多个项目之间共享通用代码(如域类)的最佳方式是什么? [英] What is the best way to share common code (such as domain classes) between two or more projects?

查看:456
本文介绍了在两个或多个项目之间共享通用代码(如域类)的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个包含两个Eclipse项目的Web应用程序。一个项目是基于HTTP的 RESTful Web服务;另一个项目是一个网站。两者都将部署为WAR。最初,两者都将部署在相同的应用服务器实例下,但最终将在不同的框上。



网站应用程序使用 RESTful WS应用程序显然,这两个项目都是常见的代码 - 特别是域类。例如,可能有一个位于< app> / users的资源,用于暴露用户对象的CRUD操作;要更新用户,该网站应用程序会将一个XML编组的用户对象发布到< app> / users。执行GET到< app> / users / 1将返回一个XML编组的User对象。



显然,在两个项目中拥有一个User类将是非常愚蠢的各种原因。所以我想知道最好的方法是什么?将共同代码放在两个项目之间共享的JAR中是我以前做过的,但是有更好还是更简单的方法?



编辑:删除RESTful引用。除了在两个Eclipse项目之间共享通用代码的正确方法是什么?

解决方案

strong>



实际创建第三个项目并添加项目依赖是最好的方式,因为关注问题的分离不仅是课程的原则,也是软件模块的一个原则。它创造了一些优点:




  • 阅读更少的代码来学习一个项目的绳索。

  • 更好的依赖关系控制,因为你可以省略一些项目间依赖关系,所以使用错误模块的类是不可能的。

  • 重复的代码是可怕的。



项目结构



确保您没有创建一个大实用程序项目,而是特定于领域的项目,如用户管理地址簿



在您的情况下,可能是




  • user-api
  • 包含用户转移对象
  • 用户服务提供CRUD操作

  • webapp (或 user-client )调用user-service。



其他Build Systems



当进行持续集成时,您需要使用比Eclipse更好的构建系统,但原则是一样的。您将创建具有最小依赖关系的小模块。



最受欢迎的Build Systems for Java项目是 Maven Ant Gradle 。每个都有自己的方式定义模块依赖。



Eclipse中的项目引用



要告诉Eclipse有关项目依赖关系,请右键单击项目,打开属性并切换到项目引用。在这里,您可以标记依赖关系,以便代码更改将立即生效,而无需手动复制JAR文件。




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 will be deployed as WARs. Initially, both will be deployed under the same application server instance, but eventually they'll be on separate boxes.

The Web site app consumes the RESTful WS app. Obviously, there will be code--specifically, domain classes--that are common to both projects. For instance, there might be a resource located at <app>/users which exposes CRUD operations on User objects; to update a user, the Web site app would POST an XML-marshalled User object to <app>/users. Doing a GET to <app>/users/1 would return an XML-marshalled User object.

Obviously, having a User class in both projects would be pretty stupid for a variety of reasons. So I'm wondering what is the best way to go about this? Putting the common code in a JAR that's shared between the two projects is what I have done in the past, but is there a better or easier way?

Edit: Removed RESTful references. Semantics aside, what is the right way to share common code between two Eclipse projects?

解决方案

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.

这篇关于在两个或多个项目之间共享通用代码(如域类)的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆