简体   繁体   English

使用 maven-shade-plugin 时无法在测试中加载 ApplicationContext

[英]Failed to load ApplicationContext in tests when maven-shade-plugin is used

I have this test我有这个测试

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:some-context-test.xml")
@DirtiesContext
public class SomeClassIT {
  ...
  @Test
  public void someTestMethod() {
    ...
  }
  ...
}

which runs with no problems when triggered from IntelliJ IDEA, but fails with the error从 IntelliJ IDEA 触发时运行没有问题,但因错误而失败

[ERROR] someTestMethod(x.y.z.SomeClassIT)  Time elapsed: 0.001 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name
 'entityManagerFactory' defined in class path resource [META-INF/db.xml]: Invocation of init method
 failed; nested exception is org.hibernate.AssertionFailure: AttributeConverter class [class x.y.z.SomeConverter]
 registered multiple times
Caused by: org.hibernate.AssertionFailure: AttributeConverter class [class x.y.z.SomeConverter] registered
 multiple times

when executed via maven通过 maven 执行时

mvn verify -DskipITs=false

But if I remove maven-shade-plugin config from pom.xml (the project is using it), then test executes with success even via maven.但是如果我从 pom.xml 中删除 maven-shade-plugin 配置(项目正在使用它),那么即使通过 maven 测试也会成功执行。

Found the solution in this Q&A: https://stackoverflow.com/a/56589859/2806801 .在此问答中找到了解决方案: https : //stackoverflow.com/a/56589859/2806801 The parameter classpathDependencyScopeExclude of maven-failsafe-plugin: maven-failsafe-plugin的参数classpathDependencyScopeExclude

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>[...]</version>
        <configuration>
          <classpathDependencyScopeExclude>runtime</classpathDependencyScopeExclude>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

DETAILS细节

Because of maven-shade-plugin the META-INF/db.xml (imported by some-context-test.xml ) was loaded twice:由于 maven-shade-plugin META-INF/db.xml (由some-context-test.xml导入)被加载了两次:

  1. jar:file:/C:/Users/.../project/module1/target/module1-v.1.0.0-SNAPSHOT.jar!/META-INF/db.xml jar:file:/C:/Users/.../project/module1/target/module1-v.1.0.0-SNAPSHOT.jar!/META-INF/db.xml
  2. jar:file:/C:/Users/.../.m2/repository/.../project/module2/1.0.0/module2-1.0.0.jar!/META-INF/db.xml jar:file:/C:/Users/.../.m2/repository/.../project/module2/1.0.0/module2-1.0.0.jar!/META-INF/db.xml

After I set parameter classpathDependencyScopeExclude to runtime the META-INF/db.xml was loaded only from在我将参数classpathDependencyScopeExclude设置为runtimeMETA-INF/db.xml仅从

  1. jar:file:/C:/Users/.../project/module1/target/module1-v.1.0.0-SNAPSHOT.jar!/META-INF/db.xml jar:file:/C:/Users/.../project/module1/target/module1-v.1.0.0-SNAPSHOT.jar!/META-INF/db.xml

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

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