Spring Boot 初级入门教程(十二) —— 运行、测试、打包过程中碰到的各种错误
2018年08月18日 09:46:41 SpringBoot ⁄ 共 8841字 暂无评论 ⁄ 被围观 2,936次

错误一:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test-springboot: Compilation failure

  1. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test-springboot: Compilation failure: Compilation failure:  
  2. [ERROR] /D:/workspace/workspace-sts-3.9.4.RELEASE-x86/test-springboot/src/main/java/com/menglanglang/test/springboot/controller/TestController.java:[3,17] 程序包org.junit不存在  
  3. [ERROR] /D:/workspace/workspace-sts-3.9.4.RELEASE-x86/test-springboot/src/main/java/com/menglanglang/test/springboot/controller/TestController.java:[4,24] 程序包org.junit.runner不存在  
  4. [ERROR] /D:/workspace/workspace-sts-3.9.4.RELEASE-x86/test-springboot/src/main/java/com/menglanglang/test/springboot/controller/TestController.java:[18,2] 找不到符号  
  5. [ERROR] 符号: 类 RunWith  
  6. [ERROR] /D:/workspace/workspace-sts-3.9.4.RELEASE-x86/test-springboot/src/main/java/com/menglanglang/test/springboot/controller/TestController.java:[22,10] 找不到符号  
  7. [ERROR] 符号:   类 Test  
  8. [ERROR] 位置: 类 com.menglanglang.test.springboot.controller.TestController  
  9. [ERROR] -> [Help 1]  
  10. org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test-springboot: Compilation failure  
  11.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)  
  12.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)  
  13.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)  
  14.     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)  
  15.     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)  
  16.     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)  
  17.     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)  
  18.     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)  
  19.     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)  
  20.     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)  
  21.     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)  
  22.     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)  
  23.     at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)  
  24.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  25.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
  26.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  27.     at java.lang.reflect.Method.invoke(Method.java:498)  
  28.     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)  
  29.     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)  
  30.     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)  
  31.     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)  
  32. Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure  
  33.     at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:858)  
  34.     at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)  
  35.     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)  
  36.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)  
  37.     ... 20 more  
  38. [ERROR]   
  39. [ERROR]   
  40. [ERROR] For more information about the errors and possible solutions, please read the following articles:  
  41. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException  

如果出现上面的错误,可以这样操作:选中项目-》菜单 project -》Clean,让项目重新编译一次,再次打包,这个错误就消失了。

错误二:webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

在打 war 包的过程中,如果提示如下错误:

  1. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project test-springboot: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]  
  2. org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project test-springboot: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)  
  3.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)  
  4.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)  
  5.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)  
  6.     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)  
  7.     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)  
  8.     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)  
  9.     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)  
  10.     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)  
  11.     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)  
  12.     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)  
  13.     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)  
  14.     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)  
  15.     at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)  
  16.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  17.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
  18.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  19.     at java.lang.reflect.Method.invoke(Method.java:498)  
  20.     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)  
  21.     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)  
  22.     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)  
  23.     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)  
  24. Caused by: org.apache.maven.plugin.MojoExecutionException: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)  
  25.     at org.apache.maven.plugin.war.WarMojo.execute(WarMojo.java:193)  
  26.     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)  
  27.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)  
  28.     ... 20 more  
  29. Caused by: org.codehaus.plexus.archiver.ArchiverException: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)  
  30.     at org.codehaus.plexus.archiver.war.WarArchiver.initZipOutputStream(WarArchiver.java:148)  
  31.     at org.codehaus.plexus.archiver.zip.AbstractZipArchiver.createArchiveMain(AbstractZipArchiver.java:309)  
  32.     at org.codehaus.plexus.archiver.zip.AbstractZipArchiver.execute(AbstractZipArchiver.java:211)  
  33.     at org.codehaus.plexus.archiver.AbstractArchiver.createArchive(AbstractArchiver.java:897)  
  34.     at org.apache.maven.archiver.MavenArchiver.createArchive(MavenArchiver.java:582)  
  35.     at org.apache.maven.plugin.war.WarMojo.performPackaging(WarMojo.java:243)  
  36.     at org.apache.maven.plugin.war.WarMojo.execute(WarMojo.java:177)  
  37.     ... 22 more  
  38. [ERROR]   
  39. [ERROR]   
  40. [ERROR] For more information about the errors and possible solutions, please read the following articles:  
  41. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException  

说明项目的 WEB-INF 目录下找不到 web.xml 文件,有两种解决方式。

一种就是直接在该目录下创建 web.xml 文件,做 web 项目应该知道,这个配置文件很重要。

还有一种方式,即就是不创建这个文件,要想打包成功,就必须在 pom 文件中添加如下配置:

  1. <build>  
  2.     <plugins>  
  3.         <!-- 。。。项目其它配置。。。 -->  
  4.         <!-- 项目打war包提示缺少web.xml,不在项目创建该文件时,添加如下配置 -->  
  5.         <plugin>  
  6.             <groupId>org.apache.maven.plugins</groupId>  
  7.             <artifactId>maven-war-plugin</artifactId>  
  8.             <version>2.6</version>  
  9.             <configuration>  
  10.                 <failOnMissingWebXml>false</failOnMissingWebXml>  
  11.             </configuration>  
  12.         </plugin>  
  13.         <!-- 。。。项目其它配置。。。 -->  
  14.     </plugins>  
  15.     <!-- 。。。项目其它配置。。。 -->  
  16. </build>  

错误三:pom.xml 首行报 Unknown 错误

有时候在修改了 SpringBoot 的版本后,pom.xml 文件提示 Unknown 错误,比如从 2.1.5.RELEASE 升级到 2.1.6.RELEASE 时就发生错误,错误如下:

一般来说这个现象应该只在STS(Spring Tool Suite)或者 Eclipse 等类似开发环境中出现,原因是 SpringBoot 版本升级时,maven jar 插件的版本号也升级了造成的问题,有些 IDE 对插件不兼容。

如果出现该错误,可以修改 pom.xml 配置文件,配置 maven jar 插件的版本号即可修复此问题,配置如下:

代码如下:

		<!-- 指定maven-jar-plugin版本,解决子model项目pom文件的Unknown错误 -->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>

其它错误待补充中。。。

原文链接:https://blog.csdn.net/tzhuwb/article/details/81805112

给我留言

留言无头像?