如何使用 IntelliJ 从 Selenium/TestNG java 文件制作可执行的 jar 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22648384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:05:15  来源:igfitidea点击:

How to make an executable jar file using IntelliJ from a Selenium/TestNG java file?

javaseleniumintellij-ideajarexecutable-jar

提问by urbanaut

I've been Googling for days trying to figure out how to do this, if anybody has done this before I would greatly appreciate the help.

我已经用谷歌搜索了好几天试图弄清楚如何做到这一点,如果有人之前做过这件事,我将不胜感激。

I have an automation test project I've created in IntelliJ that automates a user interacting with a Web Application.

我有一个在 IntelliJ 中创建的自动化测试项目,它可以使用户与 Web 应用程序交互自动化。

I'd like to put that automated test (created in Java, using Selenium and TestNG) into an executable jar file that others can run by double-clicking the jar file.

我想将该自动化测试(用 Java 创建,使用 Selenium 和 TestNG)放入一个可执行的 jar 文件中,其他人可以通过双击该 jar 文件来运行该文件。

Every time I attempt to create a jar file by navigating to Project Structure -> Artifact -> + -> Jar -> From modules with dependencies, it ends up creating a jar that claims it,

每次我尝试通过导航到 Project Structure -> Artifact -> + -> Jar -> From modules with dependencies 来创建 jar 文件时,它最终都会创建一个声明它的 jar,

"Could not find or load the main class <package.MainClass> "

when I attempt to run it with the following command:

当我尝试使用以下命令运行它时:

java -jar MyProject.jar <Manifest Path>

Any idea why I continually get this error, or have a way to do this successfully?

知道为什么我不断收到这个错误,或者有办法成功地做到这一点吗?

Also, here is my pom.xml:

另外,这是我的 pom.xml:

<groupId>TestAutomation</groupId>
<artifactId>TestAutomation</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.test.automation.Executable</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.39.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.40.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

采纳答案by urbanaut

I finally figured it out for anyone else who happens to run into this problem, this is how I got the jar file to be created and run successfully...

我终于为碰巧遇到这个问题的其他人找到了它,这就是我如何创建 jar 文件并成功运行...

I had to change my pom.xml file to the following:

我不得不将我的 pom.xml 文件更改为以下内容:

<groupId>TestAutomation</groupId>
<artifactId>TestAutomation</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.test.automation.Executable</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.40.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Then, I had to adjust my main method to not use any TestNG-related calls. For example, I could not use something like this for my main method:

然后,我不得不调整我的主要方法以不使用任何与 TestNG 相关的调用。例如,我不能在我的主要方法中使用这样的东西:

    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] {WordProfFonts2Set0.class});
    testng.addListener(tla);
    testng.run();

Finally, here are the steps to get the appropriate jar file created:

最后,以下是创建相应 jar 文件的步骤:

  1. Select File > Project Structure... from the top menu
  2. Select "Artifact" on the left menu and click the '+'
  3. Select Jar > From modules with dependencies...
  4. Select your main class using the browse button
  5. Click the radio button next to, "extract to the target jar" and click "OK"
  6. Click the '+' then select "Module Test Output"
  7. In the Available Elements pane to the right, expand the project name and select all the Maven files, then move them to the jar directory being created in the left pane
  8. Click "OK"
  9. Select Build > Build Artifacts... from the top menu
  10. Hover over the jar created and click "Build" under Actions
  1. 从顶部菜单中选择文件 > 项目结构...
  2. 在左侧菜单中选择“Artifact”,然后单击“+”
  3. 选择 Jar > 从具有依赖项的模块...
  4. 使用浏览按钮选择您的主类
  5. 单击“提取到目标 jar”旁边的单选按钮,然后单击“确定”
  6. 单击“+”,然后选择“模块测试输出”
  7. 在右侧的可用元素窗格中,展开项目名称并选择所有 Maven 文件,然后将它们移动到左侧窗格中正在创建的 jar 目录
  8. 点击“确定”
  9. 从顶部菜单中选择 Build > Build Artifacts...
  10. 将鼠标悬停在创建的 jar 上,然后单击操作下的“构建”

Notes:

笔记:

  1. Be sure to add the IE or Chrome driver to your projects resources folder, and call it via the code folder, rather than the computer's hard drive. For example, do this:

    File file = new File("src\test\resources\binaries\IEDriverServer.exe");

  1. 请务必将 IE 或 Chrome 驱动程序添加到您的项目资源文件夹中,并通过代码文件夹调用它,而不是计算机的硬盘驱动器。例如,执行以下操作:

    File file = new File("src\test\resources\binaries\IEDriverServer.exe");

Not this:

不是这个:

File file = new File
("C:\Users\<Username>\<Proj Name>\src\test\java\src\
  test\resources\binaries\IEDriverServer.exe");

Then create the same directory with the driver in it in the same folder your jar is saved to on your computer:

然后在您的 jar 保存在您的计算机上的同一文件夹中创建与驱动程序相同的目录:

src
TestAutomation.jar

2 . Be sure, if using IE, that Protected Mode is set for either all zones or none of them (in IE, go to Internet Options... > Security (tab) > Enable Protected Mode check box)

2 . 请确保,如果使用 IE,为所有区域设置保护模式或不为所有区域设置保护模式(在 IE 中,转到 Internet 选项...> 安全(选项卡)> 启用保护模式复选框)