When writing UI tests, for instance with WebDriver, you may want to add all the resources (e.g., web templates) to the ShrinkWrap archive rather than having to add each one explicitly. You can perform this task using the ExplodedImporter.
NOTE: The examples in this FAQ use the ShrinkWrap 1.0.0 APIs, which are included transitively by Arquillian Core 1.0.0.Final.
Let's assume that the web source directory is src/main/webapp (the standard Maven / Gradle convention). We'll define a constant for this location:
private static final String WEBAPP_SRC = "src/main/webapp";
Here's how we can import all the files under the web source directory into the root of the web archive (war):
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war"); war.merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class) .importDirectory(WEBAPP_SRC).as(GenericArchive.class), "/", Filters.includeAll());
What we are doing is creating a generic (virtual) archive, applying the importer view to it (ExplodedImporter), importing the contents of the web source directory, reapplying the archive view, then copying the contents of that (virtual) archive to the root of the web archive (war).
You can limit the files that are copied using a regular expression filter. Let's only important files that end in .xhtml:
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war"); war.merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class) .importDirectory(WEBAPP_SRC).as(GenericArchive.class), "/", Filters.include(".*\\.xhtml$"));
If this seems complex, you can create a helper method to hide the type switching (the as() methods).
Alternatively, you can walk walk the file system explicitly and discovered files to the archive one at a time. The following code imports files in the web source directory, but does not recurse into sub-directories:
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war"); for (File f : new File(WEBAPP_SRC).listFiles()) { war.addAsWebResource(f); }
A simpler, more sematic way to perform this task is being discussed in the issue SHRINKWRAP-247.
Comments