1

I have an application that uses Jasper to generate reports. In order to encapsulate the complexity and provide a uniform interface with the Jasper API, I have created a "intermediate" interface that wraps the Jasper classes and delegates client calls to them. This will also make it easier to change the report machine in the future - to Crystal Reports, for instance.

The thing is, since the Jasper classes are in the classpath, developers (including myself) can accidentally use some of its classes directly in the business code, and that may pass unnoticed for a long time. I would like to avoid that, or at least be notified when that happens.

The environment is basically eclipse, maven, git, sonar, bamboo ci.

I'm sure this is not an uncommon scenario, so, what is the best way to deal? Design patterns, eclipse/maven plugins, sonar alerts? Or maybe something dead simple that I'm just not seeing?

Felipe Reis
  • 479
  • 8
  • 24
  • 1
    One possibility would be to write a unit test that checks all imports from all classes. Since imports can't be accessed through reflection, you'd have to do standard file reading. -- This can be useful: http://stackoverflow.com/questions/17928121/get-list-of-necessary-classes-for-a-class-to-load – acdcjunior Nov 13 '13 at 15:37

4 Answers4

3

In maven you can specify a library is for runtime only. This allows you to not compile against that library at all. If you don't use Jasper from maven, you could avoid including it at all. You can force this by adding an <exclusion> if it is a transient dependency.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Thanks Peter, I think you got it! I've declared a dependency to Jasper in my main project (inside dependencyManagement, I hope that's the proper place to put it) and set its scope as `runtime`. Now if I use any Jasper class from my code, maven generates an error when compiling. – Felipe Reis Nov 13 '13 at 19:18
1

You should have two separate eclipse projects: One for the reporting library, one for the rest.

The reporting library project contains your interfaces, the Jasper jar files and the Jasper-specific implementation of the interfaces.

The other project depends on the reporting library project (you can set project dependencies in the projects properties dialog under "Java Build Path" -> "Projects").

As the reporting project only exports the source folder to the other project, the jasper classes are not visible to it at development time.

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
  • I've already have a separated project for my reporting library. But, since I'm using maven, all the dependencies that the report project has are included in the classpath of the main project... I think Peter's answer is a better fit for me. – Felipe Reis Nov 13 '13 at 18:29
1

I haven't used it much myself, but if you ever need more control over your dependencies you could try DCL Suite, an Eclipse plugin. It lets you define constraints between modules and you can declare the modules to be a class, a set of classes, packages, etc

Pedro Lorentz
  • 2,146
  • 2
  • 13
  • 19
0

That would only be possible if you handled classloading of Jasper and included it as a resource (a jar file) inside your own jar. Then no one would know it was available directly. Here's an example of how you can include jars inside your own jar file -> An embedded jar classloader in under 100 lines.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158