简体   繁体   English

从命令提示符或jenkins运行TestNG测试套件时出现Maven异常

[英]Maven exception while running the TestNG testsuite from command prompt or jenkins

OS: Ubuntu 16.04 Here is my updated pom.xml 操作系统:Ubuntu 16.04这是我更新的pom.xml

Now I am running following command mvn install -Pbizom - DsuiteXmlFile=src/test/java/suite/testng_OrderSanity.xml 现在我正在运行以下命令mvn install -Pbizom-DsuiteXmlFile = src / test / java / suite / testng_OrderSanity.xml

I have tried with different log4j version but not working. 我尝试使用不同的log4j版本,但无法正常工作。

This is my updated pom.xml file: 这是我更新的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             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.mobisy.bizom</groupId>
    <artifactId>bizom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>

            </plugins>
    </build>
    <properties>          
         <extentreports.version>2.40.2</extentreports.version>
    <project.build.sourceEncoding>
              UTF-8 
    </project.build.sourceEncoding>
    <project.reporting.outputEncoding>
            UTF-8 
    </project.reporting.outputEncoding>

</properties>
        <profiles>
            <profile>
                <id>bizom</id>             
 <properties>

       <maven.test.failure.ignore>true</maven.test.failure.ignore>
                <suiteXmlFile>src/test/java/suite/testng_OrderSanity.xml</suiteXmlFile>
            </properties>
            <build>
                    <plugins>
                        <plugin>

                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                            <version>2.19.1</version>
                            <configuration>
                                <properties>
                                    <property>
                                        <name>listener</name>            
                          <value>bizom.listener.CustomListener</value>
                                    </property>
                                </properties>
                 <suiteXmlFiles>
                            <suiteXmlFile>${suiteXmlFile}
                 </suiteXmlFile>
                                </suiteXmlFiles>
                   <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
                                <threadCount>1</threadCount>
                                <classpathDependencyScopeExclude>runtime</classpathDependencyScopeExclude>
                                <useSystemClassLoader>false</useSystemClassLoader>
                                <printSummary>true</printSummary>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-shade-plugin</artifactId>
                            <version>3.1.0</version>
                        </plugin>
                         <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                             <version>2.19.1</version>
                             <executions>
                                 <execution>
                                     <goals>
                                         <goal>integration-test</goal>
                                         <goal>verify</goal>
                                     </goals>
                                 </execution>
                             </executions>
                         </plugin>
                    </plugins>
            </build>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8-beta4</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-testng</artifactId>
            <version>2.19.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
            <type>jar</type>
            <version>15.0</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>2.0.5</version>
        </dependency>

        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>${extentreports.version}</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.19.1</version>
            <type>maven-plugin</type>
        </dependency>

        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.4.1</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.github.fge</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>2.2.6</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-api</artifactId>
            <version>2.19.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>maven-surefire-common</artifactId>
            <version>2.19.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.9.1</version>

        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>      
        </dependency>
    </dependencies>
</project>

But when I am running it through command prompt by below command: 但是当我通过以下命令通过命令提示符运行它时:

**mvn install -Pbizom -DsuiteXmlFile=src/test/java/suite/testng_OrderSanity.xml**

Now facing new error: 现在面临新的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project bizom: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
    [ERROR] java.lang.NoClassDefFoundError: org/apache/log4j/Priority
    [ERROR] at java.lang.Class.getDeclaredConstructors0(Native Method)
    [ERROR] at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    [ERROR] at java.lang.Class.getConstructor0(Class.java:3075)
    [ERROR] at java.lang.Class.newInstance(Class.java:412)
    [ERROR] atorg.testng.internal.ClassHelper.newInstance(ClassHelper.java:65)
    [ERROR] at org.testng.TestNG.setListenerClasses(TestNG.java:695)
    [ERROR] at org.testng.TestNG.configure(TestNG.java:1534)
    [ERROR] at org.testng.TestNG.configure(TestNG.java:1707)
    [ERROR] at org.apache.maven.surefire.testng.conf.TestNGMapConfigurator.configure(TestNGMapConfigurator.java:52)
    [ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:277)
    [ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
    [ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:121)
    [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
    [ERROR] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
    [ERROR] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    [ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    [ERROR] asun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    [ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)


Now its taking 2.19.1 but still error is coming. I have tried with different log4j version but not working.

Your build is effectively using Surefire 2.17 : 您的构建有效地使用了Surefire 2.17

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin: 2.17 :test [错误]无法执行目标org.apache.maven.plugins:maven-surefire-plugin: 2.17 :test

But you defined 2.19.1 as dependency: 但是您将2.19.1定义为依赖项:

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <type>maven-plugin</type>
    </dependency>

Thus version 2.17 is actually running but 2.19.1 classes are on your classpath and used at runtime, obviously that is not compatible. 因此,版本2.17实际上正在运行,但是2.19.1类在您的类路径中并在运行时使用,显然这是不兼容的。 New methods were added betwween 2.19 that were not present in 2.17 . 2.19之间添加了2.17中不存在的新方法。

You defined version 2.19.1 as plugins under pluginManagement , but it is not currently used in your build. 您将版本2.19.1定义为pluginManagement下的插件,但是当前未在构建中使用它。 If you want to use 2.19.1 in your current build your should use plugins and not pluginManagement . 如果要在当前版本中使用2.19.1 ,则应使用plugins而不是pluginManagement If you are not sure about the difference between pluginManagement and plugins , I strongly recommend you see the Maven doc 如果您不确定pluginManagementplugins之间的区别,强烈建议您查看Maven文档。

Putting maven-surefire-plugin config outside of pluginManagement and removing the dependency of type <type>maven-plugin</type> should to the trick. maven-surefire-plugin配置放置在pluginManagement之外,并删除类型<type>maven-plugin</type>dependency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM