Question

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)

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

For the IDE specific solutions, IntelliJ IDEA has a dependency analysis tool that allows one to define invalid dependencies as well. http://www.jetbrains.com/idea/webhelp2/dependency-validation-dialog.html

The dependency violation will be shown both when compiling and live, while editing the dependent class (as error/warning stripes in the right side error bar).

Even more automation can be obtained with JetBrains' TeamCity build server, that can run inspection builds and report the above configured checks.

For another IDE independent solution, AspectJ can be used to declare invalid dependencies (and integrate the step in the build process, in order to obtain warning/error info for the issues).

Eclipse has support for this via Build Path properties / jar properties. I think it may only work across jar / project boundaries.

Maybe Classsycle can be used: http://classycle.sourceforge.net/ddf.html

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top