【问题标题】:Maven exception while running the TestNG testsuite from command prompt or jenkins从命令提示符或詹金斯运行 TestNG 测试套件时出现 Maven 异常
【发布时间】:2017-11-27 13:38:26
【问题描述】:

操作系统:Ubuntu 16.04 这是我更新的 pom.xml

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

我尝试过使用不同的 log4j 版本但无法正常工作。

这是我更新的 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>

但是当我通过以下命令通过命令提示符运行它时:

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

现在面临新错误:

[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.

【问题讨论】:

  • 首先有一个maven插件作为依赖是完全错误的......
  • @khmarbaise 请告诉我在哪里需要更改以及缺少什么?

标签: java maven jenkins log4j


【解决方案1】:

您的构建正在有效地使用 Surefire 2.17

[ERROR] 未能执行目标 org.apache.maven.plugins:maven-surefire-plugin:2.17:test

但是您将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>

因此,2.17 版本实际上正在运行,但 2.19.1 类在您的类路径中并在运行时使用,显然这是不兼容的。在 2.19 之间添加了 2.17 中不存在的新方法。

您将版本2.19.1 定义为pluginManagement 下的插件,但当前未在您的构建中使用。如果您想在当前版本中使用2.19.1,您应该使用plugins 而不是pluginManagement。如果您不确定pluginManagementplugins 之间的区别,我强烈建议您查看Maven doc

maven-surefire-plugin 配置放在pluginManagement 之外并删除&lt;type&gt;maven-plugin&lt;/type&gt; 类型的dependency 应该可以解决问题。

【讨论】:

  • 我已经删除了“pluginManagement”,现在只使用插件,还删除了 maven-plugin 但仍然在同一页面上。它不起作用,仍然需要 2.17 而不是 2.19。 1.请帮忙!
  • 你需要完全移除依赖,而不仅仅是类型。从您的dependencies 中删除maven-compiler-plugin。对此的唯一参考应该在您的plugins 下;)
  • 我已经从依赖项中删除了它,现在 maven-compiler-plugin 仅在插件中,但仍然无法正常工作
  • 是同一个错误吗?你能编辑你的答案并提供更新的 pom 吗?
  • 已上传更新的pom.xml文件,请查看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 2014-08-27
  • 1970-01-01
相关资源
最近更新 更多