Maven项目执行java入口main方法

时间:2023-03-09 05:38:00
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

Maven项目执行java入口main方法

加入如图参数即可执行main方法,并且会把调试信息进行输出

Maven项目执行java入口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目录