由于'pom.xml'文件,为Spring MVC运行Maven项目时出错?

时间:2015-02-03 02:39:09

标签: java spring maven spring-mvc pom.xml

我正在为Spring MVC创建一个Maven项目,当我运行它时会出现以下错误。首先,当我右键单击项目的名称,然后选择“运行方式”,然后选择“Maven构建...”#39; (目标:码头:跑步)

[WARNING] Failed startup of context org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@51671b08{/mvcDemo,C:\Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\webapp}
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:259)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:235)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:227)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:375)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:337)
    at org.mortbay.jetty.handler.ContextHandler.loadClass(ContextHandler.java:1035)
    at org.mortbay.jetty.plugin.Jetty6MavenConfiguration.parseAnnotations(Jetty6MavenConfiguration.java:125)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.configure(AbstractConfiguration.java:119)
    at org.mortbay.jetty.webapp.WebXmlConfiguration.configureWebApp(WebXmlConfiguration.java:180)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.configureWebApp(AbstractConfiguration.java:96)
    at org.mortbay.jetty.plus.webapp.Configuration.configureWebApp(Configuration.java:124)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1217)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:510)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at org.mortbay.jetty.plugin.Jetty6PluginWebAppContext.doStart(Jetty6PluginWebAppContext.java:110)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
    at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:156)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:222)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.plugin.Jetty6PluginServer.start(Jetty6PluginServer.java:132)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:371)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:307)
    at org.mortbay.jetty.plugin.AbstractJettyRunMojo.execute(AbstractJettyRunMojo.java:203)
    at org.mortbay.jetty.plugin.Jetty6RunMojo.execute(Jetty6RunMojo.java:184)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

当我尝试再次右键单击项目的名称时,选择'运行方式',然后选择“Maven clean'”,然后执行& #39; Maven build ...' (使用Goals:jetty:run),我收到如下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project mvcDemo: Compilation failure: Compilation failure:
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[3,37] error: package org.springframework.stereotype does not exist
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[4,29] error: package org.springframework.ui does not exist
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[5,46] error: package org.springframework.web.bind.annotation does not exist
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[6,46] error: package org.springframework.web.bind.annotation does not exist
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[7,46] error: package org.springframework.web.bind.annotation does not exist
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[9,1] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[10,1] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[19,21] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[37,85] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[51,91] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[18,2] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[37,23] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[36,2] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[51,24] error: cannot find symbol
[ERROR] \Users\7d0\Desktop\Eclipse\WORKSPACE\PROJECTS\mvcDemo\src\main\java\org\mvcDemo\Controllers\MvcControllers.java:[50,2] error: cannot find symbol
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我在Java Build Path中添加了以下.jar文件:

jetty-6.1.26.jar, 
jetty-plus-6.1.26.jar, 
maven-hudson-plugin-6.1.7.jar, 
org.mortbay.jetty.util.jar, 
plexus-classworlds-2.5.1.jar, 
spring-agent-2.5.6.jar, 
classmate-1.0.0.jar, 
hibernate-validator-5.1.3.Final.jar, 
jboss-logging-3.1.3.GA.jar, 
validation-api-1.1.0.Final.jar, 
commons-logging-1.2.jar, 

All .jar files in spring-framework-4.1.4.RELEASE/libs/ folder 
(spring-context-4.1.4.RELEASE.jar, spring-web-4.1.4.RELEASE.jar, ...)

以下是我的' 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>springMvcDemo</groupId>
    <artifactId>mvcDemo</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>mvcDemo Maven Webapp</name>
    <url>http://maven.apache.org</url>
 <dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>    
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>       
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
</dependency>    
 </dependencies>
<build>
   <finalName>mvcDemo</finalName>    
<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>

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.10</version>
    <configuration>
        <argLine>-javaagent C:/Users/7d0/Desktop/Eclipse/Spring MVC Demo/JARs/spring-agent-2.5.6.jar</argLine>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <stopKey>foo</stopKey>
        <stopPort>9999</stopPort>
    </configuration>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <argLine>-javaagent C:/Users/7d0/Desktop/Eclipse/Spring MVC Demo/JARs/spring-agent-2.5.6.jar</argLine>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
            </configuration>
        </execution>

        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>
</plugins>    
 </build>
 </project>

如何修复错误?提前致谢。

2 个答案:

答案 0 :(得分:0)

您无法运行,因为您的maven构建/运行类路径中没有任何弹簧依赖项。

当您选择&#34;运行为 - &gt;时,将所有罐子添加到项目构建路径中并不会为您做任何事情。 maven项目&#34;因为maven将使用pom中的类路径,忽略你在IDE中设置的所有内容。

您应该修改pom文件,使其包含已添加到项目构建路径的依赖项。您可以使用m2e eclipse插件并将项目作为maven项目导入,它将使用您的pom文件中指定的依赖项来管理构建类路径,然后您不必在两个不同的位置管理它们。

答案 1 :(得分:0)

某些类未在构建路径中加载。通过右键单击项目更新您的maven项目 - &gt; Maven - &gt;更新项目---&gt;强制更新快照然后尝试。