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

转载于:https://www.cnblogs.com/yyoba/p/9504729.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值