Create Ant EAR Project Eclipse

Create Ant EAR Project Eclipse

EAR project in java binds one or more Java Enterprise Edison modules. Together this includes web module, JAR files, client module, JPA module, EJB module, connector module etc…EAR application deploys in the form of archive ear. EAR project mapped many Java EE project together and mapping information are stored in metadata files that is called application.xml file. Please follow below steps to crate EAR project and deploy on JBoss server:

Note: We will use Eclipse Kepler to create project because this version comes with maven plug-in. You could also use any version of Eclipse which comes with maven plug-in or install in eclipse if not available.

Final project structure:

Create Ant EAR Project Eclipse

  • Create dynamic web project. To create click File –> New –> Other –> Web –> Dynamic Web Project. Give project name: JavaHonkAntWAR
  • Add index.jsp inside WebContent folder below to print successful message on console after deployment.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Create Ant EAR project successfully deployed on JBoss!
</body>
</html>
  • Create java project. Click File –> New –> Other –> Java –> Java Project name: JavaHonkAntJAR
  • Now create EAR project and include two project JAR and WAR in it. Click File –> New –> Other –> Java EE –> Enterprise Application Project and add as below:

Create Ant EAR Project Eclipse Create Ant EAR Project Eclipse

Create Ant EAR Project Eclipse

  • Now enterprise application got created. We could run directly in eclipse with Jboss server. If you are not sure how to configure and run Jboss in eclipse please use this tutorial. To run EAR project right click JavaHonkAntEAR –> Run As –> Run on Server. You will below successful message on web page:

Create Ant EAR Project Eclipse

Now to run through ant build file please copy and paste below build.xml

  • Copy below build.xml to JavaHonkAntJAR project:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Java Honk JAR Project" default="all" basedir=".">

	<property name="rootWorkspace" value="C:\JavaHonk\eclipse\eclipse\workspaceEAR" />
	<property name="classdir" value="${basedir}/build/classes" />
	<property name="jarFile" value="${rootWorkspace}/JavaHonkAntEAR/build/jar/JavaHonkAntJAR.jar"/>	
	<property name="jarDir" value="${rootWorkspace}/JavaHonkAntEAR/build/jar"/>
	
	<!-- Define the CLASSPATH -->
	<path id="classpath">
		<fileset dir="${basedir}/lib">
			<include name="*.jar" />
		</fileset>
	</path>
	
	<!-- Main target -->
	<target name="all" depends="init,compile,buildJar" />	
	
	<target name="init" description="Initial process">
		<delete dir="${jarDir}"/>
		<delete dir="${classdir}"/>
		<echo message="Cleaned" />		
		<mkdir dir="${jarDir}"/>
		<mkdir dir="${classdir}"/>
		<echo message="All new directory created" />		
	</target>

	<target name="compile">
		<javac srcdir="src" destdir="build/classes" includeantruntime="false">
			<classpath refid="classpath" />
		</javac>
	</target>
	
	<!-- Create Jar File -->
	<target name="buildJar">
		<jar destfile="${jarDir}/JavaHonkAntJAR.jar" basedir="build/classes"/>
	</target>

</project>
  • Copy below build.xml to JavaHonkAntWAR project:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Create Ant EAR Project" default="all" basedir=".">
 
	<property name="rootWorkspace" value="C:\JavaHonk\eclipse\eclipse\workspaceEAR" />
	<property name="web" value="${basedir}/WebContent" />
	<property name="classdir" value="${basedir}/WebContent/WEB-INF/classes" />
 	<property name="warFile" value="${rootWorkspace}/JavaHonkAntEAR/build/war/JavaHonkAntWAR.war" />	
	<property name="warDir" value="${rootWorkspace}/JavaHonkAntEAR/build/war" />
	
	<!-- Define the CLASSPATH -->
	<path id="classpath">
		<fileset dir="${basedir}/WebContent/WEB-INF/lib">
			<include name="*.jar" />
		</fileset>
	</path>
	
	<!-- Main target -->
	<target name="all" depends="init,compile,buildWar" />
	
	<target name="init" description="Initial process">
		<delete dir="${warDir}"/>
		<delete dir="${classdir}"/>
		<echo message="Cleaned" />		
		<mkdir dir="${warDir}"/>
		<mkdir dir="${classdir}"/>
		<echo message="All new directory created" />		
	</target>
	
	<target name="compile">
		<javac srcdir="src" destdir="${classdir}" includeantruntime="false">
			<classpath refid="classpath" />
		</javac>
	</target> 
	
	<!-- Create the War File -->
	<target name="buildWar">
		<fileset dir="${classdir}" includes="**/*.class" />
		<fileset dir="${web}" includes="web.xml" />
		<fileset dir="${web}" includes="**/*.*" />
		<!-- Create war file and place in ear directory -->
		<jar jarfile="${warFile}" basedir="${web}" />
	</target> 
	
</project>
  • Copy below build.xml to JavaHonkAntEAR project:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Create Ant EAR Project" default="all" basedir=".">
 
	<property name="rootWorkspace" value="C:\JavaHonk\eclipse\eclipse\workspaceEAR"/>
	<property name="deploymentdescription" value="${basedir}/EarContent" />
	<property name="dir.deploy.folder" value="C:\JavaHonk\Jboss\jboss-6.1.0.Final\server\default\deploy" /> 
 	<property name="earFile" value="${basedir}/build/ear/JavaHonkAntEAR.ear" />	
	<property name="earDir" value="${basedir}/build/ear" />
	
	<!-- Main target -->
	<target name="all" depends="createJAR,createWAR,buildEar,deploy" />
	
	<target name="createJAR">
		<ant dir="${rootWorkspace}/JavaHonkAntJAR" antfile="build.xml"/>		
	</target>
	
	<target name="createWAR">
		<ant dir="${rootWorkspace}/JavaHonkAntWAR" antfile="build.xml"/>		
	</target>
	
	<!-- Create the Ear File -->
	<target name="buildEar">
		<delete dir="${earDir}"/>
		<mkdir dir="${earDir}"/>
		<!-- Create ear file and place in ear directory -->
		<ear destfile="${earFile}" appxml="${deploymentdescription}/META-INF/application.xml">
			<fileset dir="${basedir}/build/jar" includes="**/*.jar" />
			<fileset dir="${basedir}/build/war" includes="**/*.war" />
		</ear>
	</target>
 
	<target name="deploy">
		<copy todir="${dir.deploy.folder}" overwrite="yes">
			<fileset file="${earFile}" />
		</copy>
		<echo message="deployed" />
	</target> 
	
</project>
  • Note: Please don’t forget to change directory path of Jboss and workspace in build.xml file.
  • Now right click main build.xml file from JavaHonkAntEAR project –> Run As –> Ant Build. This will call both JAR and WAR build then will make EAR file and deploy to the JBoss deploy folder.
  • To verify please go to Jboss deploy folder and check if ( Jboss-6.1.0.Final\server\default\deploy) JavaHonkAntEAR.ear copied or not. Now start JBoss server using jboss-6.1.0.Final\bin\run.bat file check on console below:

Create Ant EAR Project Eclipse

  • Once your deployment is complete please use below URL in your browser there you will below response:

Create Ant EAR Project Eclipse

  • That’s it. For more information about create EAR project please visit this link

Leave a Reply

Your email address will not be published. Required fields are marked *