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 ...
随机推荐
- ubuntu tomcat 8.5.33 开启https
用jdk自带的个工具生成数字证书: han@ubuntu:~$ sudo $JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA -keyst ...
- php ReflectionClass类遍历类中包含元素的方法
ReflectionClass 类 类内容 class MyClass { const myconst1 = 100000001; const myconst2 = [ 1 => '开始时间', ...
- windows许可证即将过期
win+R 输入 slmgr.vbs -xpr 查看日期 激活工具地址: 链接: https://pan.baidu.com/s/1S5nealQM1bytPYV6CYbgyg 提取码: sbmu 1 ...
- CentOs查看某个字符串在某个目录下的行数
如果你想在当前目录下 查找"hello,world!"字符串,可以这样: grep -rn "hello,world!" ./ ./ : 表示路径为当前目录. ...
- kvm虚拟机存储管理
一.kvm存储虚拟化介绍: 1.KVM 的存储虚拟化是通过存储池(Storage Pool)和卷Volume)来管理的. 2.Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种型 ...
- 数组遍历for forEach for..in for..of
最开始接触的遍历for 通过下标获取数组每一项的值 ,,]; ;i<num.length;i++) { console.log(num[i]) } /*打印2 5 7*/ forEach遍历数组 ...
- Mysql更新关联子查询报错
报错内容:sql 1093 - You can't specify target table 'u' for update in FROM clause 错误原因: if you're doing ...
- Spark集群部署(standLone)模式
安装部署: 1. 配置spark为1个master,2个slave的独立集群(Standlone)模式, 可以在VMWare中构建3台运行Ubuntu的机器作为服务器: master主机配置如下: ...
- Do-Now—团队冲刺博客一(领航篇)
Do Now -- 团队冲刺博客一(领航篇) 团队博客总目录:团队作业第一周 团队作业第二周 Do Now -- 团队冲刺博客一 领航目标 ① 各个成员在 Alpha 阶段认领的任务 ② 明日各个成员 ...
- Analysis CDI
CDI是一组服务,它们一起使用,使开发人员可以轻松地在Web应用程序中使用企业bean和JavaServer Faces技术.CDI设计用于有状态对象,还有许多更广泛的用途,允许开发人员以松散耦合但类 ...