【发布时间】:2017-04-21 08:17:49
【问题描述】:
我有一个 Java/Selenium/TestNG/Maven 项目。我可以成功创建 myproj-0.0.1-SNAPSHOT.jar 文件。当我尝试运行它时出现错误:
$ java -jar myproj-0.0.1-SNAPSHOT.jar
Error: Could not find or load main class utilities.MavenTestInvoker
我的主要目标是能够运行这个 SNAPSHOT.jar 文件以在远程机器上运行我的测试用例。
我在 pom.xml 中使用构建插件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>utilities.MavenTestInvoker</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
当我构建项目时,我使用 maven 命令:“mvn clean package shade:shade”
这创建了我的超级 jar。
以下是依赖项:
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</dependency>
plus some others
</dependencies>
我做错了什么?
【问题讨论】:
-
mainClass在这里应该做什么?你也没有分享在你的 pom 中定义shade插件的内容 -
另外,您能否简单描述一下您完成上述所有工作的最终目标是什么?
-
@nullpointer 我希望能够运行新创建的 jar 文件:java -jar myproj-0.0.1-SNAPSHOT.jar 并让它运行我的 selenium 测试用例。
-
@nullpointer mainClass 应该是我的 java 程序的入口点。它应该从实用程序包和 MavenTestInvoker 类中获取 main 方法。
-
使用 maven 插件作为依赖是完全错误的。你可以使用exec-maven-plugin来启动一个jar