4

I have a handful or EJB jar modules which also include their web resources as described in in this question.

I would now like to deploy this as part of an EAR.

Now, I could do this as a war without any issues and that is my backup plan. However, I would like to understand if it is possible to deploy multiple "web fragment" jars into an ear to form a complete web application without having a war file in there to combine it all together.

Additionally, If I have multiple war files that depend on one or more of these jars, will I have to build them such that these jars are duplicated within the WEB-INF/lib of the war files even if the war files then end up within the same ear?

In this case, if the jar files include entities, would they also be needed in the root of the ear so that a global persistence unit can be defined?

I am on JBoss 7 though I would prefer a standard container independent solution if possible.

Community
  • 1
  • 1
drone.ah
  • 1,152
  • 14
  • 28

1 Answers1

3

This will work on any JEE5 compliant server.

Let's assume you have

  • 2 EJB called myejb1.jar and myejb2.jar
  • 2 WAR webapps called mywebapp1.war and mywebapp2.war
  • 2 common JARs called log4j.jar and mycommon.jar

You want to package it all into an EAR file called myapp.ear.

The myapp.ear directory structure will look like this:

myapp.ear:
   META-INF/application.xml
   myejb1.jar
   myejb2.jar
   mywebapp1.war
   mywebapp2.war
   lib/log4j.jar
   lib/mycommon.jar

The contents of your META-INF/application.xml will contain

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/application_5.xsd">

    <module>
        <ejb>myejb1.jar</ejb>
    </module>
    <module>
        <ejb>myejb2.jar</ejb>
    </module>

    <module>
        <web>
            <web-uri>mywebapp1.war</web-uri>
            <context-root>webapp1</context-root>
        </web>
    </module>
    <module>
        <web>
            <web-uri>mywebapp2.war</web-uri>
            <context-root>webapp2</context-root>
        </web>
    </module>

    <library-directory>lib</library-directory>
</application>

You will be able to access your web apps via the URL
http://myJBossServer.url/webapp1/
http://myJBossServer.url/webapp2/

You can also share static resources like CSS, images, JavaScript among multiple webapps. For example, I have a static-content.jar file with this directory layout:

static-content.jar:
   META-INF/resources/css/my.css
   META-INF/resources/img/my.jpg
   META-INF/resources/js/jQuery.js
   META-INF/resources/js/node.js

I put the static-content.jar in the WEB-INF/lib directory on both my webapps during build time. Now

http://myJBossServer.url/webapp1/css/my.css
http://myJBossServer.url/webapp2/css/my.css

have the same source from static-content.jar.

If I wanted to override the default my.css in webapp2 only, I can put the modified my.css into the mywebapp2.war directly.

mywebapp2.war
   css/my.css
   WEB-INF/lib/static-content.jar

The css/my.css in the WAR will override the my.css from static-content.jar

  • If there are web resources (files within META-INF/resources/), will it then copied into both the webapps? How can this be controlled - i.e. if I want them deployed to only one of the contexts? – drone.ah Mar 03 '13 at 11:30
  • Are you talking about shared static resource JARs? If so I'll modify the answer above. – Jirawat Uttayaya Mar 03 '13 at 15:50
  • I am talking jars with resources and I understand your solution. However, I was looking for an ear based solution which will avoid duplication of the jars within the WEB-INF/lib folders. I did mention your solution and the problem with it in my question. Is there a way to deploy an ear with a web context (the resources come from the jar files) without a war file? This was really my original questions. – drone.ah Mar 04 '13 at 11:45
  • I am looking for the same thing: i have a library that is in the ear's /lib/ folder, that contains static resources in the /META-INF/resources/ location inside the jar, that i want to reuse in multiple ".war" web apps that are in the same ".ear". Is stuffing the same library in each war redundantly the only option we have? – dammkewl May 20 '16 at 07:24