【问题标题】:How to make executable jar in testng using maven如何使用maven在testng中制作可执行jar
【发布时间】:2017-08-30 23:28:04
【问题描述】:

谁能帮助我如何为 testng maven 项目制作可执行 jar。我的目标是使用 bat 文件运行 testng 可执行 jar 来运行单元测试

下面是我的文件夹结构

如果你能帮助我,不胜感激。

【问题讨论】:

    标签: maven testng maven-plugin executable-jar maven-assembly-plugin


    【解决方案1】:

    您应该使用 maven 插件通过 mvn test 运行 testNG

    <build>
            <!-- Source directory configuration -->
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <!-- Following plugin executes the testng tests -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.14.1</version>
                    <configuration>
                        <!-- Suite testng xml file to consider for test execution -->
                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                            <suiteXmlFile>suites-test-testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    如果你真的想要一个可执行的 jar(我不认为你会这样做),你需要 shade 插件:

    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.greg</groupId>
        <artifactId>tester</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>tester</name>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
        ....
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <manifestEntries>
                                            <Main-Class>com.greg.Application</Main-Class>
                                        </manifestEntries>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
            </plugins>
        </build>
    
    </project>
    

    【讨论】:

    • 嗨,Essex,我按照你的方式出现了错误“错误:无法找到或加载主类”。我们还在 main 方法中使用了 testng.xml。当 testng.xml 出现在应用程序之外时,我们如何提供参考?也让我知道使用什么目标。
    【解决方案2】:

    有一种简单的方法,你可以通过在eclipse上点击右键来创建可执行jar跟随-https://www.mkyong.com/java/how-to-make-an-executable-jar-file/

    并创建 1 个 java 类,它将调用您的 testNG xml 文件并使用 testNG.run() 执行。

    公共类 InvokeTestNGJava {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        System.out.println("Started!");
        TestNG testng = new TestNG();
        List<String> suites = Lists.newArrayList();
        suites.add("src"+File.separator+"main"+File.separator+"resources"+File.separator+"stats-comparison.xml");
        testng.setTestSuites(suites);
        testng.run();
    }
    

    }

    Java 类将使用 - java -classpath yourjar.jar youpackage.InvokeTestNGJava 运行。它简单地调用 testng xml 并运行它。

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 2015-06-07
      相关资源
      最近更新 更多