Command execution failed.Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project

【maven 3.5.4 IDEA 2020】
学习《spring ioc基本使用》,使用maven方式构建spring项目。在test里面创建了一个main方法测试IOC的基本使用。但是运行时候报错:Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project spring_study2: Command execution failed.
在这里插入图片描述

解决一

参考博客

在maven生命周期框架中,test是用来做测试的,放入test的文件应该是test测试函数,即加了@Test的test函数,那为什么不用test函数来运行本过程呢?

public class MyTest {
    public static void main(String args[]) {
        //ApplicationContext:表示ioc容器
        //ClassPathXmlApplicationContext:表示从当前classpath路径中获取xml文件的配置
        //根据spring的配置文件来获取ioc容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }

}

改成:

public class MyTest {
    @Test
    public void Test() {
        //ApplicationContext:表示ioc容器
        //ClassPathXmlApplicationContext:表示从当前classpath路径中获取xml文件的配置
        //根据spring的配置文件来获取ioc容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }
}

然后在右侧Maven中,clean -> test 成功。

解决二

参考博客

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project

仔细观察上面,发现其实是说插件exec-maven-plugin:3.0.0有问题。到本地Maven仓库去查看,可以找到地方:
在这里插入图片描述

先把本地仓库的mojo文件夹删除。然后在pom文件中添加插件:

<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>
             </configuration>
         </plugin>
     </plugins>
</build>

在这里插入图片描述

运行可能会出现中文乱码。此时在setting->maven->Runner->VM Options一栏中填入 -Dfile.encoding=gb2312。成功。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐