4

I am working on application which contains Maven multi module projects. When tryng to @Autowire a service class from another module I'm getting java.lang.NoClassDefFoundError: What makes this project unique is the dependency is between 2 web modules.

Parent Project


Pom.xml

<project>
    <groupId>com.test.simple-project</groupId>
    <artifactId>simple-parent</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>module-x</module>
        <module>module-y</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Module X


Pom.xml

<project>
    <artifactId>module-x</artifactId>
    <parent>
        <groupId>${project.groupId}</groupId>
        <artifactId>simple-parent</artifactId>
    </parent>
    <packaging>war</packaging>
     <dependencies>
        ...
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


package com.test.module-x.service;      
@Service("userService")
   public class UserServiceImpl implements UserService {
}

Module Y


Pom.xml

<project>
    <artifactId>module-y</artifactId>
    <parent>
        <groupId>${project.groupId}</groupId>
        <artifactId>simple-parent</artifactId>
        <version>${project.version}</version>
    </parent>
    <packaging>war</packaging>
     <dependencies>
        <dependency>
            <groupId>com.test.simpleproject</groupId>
            <artifactId>module-x</artifactId>
        </dependency>
        ...
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


component-scan in Y

<context:component-scan base-package="com.test.module-x.service" />


 @Controller
     public class SimpleController {

        @Autowired
        private UserService userService;
    }

Both modules are Spring mvc web projects (both pom.xml packaging is war)

Running the app throws java.lang.NoClassDefFoundError: com/test/module-x/service/UserService

java.lang.NoClassDefFoundError: Lcom/test/module-x/service/UserService;
    at java.lang.Class.getDeclaredFields0(Native Method)
        at java.lang.Class.privateGetDeclaredFields(Class.java:2570)
        at java.lang.Class.getDeclaredFields(Class.java:1903)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:324)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:285)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:846)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:498)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

Any ideas?

2
  • Hard to say unless you include more information - ideally some of the pom.xml - the war plugin in particular
    – Donald_W
    Jul 1, 2015 at 20:00
  • More information is included
    – tangobee
    Jul 2, 2015 at 3:44

2 Answers 2

3

A WAR module with a dependency on another WAR module does not have access to classes generated by the dependency module. You will therefore have to provide the classes in module-x to those in module-y. There are two ways to do it.

Option 1: Cleaner design


Abstract out common classes into a separate JAR module and declare a dependency from both module-x and module-y to this module.

Option 2: Get access to classes declared in module-x from classes in module-y


First, configure the maven-war-plugin in module-x to package the classes as a separate JAR file.

<project>
  <artifactId>module-x</artifactId>
  <packaging>war</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <attachClasses>true</attachClasses>
          <classesClassifier>classes</classesClassifier>
        </configuration>
      </plugin>
    </plugins>
  </build>

Now, declare a dependency on module-x in module-y:

<project>
  <artifactId>module-y</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module-x</artifactId>
      <version>${project.version}</version>
      <classifier>classes</classifier>
    </dependency>
  </dependencies>
</project>
4
  • 1
    Tried option 2 but I'm getting the same error. Do you think it has something to do with component-scan.
    – tangobee
    Jul 2, 2015 at 9:47
  • 1
    If it is the same stacktrace, component scan has nothing do with it. If it was a component scan error, there would be something like No matching bean of type ... found. Here is a working sample. Compare it with your code to find the differences that are causing the error. If this does not work, post a full sample project that demonstrates your problem.
    – manish
    Jul 2, 2015 at 10:30
  • Thank you for taking the time to create the sample project. I'm checking it, will let you know the result.
    – tangobee
    Jul 2, 2015 at 17:37
  • Just started working again on this project. The sample project you provided works fine but I changed pom.xml to support running the app on servers defined in eclipse and I'm getting org.springframework.beans.factory.BeanCreationException:creating bean with name 'fooController' defined in file ...\...\FooController.class]: Post-processing failed of bean type [class org.example.web.FooController] failed; nested exception is java.lang.IllegalStateException: Failed to introspect bean class [org.example.web.FooController] for resource metadata: could not find class that it depends on
    – tangobee
    Dec 15, 2016 at 2:59
1

You have to define, in the pom.xml file associated to the Module Y a dependency on the output war file of the Module X. It is strange however to have 2 war files and place a dependency between them. You will have, to my understanding, 2 web applications. If I guess correctly, Module X is somehow the business layer of the whole system, (includes the services) so is enough to generate a jar file from it, and define only Module Y as a web module. Nevertheless, you need to have an explicit maven dependency to Module X in the pom.xml file of Module Y.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.