Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 311
The Awnry Bear Avatar asked Sep 08 '11 00:09

The Awnry Bear


People also ask

What is application domain in programming?

Application domains provide an isolation boundary for security, reliability, and versioning, and for unloading assemblies. Application domains are typically created by runtime hosts, which are responsible for bootstrapping the common language runtime before an application is run.

What is web application domain?

An application domain is the segment of reality for which a software system is developed. It is the background or starting point for the actual-state analysis and the creation of a domain model. An application domain can be an organization, a department within an organization, or a single workplace.

What is an AppDomain C#?

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown.


2 Answers

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

like image 116
Christian Strempfer Avatar answered Oct 03 '22 01:10

Christian Strempfer


Imho, this depends on your build system, not your IDE.

So, if you

  • use plain Eclipse to build and like to keep things simple, just add a third project and add a dependecy.
  • use osgi, you'd properly already have created a new osgi project.
  • use a build tool, like maven or gradle, then setup a multi-project build.
like image 34
Stefan K. Avatar answered Oct 03 '22 01:10

Stefan K.