使用Arquillian在WildFly 10上执行集成测试时遇Duplicate layer 'base'错误
WildFly 10.x嵌入式容器集成测试异常排查指南(Cargo+Arquillian+Failsafe)
我之前帮不少开发者排查过这类问题,大概率是版本不兼容或者配置细节没踩对,咱们一步步来捋:
1. 优先检查版本兼容性(最常见的坑)
- WildFly 10.x 原生仅支持 JEE 7,但你用的是JEE 8 API,这会直接导致类加载冲突、API不兼容的异常。建议要么降级到JEE 7依赖,要么升级WildFly到11.x及以上(WildFly 11开始正式支持JEE 8)。
- 确保Arquillian容器适配器版本和WildFly 10严格匹配,比如使用
org.jboss.arquillian.container:arquillian-wildfly-embedded:1.0.0.Final,别混用更高版本的适配器。 - Cargo插件版本也要适配WildFly 10,推荐用
org.codehaus.cargo:cargo-maven2-plugin:1.6.10,这个版本对WildFly 10的支持比较稳定。
2. 验证Cargo插件核心配置
检查你的Cargo插件配置是否覆盖这些关键项:
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.6.10</version> <configuration> <container> <containerId>wildfly10x</containerId> <type>embedded</type> <systemProperties> <jboss.home>${project.build.directory}/wildfly-10.1.0.Final</jboss.home> <java.net.preferIPv4Stack>true</java.net.preferIPv4Stack> </systemProperties> </container> <configuration> <properties> <cargo.hostname>localhost</cargo.hostname> <cargo.jvmargs>-Xmx512m -XX:MaxPermSize=256m</cargo.jvmargs> </properties> </configuration> <deployables> <deployable> <groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> <type>war</type> <properties> <context>/your-app-context</context> </properties> </deployable> </deployables> </configuration> <executions> <execution> <id>start-container</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <id>stop-container</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin>
- 确认
containerId是wildfly10x,Cargo对不同WildFly版本有专属的ID标识,写错会导致容器识别失败。 - 检查
jboss.home指向的路径是否有读写权限,Cargo下载的WildFly会存放在这个目录,权限不足会直接导致安装失败。 - 嵌入式容器别加过于复杂的JVM参数,避免内存溢出或者启动卡死。
3. 检查Arquillian测试配置
- 确保测试类的Arquillian注解配置正确:
@RunWith(Arquillian.class) public class YourServiceIntegrationTest { @Deployment public static WebArchive createDeployment() { return ShrinkWrap.create(WebArchive.class) .addClasses(YourService.class, YourDao.class) .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); } @Test public void testBusinessLogic() { // 测试逻辑 } }
- 确认
beans.xml存在且配置正确:WildFly 10对应CDI 1.2,空的beans.xml即可,别用JEE 8的CDI 2.0配置(WildFly 10不支持)。 - 检查Arquillian依赖是否正确引入嵌入式适配器:
<dependency> <groupId>org.jboss.arquillian.container</groupId> <artifactId>arquillian-wildfly-embedded</artifactId> <version>1.0.0.Final</version> <scope>test</scope> </dependency>
4. 验证Failsafe插件配置
确保Failsafe正确绑定到集成测试阶段,且匹配正确的测试类:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.2</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> <configuration> <includes> <include>**/*IT.java</include> </includes> </configuration> </plugin>
- 运行集成测试要执行
mvn verify或mvn integration-test,别用mvn test(后者只跑单元测试)。 - 如果你的测试类不是以
IT结尾,要修改<includes>里的匹配规则。
5. 排查POM配置文件激活问题
你提到POM有3个配置文件,一定要确保运行集成测试时激活了包含Cargo、Arquillian、Failsafe配置的那个profile:
- 手动指定profile运行:
mvn verify -Pyour-integration-profile。 - 在IntelliJ IDEA的Maven面板里,确认对应的profile已经勾选激活。
6. 查看日志定位具体异常
如果以上配置都没问题,一定要去看详细日志找根因:
- Cargo启动容器的日志在
target/cargo/logs目录下,查看server.log,看容器是否正常启动、有没有部署失败的报错。 - 开启Arquillian调试日志:添加JVM参数
-Darquillian.debug=true,能看到测试过程中容器连接、部署的详细步骤。
内容的提问来源于stack exchange,提问作者robyp7
相关产品推荐
相关产品推荐

