升级Maven依赖项后,Jenkin构建失败

时间:2019-07-26 10:18:10

标签: java maven jenkins intellij-idea

最近,我正在致力于升级团队之一项目的依赖关系。更改后,项目在我的笔记本电脑上本地成功构建。但是,相同的代码更改在Jenkins上引发了编译错误。

我遇到的错误与powermockito软件包中的class file for xxx.xxx.xxx. not found有关。附加一些相关的代码段以供参考。

使用的以前的pom版本:

... some lines above it ...   

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!--dropwizard versions-->
    <dropwizard.version>1.3.5</dropwizard.version>
    <dropwizard.swagger.version>1.0.0-2-auth-SNAPSHOT</dropwizard.swagger.version>
    <dropwizard.bundle.version>1.3.5-1</dropwizard.bundle.version>
    <dropwizard.requestinfo>1.3.5-3</dropwizard.requestinfo>
    <dropwizard.rosey.data.provider.version>1.3.5-3</dropwizard.rosey.data.provider.version>
    <dropwizard.service.discovery>1.3.5-0</dropwizard.service.discovery>
    <dropwizard.rmq.actor.version>1.2.2-1</dropwizard.rmq.actor.version>
    <dropwizard.riemann.version>1.3.5-4</dropwizard.riemann.version>

    <!--hystrix versions-->
    <hystrix.dropwizard.version>0.4</hystrix.dropwizard.version>
    <hystrix.configurator.version>0.0.6</hystrix.configurator.version>
    <hystrix.version>1.5.3</hystrix.version>
    <hystrix.wrapper.version>1.0-SNAPSHOT</hystrix.wrapper.version>

    <!--internal clients-->
    <user-service-model.version>1.7.91</user-service-model.version>
    <kratos.version>0.8.2-SNAPSHOT</kratos.version>
    <rosey.dropwizard.config>1.14</rosey.dropwizard.config>

    <!--others-->
    <mysql.driver.version>5.1.38</mysql.driver.version>
    <okhttp3.version>3.4.1</okhttp3.version>
    <lombok.version>1.18.8</lombok.version>
    <junit.version>4.12</junit.version>
    <powemock.version>1.7.3</powemock.version>
    <h2.version>1.4.191</h2.version>
    <square.okhttp.version>3.4.1</square.okhttp.version>
    <rmq.version>4.1.0</rmq.version>
    <zookeeper.version>3.4.13</zookeeper.version>
    <curator.version>4.0.1</curator.version>
</properties>

... some lines below it ...

当前使用的pom版本:

... some lines above it ...

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!--dropwizard versions-->
    <dropwizard.version>1.3.12</dropwizard.version>
    <dropwizard.swagger.version>1.3.7-1</dropwizard.swagger.version>
    <dropwizard.bundle.version>1.3.5-1</dropwizard.bundle.version>
    <dropwizard.requestinfo>1.3.5-8</dropwizard.requestinfo>
    <dropwizard.rosey.data.provider.version>1.3.5-6</dropwizard.rosey.data.provider.version>
    <dropwizard.service.discovery>1.3.12-3</dropwizard.service.discovery>
    <dropwizard.rmq.actor.version>1.3.12-1</dropwizard.rmq.actor.version>
    <dropwizard.db.sharding.bundle.version>1.3.12-3</dropwizard.db.sharding.bundle.version>
    <dropwizard.riemann.version>1.3.12-1</dropwizard.riemann.version>

    <!--hystrix versions-->
    <hystrix.dropwizard.version>0.4</hystrix.dropwizard.version>
    <hystrix.configurator.version>0.0.6</hystrix.configurator.version>
    <hystrix.version>1.5.3</hystrix.version>

    <!--internal clients-->
    <user-service-model.version>1.7.96-STAGE-SNAPSHOT</user-service-model.version>
    <kratos.version>0.9.6</kratos.version>
    <rosey.dropwizard.config>1.15</rosey.dropwizard.config>

    <!-- storage -->
    <aerospike.version>3.3.0</aerospike.version>
    <mock.aerospike.version>0.0.4</mock.aerospike.version>

    <!--others-->
    <okhttp3.version>3.4.1</okhttp3.version>
    <lombok.version>1.18.8</lombok.version>
    <junit.version>4.12</junit.version>
    <powemock.version>1.7.3</powemock.version>
    <guava.version>28.0-jre</guava.version>
    <h2.version>1.4.199</h2.version>
    <rmq.version>5.7.2</rmq.version>
    <zookeeper.version>3.4.13</zookeeper.version>
    <curator.version>4.2.0</curator.version>
    <sentinel.model.version>1.0-SNAPSHOT</sentinel.model.version>
</properties>

... some lines below it ...

最低错误日志:

... some code above it ...  

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/MerchantResourceTest.java:[19,1] cannot access org.powermock.api.support.membermodification.MemberModifier
  class file for org.powermock.api.support.membermodification.MemberModifier not found
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/VPAResourceTest.java:[29,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/VPAResourceTest.java:[30,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/services/PrimerServiceTest.java:[28,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/services/PrimerServiceTest.java:[29,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/AddressResourceTest.java:[22,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/AddressResourceTest.java:[23,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/services/WalletServiceTest.java:[27,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/services/WalletServiceTest.java:[28,1] static import only from classes and interfaces
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/MerchantResourceTest.java:[24,25] cannot access org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner
  class file for org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner not found
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/VPAResourceTest.java:[37,25] incompatible types: java.lang.Class<org.powermock.modules.junit4.PowerMockRunner> cannot be converted to java.lang.Class<? extends org.junit.runner.Runner>
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/BaseResourceTest.java:[26,19] package org.mockito does not exist
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/BaseResourceTest.java:[30,26] package org.mockito does not exist
[ERROR] /var/lib/jenkins/jobs/UserService_Develop/workspace/src/test/java/com/xxxxxxx/userservice/resources/BaseResourceTest.java:[30,1] static import only from classes and interfaces

... some code below it ...

我做过的实验:当我从powermockito依赖项中删除了测试范围时,一切都像一个魅力。

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>${powemock.version}</version>
        <scope>test</scope>          <- This line
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>${powemock.version}</version>
        <scope>test</scope>          <- This line
    </dependency>

在下面粘贴Mockito的Maven依赖树

... some line above it ...

[INFO] +- org.powermock:powermock-api-mockito:jar:1.7.3:test
[INFO] |  +- org.powermock:powermock-api-mockito-common:jar:1.7.3:test
[INFO] |  |  \- org.powermock:powermock-api-support:jar:1.7.3:test
[INFO] |  \- org.mockito:mockito-core:jar:1.10.19:test
[INFO] +- org.powermock:powermock-module-junit4:jar:1.7.3:test
[INFO] |  +- org.powermock:powermock-module-junit4-common:jar:1.7.3:test
[INFO] |  |  +- org.powermock:powermock-reflect:jar:1.7.3:test
[INFO] |  |  \- org.powermock:powermock-core:jar:1.7.3:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test

... some lines below it ...

在Jenkins上执行的Maven命令, 以下示例是从控制台日志中获取的:

Executing Maven:  -B -f /var/lib/jenkins/jobs/UserService_Develop/workspace/pom.xml -U clean package cobertura:cobertura docker:build docker:push -Pdocker -Dproject.version=1.8.23-SNAPSHOT -Dbuild.number=1501 -Dbuild.revision=9d8d00591c23b061b07e1a08c00ff024db8ba318 -Dmaven.test.failure.ignore=true -DskipTests -DargLine=-Xmx512m

理想情况下,它不应该发生。我用Google搜索几天后发现此博客Why your tests may pass locally but fail in Jenkins。不幸的是,似乎没有什么对我有用。这里需要一些帮助。谢谢!

2 个答案:

答案 0 :(得分:3)

您的实验表明,依赖关系树不是问题。问题是依赖性的范围。测试范围仅将依赖项添加到类路径以进行测试编译和测试执行。

这里可能出问题的是,您的测试类实际上是在编译阶段而不是测试编译阶段进行编译的。您可以做的一项测试来检查这一点,就是将范围保留为test,并尝试使用mvn compile构建项目,如果一切配置正确,则不应构建测试源。

除此之外,很难在没有看到完整的pom.xml的情况下猜测您到底在做什么以及可能会出错,但是我希望这可以为您提供一个起点。

答案 1 :(得分:3)

以下是关于为什么删除test范围时为什么要编译代码的假设:

就范围而言,Maven documentation提出以下建议:

  

编译

     

这是默认范围,如果未指定则使用。编译   依赖项在项目的所有类路径中均可用。   此外,这些依赖项会传播到相关项目中。

     

测试

     

此范围表示正常情况下不需要依赖项   使用该应用程序,并且仅可用于测试编译   和执行阶段。此范围不是可传递的。

现在,这可能意味着范围classpath的{​​{1}}编译可能与范围test的{​​{1}}潜在不同

当您删除测试范围时,由于默认范围为classpath

,因此发现了模仿罐。