Java MDB EJB Project maven depends on Web Project

2019-08-19 07:28发布

问题:

I got an existing web project. Now I got a requirement to add a MDB EJB project and did the same. The EJB project maven depends on the web project

So all is well until now. The moment I start to use some of the classes in the web project in my MDB it compiles fine and everything look great But bombs in the runtime with NoClassDefFoundError

I went to my web pom.xml and added this in the configuration secstion

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <attachClasses>true</attachClasses>

and in my EJB POM

<dependency>
        <groupId>my.group</groupId>
        <artifactId>my-web-project-name</artifactId>
        <version>${project.version}</version>
        <classifier>classes</classifier>
    </dependency>

How to make my EJB find the classes inside the web project

I really can't do it the other way as i am not allowed to edit the web project. Please Help!!!

回答1:

Basically by default all servers use module classloaders and web module may depend on ejb, but not other way around. It is definitely not recommended. You have following options:

  • if you are on Java EE 6 compliant server, you could add your EJBs directly to web module (requires changes in the web project)
  • extract common classes from web project and create java utility project. Add that project to EAR, and make web and ejb project depend on that, and web depend on ejb (also requires changes in the web)
  • configure single classloader for your whole application. If your server allows that, this should solve your issues.

So to summarize, I'd rather recommend edit web project and use one of two first solutions, but if you really cant then third solution should work.