在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目录

最新文章

  1. springboot中swaggerUI的使用
  2. POJ2774 (后缀数组)
  3. HDU4276 The Ghost Blows Light(树形DP+背包)
  4. ZOJ 1110 Dick and Jane
  5. *[hackerrank]Lexicographic paths
  6. easyui源码翻译1.32--datagrid(数据表格)
  7. PHP中的cookie创建取回删除;
  8. 线性代数(高斯消元):JSOI2008 球形空间产生器sphere
  9. Android NumberPicker默认外观的修改
  10. 【回顾整理】暴走的SQL语句练习!!!
  11. skiing(搜索+记忆化搜索)
  12. gcc与g++的编译链接的示例详解
  13. 转:NSString什么时候用copy,什么时候用strong
  14. tp框架的MVC模式
  15. ThreadLocal及InheritableThreadLocal的原理剖析
  16. JavaScript -- 时光流逝(二):js中数组的方法
  17. 联想Y7000安装显卡驱动
  18. python学习笔记(4)-基本数据类型-数字类型及操作
  19. Unity Chan 2D Asset
  20. Jmeter 常用断言使用

热门文章

  1. Spark SQL UDAF示例
  2. 通过View.post()获取View的宽高
  3. docker 安装kafka
  4. MYSQL双主全库同步复制
  5. swagger结合dubbo的rest服务测试
  6. 编写一份好的 Vimrc
  7. 迁移hive,不同集群。
  8. 安装Manjaro KDE 18.04
  9. Git是什么、Git的功能、为什么versioncontrol用Git、Git的常用命令、Git的优缺点
  10. &lt;玩转Django2.0&gt;读书笔记:URL规则和视图