views:

401

answers:

7

What are the possibilities to enforce certain package dependencies in a java build system? E.g. the myapp.server.bl.Customer class not allowed to refer to the myapp.client.ui.customlayout package. I'm interested in either Ant or IDE-specific solutions.

I'd like to get an error message in the build process 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)

+2  A: 

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

davetron5000
+1  A: 

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

basszero
A: 

Yes, Maven is your choice here, it will do everything for you

Kristian
It won't advise you as you write your code, like IDE support can. Maven is a nice complement to IDE support, but if I had to choose one, I'd opt for the fail-fast advice an IDE gives you.
erickson
This is far too broad an answer. Suggesting Maven is like suggesting Java. Please provide a link to a specific Maven feature that achieves the OP's goal.
Alain O'Dea
+5  A: 

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.

Alex Miller
+3  A: 

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.
bombadil
+2  A: 

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).

Andrei
A: 

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.

Alain O'Dea