Search code examples
javamavenmoduledependenciesmaven-module

How to run multi module maven application?


I tried to run my multi module maven application with

mvn -f pom.xml -DskipTests clean install
java -jar modules/mainmodule/target/mainmodule-1.0-SNAPSHOT.jar

The build works without problems and all modules are available in my .m2 repository, but when running the application I get that error:

Exception in thread "main" java.lang.NoClassDefFoundError: de/peyrer/indexmodule/Indexmodule

This isn't the only dependency java can't find, it can't find any dependency. To me it seems that java can`t find my local maven repository. So what am I doing wrong?


Solution

  • The NoClassDefFoundError error occurs when the class on which your application depends is not available when executing code.

    Perhaps this happens because you have not added dependencies of your maven project to the jar file.

    Try to use maven-assembly-plugin.

      <build>
        <plugins>
          <!-- any other plugins -->
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
          </plugin>
        </plugins>
      </build>