已解决:Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (default-cli) on project

问题描述

出现以上报错的来源是,我在一个SpringBoot项目(Maven构建的)的 junit 的 test 文件夹建立测试文件,然后我想不用@Test来测试,直接写main方法运行,结果就报错。

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (default-cli) on project mybatis_plus: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [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/MojoExecutionException

同时报错描述中还有错误找不到主类的提示。后来查看target文件下的字节码文件确实有生成也能正常运行。那到底怎么回事呢?

解决办法

这个的问题就是Maven项目执行main函数的时候需要两个插件

  • maven-compiler-plugin:用来编译Java文件,指定JDK版本等
  • exec-maven-plugin:用来执行class文件,其中插件配置中需指明执行类的路径。

只需要在pom.xml中配置

<build>
     <plugins>
         <plugin>
             <groupId>org.codehaus.mojo</groupId>
             <artifactId>exec-maven-plugin</artifactId>
             <version>1.6.0</version>
             <executions>
                 <execution>
                     <goals>
                         <goal>java</goal>
                     </goals>
                 </execution>
             </executions>
             <configuration>
                 <classpathScope>test</classpathScope>
                 <!--<mainClass></mainClass>-->
             </configuration>
         </plugin>
     </plugins>
</build>

内部标签分析

< goal >
  exec主要由两个goal组成:exec:exec和exec:java。你应该如何选择呢?首先,你需要记住,exec:exec总是比exec:java强大而灵活,除此之外,两者的主要区别是在线程管理上:exec:exec总是启动一个新的线程,并且在只剩下守护线程的时候从VM上退出(关闭应用程序)。而对于exec:java,当所有非守护线程结束时,守护线程会被joine或interrupt,应该程序不会关闭。但是对于一般的使用者来说,这种差别并不重要。对于两者的选择,一般来说,如果你的工程启动非常简单,不需要设置jvm参数、系统属性、命令行参数,那么就用exec:java,

< classpathScope >

  • compile
      默认就是compile,什么都不配置也就是意味着compile。compile表示被依赖项目需要参与当前项目的编译,当然后续的测试,运行周期也参与其中,是一个比较强的依赖。打包的时候通常需要包含进去。

  • test
      scope为test表示依赖项目仅仅参与测试相关的工作,包括测试代码的编译,执行。比较典型的如junit。

  • runntime
      runntime表示被依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。与compile相比,跳过编译而已,说实话在终端的项目(非开源,企业内部系统)中,和compile区别不是很大。比较常见的如JSR×××的实现,对应的API jar是compile的,具体实现是runtime的,compile只需要知道接口就足够了。oracle jdbc驱动架包就是一个很好的例子,一般scope为runntime。另外runntime的依赖通常和optional搭配使用,optional为true。我可以用A实现,也可以用B实现。

  • provided
      provided意味着打包的时候可以不用包进去,别的设施(Web Container)会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是在打包阶段做了exclude的动作。

  • system
      从参与度来说,也provided相同,不过被依赖项不会从maven仓库抓,而是从本地文件系统拿,一定需要配合systemPath属性使用。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐