您可以使用 Cheesus 提供的方式。当我处理现有的 EAR 时,我更喜欢将运行测试的 WAR 与我放入特殊 JAR 以及其他测试 EJB 的实际测试分开。我的部署如下所示:
@Deployment
public static EnterpriseArchive createDeployment() {
String path = System.getProperty(EAR_PATH);
File f = new File(path);
EnterpriseArchive ear = ShrinkWrap.createFromZipFile(EnterpriseArchive.class, f);
final JavaArchive foobarEjb = ShrinkWrap.create(JavaArchive.class, "foobarEjb.jar");
foobarEjb.addClasses(
MyTest1.class,
MyTest2.class);
ear.addAsModule(foobarEjb);
final WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
.addAsWebInfResource("WEB-INF/web.xml")
.addAsWebResource("index.xhtml");
ear.addAsModule(Testable.archiveToTest(war));
modifyApplicationXML(ear);
modifyJBossDeploymentStructure(ear);
return ear;
}
注意修改application.xml
和的方法jboss-deployment-structure.xml
。我需要这些来正确地将 JAR 初始化为 EAR 中的 EjbModule。
示例如何更改application.xml
:
private static void modifyApplicationXML(EnterpriseArchive ear) {
Node node = ear.get("META-INF/application.xml");
DescriptorImporter<ApplicationDescriptor> importer = Descriptors.importAs(ApplicationDescriptor.class, "test");
ApplicationDescriptor desc = importer.fromStream(node.getAsset().openStream());
String xml = desc.exportAsString();
// remove lib definition
xml = xml.replaceAll("<library-directory>.*<\\/library-directory>", "");
desc = (ApplicationDescriptor) importer.fromString(xml);
// append foobar test ejbs
desc.ejbModule("foobarEjb.jar");
// append test war
desc.webModule("test.war", "/test");
// append lib definition
desc.libraryDirectory("lib/");
Asset asset = new StringAsset(desc.exportAsString());
ear.delete(node.getPath());
ear.setApplicationXML(asset);
}