3 Replies Latest reply on Jun 6, 2012 8:03 AM by charlee_ch

    How to add a class and all its parents and dependencies ?

    aogier

      I've got to test that class :

      {code:java}

      @Stateless

      public class AService extends Service {

           @Inject

           BService bservice;

           @Inject

           CService cservice;

           // here the method I want to test

      }

      {code}

       

      "Service" class is in another jar.

       

      I'm following that tutorial.

       

      So I've written that createDeployment() method in my test :

       

      {code:java}

      @Deployment

      public static Archive<?> createDeployment() {

          return ShrinkWrap.create(JavaArchive.class)

                    .addClass(AService.class)

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

                    .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

      }

      {code}

       

      Now I've got a "java.lang.NoClassDefFoundError: Service"... Is there a way to say to ShrinkWrap to include all the parents classes of my "AService" class, the dependent classes (@Inject) BService and CService automatically ? (without adding them each time with "addClass" because I've got a long depdenency tree else...)

        • 1. Re: How to add a class and all its parents and dependencies ?
          charlee_ch

          Hi,

           

          The JavaArchive has no method for adding the dependencies. You may use the WebArchive or EnterpriseArchive by using the method named "addAsLibraries()". If you are in the Maven environment, the Shrinkwrap Resolver will help you as the following link: -

           

          http://www.davidsalter.com/2012/04/using-the-shrinkwrap-maven-resolver-for-arquillian-tests.html

           

          https://community.jboss.org/wiki/HowToIAddMavenArtifactsToMyShrinkWrapArchives

           

          I hope this may help.

           

          Regards,

           

          Charlee Ch.

          1 of 1 people found this helpful
          • 2. Re: How to add a class and all its parents and dependencies ?
            aogier

            Thank you for that reply Charlee, it helps me to go a step further, but I'm not done yet.

             

            Here is now what I'm doing :

             

            @Deployment
            public static Archive<?> createDeployment() {
                      final MavenDependencyResolver resolver = DependencyResolvers.use(MavenDependencyResolver.class).loadMetadataFromPom("pom.xml");
            
                      return ShrinkWrap.create(WebArchive.class)
                                .addClass(AService.class)
                                .addAsLibraries(resolver.artifact("my.company:myproject-services:1.0-SNAPSHOT").resolveAsFiles())
                                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
            }
            

             

            My problem is now that JBoss loads me 2 persistence units definitions as myproject-services depends on myproject-orm which contains the @Entity classes but also "persistence.xml" which points to another PU that the one defined in my "test-persistence.xml"...

             

            Do you know a way to avoid the "persistence.xml" from myproject-orm to be include in the ShrinkWrap Archive, but all the classes & dependencies of that project to still be included ?

             

            PS : Here is the JBoss stack

             

            11:18:10,235 INFO  [org.jboss.as.jpa] (MSC service thread 1-16) JBAS011401: Read persistence.xml for test
            11:18:10,237 INFO  [org.jboss.as.jpa] (MSC service thread 1-16) JBAS011401: Read persistence.xml for myproject
            11:18:10,245 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC00001: Failed to start service jboss.deployment.unit."2036359f-2a79-4e03-b752-0b3100399f44.war".DEPENDENCIES: org.jboss.msc.service.StartException in service jboss.deployment.unit."2036359f-2a79-4e03-b752-0b3100399f44.war".DEPENDENCIES: Failed to process phase DEPENDENCIES of deployment "2036359f-2a79-4e03-b752-0b3100399f44.war"
                      at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:119) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
                      at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
                      at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
                      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_26]
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_26]
                      at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_26]
            Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment "2036359f-2a79-4e03-b752-0b3100399f44.war".  Either change the application to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
                      at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:69)
                      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getPersistenceUnit(JPAAnnotationParseProcessor.java:284)
                      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getBindingSource(JPAAnnotationParseProcessor.java:220)
                      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processField(JPAAnnotationParseProcessor.java:151)
                      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processPersistenceAnnotations(JPAAnnotationParseProcessor.java:118)
                      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.deploy(JPAAnnotationParseProcessor.java:90)
                      at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
                      ... 5 more
            
            
            • 3. Re: How to add a class and all its parents and dependencies ?
              charlee_ch

              Hi Anthony,

               

              Please correct me, if I'm wrong. I understand that the "my.company:myproject-services:1.0-SNAPSHOT" contains its own "persistence.xml". Since it is a ready packaged jar file, I thought that the Shrinkwrap cannot exclude the resources inside it.

               

              I'm not familiar with JBoss. I understand that you may use the @PersistenceContext/@PersistenceUnit injection with without any parameter by default. Since you have 2 persistence units, the JBoss may confuse which the persistence unit should be used is. You may need to mention the using unit name explicitly as the following: -

               

              {code:java}

                  @PersistenceUnit(unitName = "...")

                  private EntityManagerFactory emf;

               

              {code}

               

              and/or

               

              {code:java}

                  @PersistenceContext(unitName = "...")

                  private EntityManager em;

              {code}

               

              I also search the term "JBAS011470" via google and found the useful information here: - https://community.jboss.org/message/642505 They also mention to specify the unit name expliclitly for each @PersistenceContext and @PersistenceUnit as well.

               

              I hope this may help.

               

              Regards,

               

              Charlee Ch.