3 Replies Latest reply on Mar 2, 2011 9:50 PM by Iván Pazmiño

    Adding a jar to the archive

    Naresh Bhatia Novice

      To create a Test archive, I see the following example code:

       

      `       return ShrinkWrap.create(JavaArchive.class, "test.jar")

                .addClasses(ClassUnderTest.class)

                .addManifestResource(

                   EmptyAsset.INSTANCE,

                   ArchivePaths.create("beans.xml"));

      `
      In my project, the class under test needs other classes that are packaged under a different jar. How do I add that jar to the JavaArchive above? Or do I have to create a WarArchive for this?
      Thanks.
             return ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addClasses(SecurityService.class)
                .addManifestResource(
                   EmptyAsset.INSTANCE,
                   ArchivePaths.create("beans.xml"));

        • 1. Re: Adding a jar to the archive
          Aslak Knutsen Master

          You could use archive.merge(archive) to join the test.jar with your other.jar..

           

          archive.merge(

             ShrinkWrap.create(JavaArchive.class).as(ZipImporter.class).import(file))

           

          or simply add based on packages from the other.jar:

           

          archive.addPackages(true, OtherClass.class.getPackage())

           

          but in general you probably want to deploy as something that could hold libraries, e.g WebArchive

          • 2. Re: Adding a jar to the archive
            Naresh Bhatia Novice

            Thanks Aslak. I have changed my deployment to create a war. It is working fine now.

             

                    WebArchive archive = ShrinkWrap
                        .create(WebArchive.class, "test.war")
                        .addClasses(SecurityService.class)
                        .addPackage(UserRepository.class.getPackage())
                        .addManifestResource("test-persistence.xml", "persistence.xml")
                        .addWebResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));

                    return ShrinkWrap

                        .create(WebArchive.class, "test.war")

                        .addClasses(SecurityService.class)

                        .addPackage(UserRepository.class.getPackage())

                        .addManifestResource("test-persistence.xml", "persistence.xml")

                        .addWebResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));

            • 3. Re: Adding a jar to the archive
              Iván Pazmiño Novice

              To merge the jar with the test archive you need to cast-back to JavaArchive the ZipImporter

              ZipImporter zip = ShrinkWrap.create(JavaArchive.class).as(ZipImporter.class).importZip(file); archive.merge(zip.as(JavaArchive.class));