0

I am trying to initialize a Hibernate session in my project-web-server. For that I am using a library I wrote project-data-model that is linked as dependency in maven:

project-web-server pom.xml:

    <dependency>
        <groupId>com.preoject.server</groupId>
        <artifactId>project-data-model</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

HibernateSession.java is therefore in project-data-model; this is how I use it in project-web-server:

public class ServerConfig implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        try {
            HibernateSession.initialize();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent event) {
        // Do stuff on shutdown.
    }
}

Even though I referenced the data model project as dependency I am getting a java.lang.NoClassDefFoundError exception:

java.lang.NoClassDefFoundError: org/hibernate/Interceptor
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
    at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:65)
    at com.project.datamodel.HibernateSession.initialize(HibernateSession.java:19)
    at com.project.web.server.ServerConfig.contextInitialized(ServerConfig.java:14)
    ...

I can't figure out what I have to do here. The folder war/WEB-INF/lib does not contain any Hibernate related libraries; could that be the problem? I'm not sure because I have added the Hibernate dependencies to the parent pom.xml:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.1.Final</version>
        <classifier>tests</classifier>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • How do you actually build and deploy the application? – Dragan Bozanovic Jun 03 '15 at 21:21
  • @DraganBozanovic I am not quite sure if I understand your question. What do you mean? – Stefan Falk Jun 03 '15 at 21:25
  • You have to build your application to run it. If you don't know how it's built under the hood, I assume you're just trying to run it within an IDE (Eclipse) relying on maven Eclipse plugin to connect all the pieces together? – Dragan Bozanovic Jun 03 '15 at 21:45
  • @DraganBozanovic Basically yes, but I realized that Maven does not copy the libraries into the `/lib` directory that is needed by the GWT application. Now I am looking for a method to tell Maven to just move the dependencies into `war/WEB-INF/lib`. – Stefan Falk Jun 03 '15 at 21:47
  • @DraganBozanovic I have of course managed to copy those files by hand - then it works. But that's of course not the reason why I am using a Maven project ^^ – Stefan Falk Jun 03 '15 at 21:48
  • Sure, I completely agree. As I described in my answer below, I think this is not Maven's fault, but rather the issue with GWT/GAE plugin. – Dragan Bozanovic Jun 03 '15 at 22:14

1 Answers1

0

Maven Eclipse plugin will by default add your dependencies to the Eclipse project classpath. It is not aware of any custom build/packaging/deployment scenarios unless you explicitly configure it for the use cases you need. Maybe this question can be helpful in your use case. This article explains how to configure M2Eclipse plugin goals.

Nevertheless, I would expect GWT plugin for Eclipse (I assume you use it) to take everything from the project classpath and copy it to the appropriate places, I'm not sure why it doesn't do it with your setup.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • I got so far but then I ended up with the error "*Artifact has not been packaged yet. When used on reactor artifact, copy should be executed after packaging*". and there I was stuck again. I am rather new to Maven to be honest .. – Stefan Falk Jun 03 '15 at 22:21
  • Maybe [this article](http://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html) can be helpful. – Dragan Bozanovic Jun 03 '15 at 22:44
  • Hmm.. I am not sure but is `` M2Eclipse specific? I am not using this tag. I am using the same syntax that the first link you posted provides. That works if I make a Maven Update but the error stays. `` is not recognized inside the `` tags. – Stefan Falk Jun 03 '15 at 23:04