2017-08-11 20 views
-1

我有這個後臺項目在我的pom.xml:的NoClassDefFoundError越來越後端謨到我的前端時

<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> 
... 

然後我的前端項目獲得這個後臺項目在我的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 { 

... 

開始我的服務器時,我有這樣的錯誤:

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

你們有,爲什麼我有這樣的錯誤任何想法?

謝謝!

+0

我的同事創造了2 「的web項目」。因此,類在WEB-INF文件夾....無論如何,謝謝你們 – Spiiff77

回答

0

至少部分問題就在於此:

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

如果你包括戰爭類型的神器,你不能從它的WEB-INF/classes文件夾就像我認爲你要使用的類這樣做,所以你看到的錯誤是預期的行爲。

一場戰爭不像一個罐子。

關於可能的解決方案,我想提一下Maven中的WAR項目也可以將其作爲jar發佈爲classes。 這link here更好地描述它:

如何創建包含在我的web應用程序的類的JAR?

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, 

使用以下配置:

  <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,然後宣佈對JAR依賴從你的web應用的 以及從任何需要它的其他項目。

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

神器,用分類,使用以下配置:

<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.