4 Replies Latest reply on Feb 1, 2016 8:51 AM by pzelenka00

    Testing REST service from EAR with mulitple web modules

    pzelenka00

      Hi,

       

      I'm struggling with ShrinkWrap deployment of EAR that has multiple web modules. I always get this exception: java.lang.UnsupportedOperationException: Multiple WebArchives found in dag.ear. Can not determine which to enrich.

       

      @Deployment
      public static Archive<?> createEarDeployment() {
          EnterpriseArchive ear = ShrinkWrap.create(ZipImporter.class, "app.ear").importFrom(new File("myapp.ear")).as(EnterpriseArchive.class);
          return ear;
      }

       

      Any idea how to solve it?

       

      Thanks

       

      Pavel

        • 1. Re: Testing REST service from EAR with mulitple web modules
          thomas_schindler

          Hi Pavel,

           

          I'm struggling with the same Problem.  Unfortunately, I have no solution, but maybe a workaround that may be useful for you too. I have an EAR in which two web modules are embedded. As I'm not interestet in testing the web modules, I simply remove them from the EnterpriseArchive:

           

                    EnterpriseArchive ear = ShrinkWrap.createFromZipFile(EnterpriseArchive.class, new File(new File(".."), "/projectdir/build/libs/some.ear"));

                    ear.delete("webmodule1.war");

                    ear.delete("webmodule2.war");

                    rewriteAppDescriptor(ear).

                    return ear;

                   

             

          If you do that, you have to remove the deleted modules from the application.xml as well. I did this in a method "private static void rewriteAppDescriptor(EnterpriseArchive ear)".

           

          Hope that is helpful for you too.

           

          Thomas

          • 2. Re: Testing REST service from EAR with mulitple web modules
            pzelenka00

            Hi Thomas,

             

            I used the same approach i.e. romoving unnecessary web modules from built EAR. Could you please share the implementation of the method "private static void rewriteAppDescriptor(EnterpriseArchive ear)"?

            I use application.xml which I have as a separate file. So it would be nice if I can build or modify application.xml programatically.

             

            Thanks

             

            Pavel

            • 3. Re: Testing REST service from EAR with mulitple web modules
              thomas_schindler

              Hi Pavel,

               

              actually, what I do is, reading the application.xml from the ear and writing it without the web modules on harddisk, then I remove the application.xml from the ear and add the written file to the ear again. Before I share the implementation, I have to say, this is done for a proof-of-concept and is no production code. Removing is therefore very straightforward without much checking.

               

               

               

                 private static void adaptWarModules(EnterpriseArchive ear)
                 {
                    ear.delete("webmodule1.war");
                    ear.delete("webmodule2.war");
                    Node node = ear.get("META-INF/application.xml");
                    String startModuleString = "<module>";
                    String endModuleString = "</module>";
                    String webModuleString = "<web>";
                    File appFile = new File("d:/temp/application.xml");
                    try (InputStream stream = node.getAsset().openStream())
                    {
                       BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                       FileWriter fw = new FileWriter(appFile);
                       StringBuilder out = new StringBuilder();
                       StringBuilder module = new StringBuilder();
                       boolean moduleStartet = false;
                       String line;
                       while ((line = reader.readLine()) != null)
                       {
                          out.append(line).append("\n");
                          if (line.contains(startModuleString))
                          {
                             moduleStartet = true;
                             module.append(line).append("\n");
                             continue;
                          }
                          else if (line.contains(endModuleString))
                          {
                             module.append(line).append("\n");
                             if (!module.toString().contains(webModuleString))
                             {
                                fw.write(module.toString());
                                fw.flush();
                             }
                             module = new StringBuilder();
                             moduleStartet = false;
                             continue;
                          }
                          else if (moduleStartet)
                          {
                             module.append(line).append("\n");
                             continue;
                          }
              
              
                          fw.write(line);
                          fw.write("\n");
                          fw.flush();
              
              
                       }
                       reader.close();
                       fw.close();
              
              
                    }
                    catch (IOException e)
                    {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }
                    ear.delete("META-INF/application.xml");
                    ear.addAsManifestResource(appFile);
                 }
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              • 4. Re: Testing REST service from EAR with mulitple web modules
                pzelenka00

                Hi Thomas,

                 

                many thanks for your reply

                 

                Pavel