-1

I have a backend project with this in my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>GestionScolaire</groupId>
  <artifactId>GestionScolaire</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src/java</sourceDirectory>
...

Then my FrontEnd project get the backend project with this in my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>GestionScolaire</groupId>
<artifactId>GestionScolaireWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<description>project_Spring</description>
<name>GestionScolaireWeb</name>
<url>http://maven.apache.org</url>
<properties>
    <java.version>1.6</java.version>
    <spring.version>3.1.0.RELEASE</spring.version>
    <cglib.version>2.2.2</cglib.version>
</properties>

<dependencies>  
<dependency>
    <groupId>GestionScolaire</groupId>
    <artifactId>GestionScolaire</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>war</type>
</dependency>

Matiere

package model;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Version;

@Entity
public class Matiere {

...

I have this error when starting my server:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Matiere

You guys have any idea of why I have this error?

Thanks!

Spiiff77
  • 55
  • 9
  • My colleague had created 2 "web project". So classes were in the WEB-INF folder.... Thank you guys anyway – Spiiff77 Aug 14 '17 at 12:49

1 Answers1

0

At least part of the problem lies in this:

<dependency>
    <groupId>GestionScolaire</groupId>
    <artifactId>GestionScolaire</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>war</type>
</dependency>

If you're including an artifact of type war, you can't use the classes from its WEB-INF/classes folder like I think you are trying to do, so the error you are seeing is expected behavior.

A war doesn't behave like a jar.

Regarding possible solutions, I'd like to mention that a WAR project in Maven can also "publish" its classes as a jar. This link here describes it better:

How do I create a JAR containing the classes in my webapp?

If you would simply like to package the classes and resources as a JAR in WEB-INF/lib rather than as loose files under WEB-INF/classes,

use the following configuration:

          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
              <archiveClasses>true</archiveClasses>
            </configuration>
          </plugin>

If you need to re-use this JAR in another project, the recommended approach is to move the classes to a separate module that builds a

JAR, and then declare a dependency on that JAR from your webapp as well as from any other projects that need it.

If you can't move the classes to another project, you can deploy the classes and resources included in your webapp as an "attached"

artifact, with a classifier, by using the following configuration:

    <project>
      ...
      <artifactId>mywebapp</artifactId>
      <version>1.0-SNAPSHOT</version>
      ...
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
              <attachClasses>true</attachClasses>
            </configuration>
          </plugin>
        </plugins>
      </build>
      ...
    </project>

This will result in two artifacts being deployed: mywebapp-1.0-SNAPSHOT.war and mywebapp-1.0-SNAPSHOT-classes.jar.
Andrei
  • 1,613
  • 3
  • 16
  • 36