回答

0

您應該使用Maven插件與MVN試運行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(我不認爲你這樣做),你需要樹蔭插件:

<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> 
+0

嗨艾塞克斯,我按照你的方式去錯誤「錯誤:無法找到或加載主類」。我們還在main方法中使用testng.xml。當testng.xml存在於應用程序之外時,我們如何給出參考?同時讓我知道要使用什麼目標。 –

0

有1點簡單的方法,你可以通過點擊右側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並運行它。

相關問題