Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict dependencies between Java packages

What are the possibilities to enforce restrictions on the package dependencies in a Java build system? For example, the myapp.server.bl.Customer class should not be allowed to refer to the myapp.client.ui.customlayout package. I'm interested in either Ant-based or IDE-specific solutions.

I'd like to get an error message in the build indicating that a (custom) package dependency rule has been violated and the build aborted. I also would like to maintain the dependencies in a list, preferably in a text file, outside of the Ant scripts or IDE project files.

(I don't know Maven but I've read it here it has better support for module dependency management)

like image 974
Karl Avatar asked Sep 29 '08 15:09

Karl


4 Answers

You can use multiple modules in IDEA or Maven or multiple projects in Eclipse and Gradle. The concept is the same in all cases.

A trivial interpretation would be a module for myapp.server.bl and another for myapp.client.ui.customlayout with no compile time dependencies between either of them. Now any attempt to compile code or code-complete against the opposite module/project will fail as desired.

To audit how extensive the problem already is, a useful starting point for IntelliJ IDEA is Analyzing Dependencies:

http://www.jetbrains.com/idea/webhelp/analyzing-dependencies.html

From that article you can see how to run and act on dependency analysis for your project.

like image 195
Alain O'Dea Avatar answered Oct 20 '22 15:10

Alain O'Dea


I believe Checkstyle has a check for that. It's called Import Control

like image 28
davetron5000 Avatar answered Nov 06 '22 01:11

davetron5000


You can configure Eclipse projects to specify Access Rules. Access rules can specify "Forbidden", "Discouraged", and "Accessible" levels all with wildcard rules. You can then configure violations of either Discouraged or Forbidden to be flagged as either warnings or errors during builds.

Kind of an old article on the idea (details may be out of date):

http://www.eclipsezone.com/eclipse/forums/t53736.html

If you're using Eclipse (or OSGi) plugins, then the "public" parts of the plugin/module are explicitly defined and this is part of the model.

like image 44
Alex Miller Avatar answered Nov 06 '22 01:11

Alex Miller


ivy seems like a good solution for your problem (if you are using ant). Ivy is the offical dependency management component of Ant and thus integrates nicely with ant. It is capable of resolving dependencies, handle conflicts, create exclusions and so on.

It uses a simple xml structure to describe the dependencies and is easier to use than Maven, because it only tries to address dependency resolution problems.

From the Ivy homepage:

Ivy is a tool for managing (recording, tracking, resolving and reporting) project dependencies. It is characterized by the following:

  1. flexibility and configurability - Ivy is essentially process agnostic and is not tied to any methodology or structure. Instead it provides the necessary flexibility and configurability to be adapted to a broad range of dependency management and build processes.
  2. tight integration with Apache Ant - while available as a standalone tool, Ivy works particularly well with Apache Ant providing a number of powerful Ant tasks ranging from dependency resolution to dependency reporting and publication.
like image 3
bombadil Avatar answered Nov 06 '22 01:11

bombadil