How do I add a resource to a Java archive using ShrinkWrap
krmahadevan Mar 22, 2018 11:32 PMMy use-case: I am basically adding tests to the open source library TestNG. As part of test, I need to test if TestNG is able to properly work with a jar file and extract out tests from it. Earlier we had a pre-built jar that was checked-in to the codebase and the tests were working with it. Then I came to know that I could use ShrinkWrap to dynamically create a jar.
Here's the version information that I am working with
- org.jboss.shrinkwrap:shrinkwrap-api:1.2.6
- org.jboss.shrinkwrap:shrinkwrap-impl-base:1.2.6
I am trying to basically add a few resources to a jar that I am creating using ShrinkWrap. The addition of resources works fine when I build the code locally on my Mac. But when I attempt to build the code via Travis (Travis is basically used as the CI tool for the open source library TestNG), the build keeps failing with the following error
java.lang.IllegalArgumentException: src/test/resources/jarfileutils/testng-tests.xml was not found in any available ClassLoaders at org.jboss.shrinkwrap.impl.base.container.ContainerBase.addNestedJarFileResource(ContainerBase.java:811) at org.jboss.shrinkwrap.impl.base.container.ContainerBase.addAsResource(ContainerBase.java:1150) at org.testng.jarfileutils.JarCreator.generateJar(JarCreator.java:34) at org.testng.JarFileUtilsTest.generateTestJar(JarFileUtilsTest.java:30)
Here's how my code looks like
public class JarCreator { public static File generateJar() throws IOException { File jarFile = File.createTempFile("testng", ".jar"); ConfigurationBuilder builder = new ConfigurationBuilder(); Configuration config = builder .classLoaders(Collections.singletonList(Thread.currentThread().getContextClassLoader())) .build(); JavaArchive archive = ShrinkWrap .createDomain(config) .getArchiveFactory() .create(JavaArchive.class, "testng-tests.jar") .addClasses(getTestClasses()); for (String resource : getResources()) { archive = archive.addAsResource(getFile(resource), new BasicPath(resource)); } archive.as(ZipExporter.class).exportTo(jarFile, true); return jarFile; } private static File getFile(String resource) { return new File("src/test/resources/" + resource); } private static Class[] getTestClasses() { return new Class[]{ SampleTest1.class, SampleTest2.class, SampleTest3.class, SampleTest4.class, SampleTest5.class }; } private static List getResources() { return Arrays.asList( "jarfileutils/testng-tests.xml", "jarfileutils/child.xml", "jarfileutils/child/child.xml", "jarfileutils/child/childofchild/childofchild.xml", "jarfileutils/childofchild/childofchild.xml" ); } }
Here's how my folder structure looks like in src/test/resources
08:56 $ tree src/test/resources/jarfileutils/ src/test/resources/jarfileutils/ ├── child │ ├── child.xml │ └── childofchild │ └── childofchild.xml ├── child.xml ├── childofchild │ └── childofchild.xml └── testng-tests.xml
Can someone please let me know, if my code for creating a simple jar is correct or not.
I have tried to examine the jar that gets created and it seems ok to me. But I dont know why I get the error only on Travis.
In case anyone is interested, here's the complete travis log : https://api.travis-ci.org/v3/job/357213270/log.txt