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

1 问题

我使用IDEA2020创建了一个maven项目,下面是我的pom.xm

<?xml version="1.0" encoding="UTF-8"?>
<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>com.yulong</groupId>
    <artifactId>mybatis-day01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>

        <!--日志-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

下面是我的程序,很简单就是使用mybatis作为数据持久层访问数据库。
在这里插入图片描述
MybatisTest.java 如下

public class MybatisTest {

    public static void main(String[] args) throws IOException {
        /**
         * 1 读取配置文件
         * 2 创建SqlSessionFactory
         * 3 使用工厂生产SqlSession对象
         * 4 使用SqlSession创建Dao接口的代理对象
         * 5 使用代理对象执行方法
         * 6 释放资源
         */
        //1 读取配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");

        //2 创建SqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

        //3 使用工厂生产SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //4 使用SqlSession创建Dao接口的代理对象
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        //5 使用代理对象执行方法
        List<User> users = userDao.findAll();

        for (User user : users) {
            System.out.println(user);
        }

        //6 释放资源
        sqlSession.close();
        inputStream.close();
    }
}

当我运行这个main方法的时候出现了如下的错误

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

解决办法有很多个

解决办法1: 换成IDEA 2017

我用IDEA2017 打开这个项目运行并不会出现这个问题。我是最近才使用2020的,感觉用的很不习惯,感觉2017更好用。

解决办法2: 不要用main方法执行程序,而使用单元测试。

因为我创建的maven项目,而maven项目执行main函数的时候需要两个插件
maven-compiler-plugin:用来编译Java文件,指定JDK版本等
exec-maven-plugin:用来执行class文件,其中插件配置中需指明执行类的路径。

所以我在MybatisTest.java 下面使用单元测试,问题就解决了。代码如下:

 @Test
    public void test() throws IOException {
        /**
         * 1 读取配置文件
         * 2 创建SqlSessionFactory
         * 3 使用工厂生产SqlSession对象
         * 4 使用SqlSession创建Dao接口的代理对象
         * 5 使用代理对象执行方法
         * 6 释放资源
         */
        //1 读取配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");

        //2 创建SqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

        //3 使用工厂生产SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //4 使用SqlSession创建Dao接口的代理对象
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        //5 使用代理对象执行方法
        List<User> users = userDao.findAll();

        for (User user : users) {
            System.out.println(user);
        }

        //6 释放资源
        sqlSession.close();
        inputStream.close();
    }

执行这个测试函数,问题解决:
在这里插入图片描述

解决办法3 在pom文件中添加插件

如果非要使用main函数执行,那就在maven引入办法二提到的两个插件即可(compiler可以不用引入),但是有可能会导致中文乱码问题。

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

再次运行main方法,出现了中文乱码
在这里插入图片描述

解决数据库中文乱码

方法一:
在setting->maven->Runner->VM Options
  一栏中填入 -Dfile.encoding=gb2312

方法二:在pom.xml中添

<properties>
	<!-- 文件拷贝时的编码 -->
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<!-- 编译时的编码 -->
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>

再次运行main方法,中文就可以正常显示了
在这里插入图片描述

### Maven 部署插件执行失败解决方案 当遇到 `maven deploy plugin execution failed` 的错误时,通常是因为配置不正确或依赖项缺失。以下是详细的排查和解决方法: #### 1. 检查 POM 文件中的插件配置 确保 `pom.xml` 中的 `<build>` 节点下有正确的 `maven-deploy-plugin` 配置[^1]。 ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.0.0-M1</version> <executions> <execution> <id>default-deploy</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` #### 2. 处理生命周期配置未覆盖的问题 如果出现类似于 `Plugin execution not covered by lifecycle configuration` 的警告,则可能需要调整 Eclipse 或其他 IDE 的设置来忽略这些警告,或者通过修改 POM 来让其适应默认构建周期[^2]。 对于特定于环境的自定义插件(如 githook),可以考虑将其移到 profile 下以便按需激活: ```xml <profiles> <profile> <id>custom-hooks</id> <activation> <activeByDefault>false</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>org.sandbox</groupId> <artifactId>githook-maven-plugin</artifactId> <version>1.0.2</version> <executions> <execution> <id>install</id> <phase>initialize</phase> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> ``` #### 3. 使用 Flatten 插件优化 POM 结构 为了减少复杂度并提高可读性和维护性,建议引入 `flatten-maven-plugin` 对最终打包发布的 POM 进行简化处理[^3]: ```xml <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>1.2.7</version> <configuration> <!-- 设置为 true 可以移除不必要的父级引用 --> <updatePomFile>true</updatePomFile> </configuration> <executions> <execution> <id>flatten</id> <phase>package</phase> <goals> <goal>flatten</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 以上措施能够有效帮助排除常见的 Maven 构建过程中由于插件配置不当所引发的各种异常情况。
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值