1

I have a specific question regarding the location of the hibernate-annotations jar in my maven project.

My project consists of three modules. The modules Client and Server are both depending on the module Shared. Maven builds two packages for deployment: Client + Shared and Server + Shared.

Annotated Hibernate entities are located in the Shared module since I need to pass them between the client and the server (via RMI).

Now comes my problem. The hibernate-annotations.jar is used as dependency of the Shared module to allow compilation of the Hibernate entities. The library itself depends on hibernate-core. As result, I have the hibernate libraries in my deployed client application, even if I don't really need them there. The jars are quite big and I want to keep the client as slim as possible.

Are there some established techniques to avoid this problem? One that comes to mind would be to use XML based Hibernate configuration so I could move the Hibernate dependencies to the Server module, but I would like to stick to annotations.

Thanks.

  • 1
    Yes, create DTOs for client-server-communication. – Smutje Jan 05 '15 at 15:14
  • Could you be more specific on this? As far as I understand you, my project is structured in exactly the same manner. Annotated DTO's are shared between the client and the server and hence require hibernate libraries in both modules. – Anton Afanasjew Jan 05 '15 at 15:28

2 Answers2

1

I think that you should use the scope tag for the dependencies in your pom.

Look at this and choose the appropriate one : http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

According to me, if you only need the dependency at compile time, you should use the scope "compile" and so on

Hope this will help you!

Ruddy
  • 89
  • 4
  • Thanks for the answer. Hmm, I think, the 'compile' scope will include the library at runtime as well, but I might use 'provided'. Don't know what happens at runtime, though, if the annotations can't be recognized without the library. I will test it today in the evening and post my results. – Anton Afanasjew Jan 05 '15 at 15:29
1

You could exclude one or more transitive dependencies of the Shared dependency in the pom.xml of your Client. Here an example assuming the shared project has group ID "org.shared", artifact ID "shared" and version "0.0.1-SNAPSHOT":

<dependency>
  <groupId>org.shared</groupId>
  <artifactId>shared</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <exclusions>
    <exclusion>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
    </exclusion>
    ...
  </exclusions>
</dependency>
gclaussn
  • 1,736
  • 16
  • 19
  • Thanks for the answer. I think this will work partially, but I will still need the hibernate-annotation dependency for compilation. – Anton Afanasjew Jan 05 '15 at 16:20
  • Only exclude the ones, which are dependencies of hibernate-annotation, but not needed at compile time. In your case only the annotations are needed for the entity classes, but not hibernate-core. In most cases transitive dependencies of a dependency will also be required for compilation and runtime. But your example is a special case, since only a distinct part of the hibernate library is used on the client side. Check Maven's dependency graph to find more unnecessary dependencies. – gclaussn Jan 05 '15 at 21:45