微服务的测试类中启动报错

215 阅读1分钟

tag: Process exited with an error: 1 (Exit value: 1)

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

使用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.

测试代码如下

目录结构:

public class MinIOTest {

    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {

            fileInputStream =  new FileInputStream("D:\\list.html");;

            //1.创建minio链接客户端
            MinioClient minioClient = MinioClient.builder()
                    .credentials("admin", "admin123").endpoint("<http://localhost:9000>").build();
            //2.上传
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object("list.html")//文件名
                    .contentType("text/html")//文件类型
                    .bucket("leadnews")//桶名词  与minio创建的名词一致
                    .stream(fileInputStream, fileInputStream.available(), -1) //文件流
                    .build();
            minioClient.putObject(putObjectArgs);
            System.out.println("<http://localhost:9000/leadnews/ak47.jpg>");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

运行报错信息如下:

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (default-cli) on project minio-demo: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

解决一

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

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。成功。