Wednesday, January 04, 2006

Creating an ear, in the simplest way

I don't know how many times I've wanted to test something simple in an ear, but wanted to avoid the complexities of the application I'm working on, or the container I'm working in. I end up having to remind myself what are the basic requirements for creating an ear. That often takes a while, since in a normal working environment, you create the ear and forget about it.

So, I created a very simple ear here, and deployed it in JBoss. It has only four files: a build file, an application configuration, a web configuration, and a jsp. Here are the steps I followed:
  1. Installed JBoss Application Server. I installed version 4.0.3 SP1, available here: http://www.jboss.com/products/jbossas/downloads. It's an executable jar, and I did a complete install, and accepted all the defaults.
  2. Created a new project in my IDE. My project has the following top-level directories:
    • bin/ -- for the build file
    • conf/ -- for application configuration, in this case just application.xml
    • testWar/ -- a basic web application
  3. Created a META-INF/ directory in conf/, and inside put an application.xml file (see below). All that this file does is configure the web-app at the context root /testEar.
  4. Created a web/jsp/ directory in testWar/, and inside put a hello.jsp file (see below).
  5. Created a WEB-INF/ directory in testWar/, and inside put a web.xml file (see below). All this file does is configure hello.jsp as the welcome page.
  6. Created a build.xml file in bin/ (see below).
  7. Ran the ant build (in the bin directory, ran "ant build". (I assume a JDK and ant are installed).
  8. Copied the resulting ear (testEar.ear) into the JBoss deployment directory (in my installation, this is in jboss-4.0.3SP1/server/default/deploy).
  9. Started JBoss (in my installation, I ran jboss-4.0.3SP1/bin/run.bat).
  10. Pointed my browser to http://localhost:8080/testEar.
Here is application.xml:
<application xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"
version="1.4">
<description>Test Ear</description>
<display-name>Test Ear</display-name>
<module>
<web>
<web-uri>testWar.war</web-uri>
<context-root>testEar</context-root>
</web>
</module>
</application>


Here is hello.jsp:

Hello, World


Here is web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<welcome-file-list>
<welcome-file>/jsp/hello.jsp</welcome-file>
</welcome-file-list>
</web-app>


Here is build.xml:

<project name="testEar" default="build">

<property name="project.root.dir" value=".."/>
<property name="conf.dir" value="${project.root.dir}/conf"/>
<property name="test.war.dir" value="${project.root.dir}/testWar"/>

<target name="build">
<war destfile="testWar.war" webxml="${test.war.dir}/WEB-INF/web.xml">
<fileset dir="${test.war.dir}/web"/>
</war>
<ear destfile="testEar.ear" appxml="${conf.dir}/META-INF/application.xml">
<fileset dir="." includes="*.war"/>
</ear>
</target>
</project>

2 Comments:

Anonymous Anonymous said...

hello,

this worked for me on jboss 5 except that i had to change the header in web.xml to



Marc

February 15, 2010 at 8:49 AM  
Anonymous Anonymous said...

Hi all,

apparantly pasting xml doesnt work correctly, i had to change the web-app tag in web.xml to

version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

best wishes marc

February 15, 2010 at 8:56 AM  

Post a Comment

<< Home