Maven项目执行java入口main方法
在Maven项目中配置pom.xml文件加载maven-surefire-plugin插件来执行testng.xml,相信大家对此种用法已经非常熟悉了。但是有些场景可能需要我们去加载执行java的main方法,比如在多设备并发集成中:我们在main方法里面执行动态生成的testng.xml文件等等场景。
同样,也是在pom.xml文件中做文章,在文件中添加如下配置:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.lemon.phoenix.util.StartServer</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
使用了插件的方式去配置生命周期触发指定的插件运行特定的任务,<phase>指定了Maven的生命周期阶段,<goal>指定了插件的目标为java
将StartServer类的main方法的执行绑定到了Maven的test阶段,通过执行mvn test即可执行main方法。
如果在运行main方法需要往里面传入参数,那么我们还可以进行如下配置,其中arg0,arg1即为对应的参数:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.lemon.phoenix.util.StartServer</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
引入exec-maven-plugin插件可能出错的情况:
第一种:
-source 1.5 中不支持 diamond 运算符
[ERROR] (请使用 -source 7 或更高版本以启用 diamond 运算符)
原因:
执行main方法需要先进行compile(编译)过程,由于Maven compiler默认加载时使用JDK1.5来进行编译,但是我们项目里面的代码可能是JDK1.7或者JDK1.8,不兼容导致的,所以需要将compiler插件的JDK版本保持和项目同步
解决方案:
在pom.xml引入如下插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
第二种:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project appiumdemo: Fatal error compiling: basedir D:\eclipse-workspace\appiumdemo\target\generated-sources\annotations does not exist -> [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:
原因:
console里面并没有任何有效的关键信息,我们可以根据其中的提示加载-X参数来进行debug调试
解决方案:
在项目右键选择run as->Run Configurations
加入如图参数即可执行main方法,并且会把调试信息进行输出
第三种:
Caused by: java.lang.ClassNotFoundException:
com.lemon.phoenix.util.StartServer
at java.net.URLClassLoader.findClass (URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass (ClassLoader.java:424)
at java.lang.ClassLoader.loadClass (ClassLoader.java:357)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run
(ExecJavaMojo.java:276)
at java.lang.Thread.run (Thread.java:748)
原因:
项目的主代码没有遵循Maven的约定造成的,项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包。默认情况下,Maven假设项目主代码位于src/main/java目录,我们遵循Maven的约定,创建该目录,然后在该目录下创建文件。
解决方案:
将项目的主代码移动到src/main/java目录
Maven项目执行java入口main方法的更多相关文章
- 用maven运行指定java类main方法
mvn exec:java -Dexec.mainClass="com.java2s.ide.App"
- 解决idea中maven项目无法读取src/main/java目录下面的配置文件问题
解决idea中maven项目无法读取src/main/java目录下面的配置文件问题 当我们在maven项目中使用Mybatis的时候,需要给Mybatis配置核心xml文件(MyBatis-Conf ...
- 为什么java的main方法必须是静态的
今天看类型信息时发现一个问题,不能再main方法中打印this关键字的信息,这时想起了之前的知识,不能再静态方法中调用this.理由很简单,this表示“这个对象”,也就是声明一个类的对象,然而静态方 ...
- 1-为什么java的main方法必须是静态的
为什么java的main方法必须是静态的 今天看类型信息时发现一个问题,不能再main方法中打印this关键字的信息,这时想起了之前的知识,不能再静态方法中调用this.理由很简单,this表示“ ...
- eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误
eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误 具体描述: 整体编译能够编译成功,但新加一个java,里面创建一个main方法,运行时,报无法加载主类的错误, 整体编 ...
- Ant执行一个含有main方法的class文件
目前需要使用ant来执行一个含有main方法的class文件,并且需要通过命令来行传两个参数(start和end)到main方法. <target name="gsp" de ...
- AndroidStudio运行java的main方法
新建一个java文件,含有main方法 package com.why.project.androidcnblogsdemo.utils; /** * Created by HaiyuKing * U ...
- java中main方法的 (String []args)
java中main方法的 (String []args) String[] args是main函数的形式参数,可以用来获取命令行用户输入进去的参数.java 本身不存在不带String ...
- 构建maven项目,自定义目录结构方法
构建maven项目 创建自定义的文件目录方法: 在项目名称右键-->Builder Path-->Configure Builder Path...Source菜单下的Add Folder ...
随机推荐
- swagger出现no response from server错误的解决办法
解决办法:1.启用80端口2.如果不是使用的80端口,是用的nginx做了映射的其他端口的话可以用Springfox swagger-ui 覆盖默认request host,加上这个在spring的应 ...
- 【ES6】Generator+Promise异步编程
一.概念 首先我们要理解Generator和Promise的概念. Generator:意思是生成器,可以在函数内部通过yeild来控制语句的执行或暂停状态. *Foo(){ yeild consol ...
- tensorflow卷积神经网络-【老鱼学tensorflow】
前面我们曾有篇文章中提到过关于用tensorflow训练手写2828像素点的数字的识别,在那篇文章中我们把手写数字图像直接碾压成了一个784列的数据进行识别,但实际上,这个图像是2828长宽结构的,我 ...
- Linux学习之基本操作命令
目录基本操作命令 列目录内容ls ls [options] [files] #options是可选参数 常用可选参数:-a 所有文件及目录 -A 等同于-a,但是不列出.以及.. -l 长格 ...
- MySQL 如何使用左链接代替 NOT IN
核心思想 通过左链接 查询出要排除的数据 然后和主表进行匹配 拿去未匹配到的数据 可以使用 IS NULL 来过滤掉 案例稍后 更新 select * from a left join on a.id ...
- C++ otlv4 连接 sql server 数据库小记
otlv4介绍: http://otl.sourceforge.net/ 测试代码 // testotlv4.cpp : 定义控制台应用程序的入口点. // #include "stdafx ...
- 用idea 创建一个spring小demo,基于xml文件配置
1.首先,File->new->project ,进入新增项目页面 或者在 2.勾选spring,然后点击下一步 3.修改项目名称和项目位置 进入页面后 5.创建一个spring配置文件 ...
- python练习题目
1.查看本机安装python版本 2.用python打印"Hello World",给出源代码和执行结果 a.命令行窗口输出(前提:python程序加入PATH系统环境变量) b. ...
- IIS 接口访问404
IIS->网站->ASP->启用父路径(true)
- 2018面向对象程序设计(java)课程学习进度条
周次 (阅读/编写)代码行数 发布博文量/评论他人博文数量 课余学习时间 学习收获的最大程序阅读或编程任务 1 30-50 1/0 5 九九乘法表 2 60-80 1/0 6 实验一,实验二 3 12 ...